Skip to content

FIX: Fixing test that does not work properly / Failing in connection pool - #493

Closed
Subrata (subrata-ms) wants to merge 10 commits into
mainfrom
subrata-ms/ConnectionPoolTestFix
Closed

FIX: Fixing test that does not work properly / Failing in connection pool#493
Subrata (subrata-ms) wants to merge 10 commits into
mainfrom
subrata-ms/ConnectionPoolTestFix

Merge branch 'main' into subrata-ms/ConnectionPoolTestFix

4fa4ba8
Select commit
Loading
Failed to load commit list.
Azure Pipelines / MSSQL-Python-PR-Validation failed May 11, 2026 in 35m 42s

Build #pr-validation-pipeline had test failures

Details

Tests

  • Failed: 9 (0.41%)
  • Passed: 2,133 (98.34%)
  • Other: 27 (1.24%)
  • Total: 2,169

Annotations

Check failure on line 933 in Build log

See this annotation in the file changed.

@azure-pipelines azure-pipelines / MSSQL-Python-PR-Validation

Build log #L933

Bash exited with code '134'.

Check failure on line 929 in Build log

See this annotation in the file changed.

@azure-pipelines azure-pipelines / MSSQL-Python-PR-Validation

Build log #L929

Bash exited with code '134'.

Check failure on line 15 in Build log

See this annotation in the file changed.

@azure-pipelines azure-pipelines / MSSQL-Python-PR-Validation

Build log #L15

Bash exited with code '1'.

Check failure on line 992 in Build log

See this annotation in the file changed.

@azure-pipelines azure-pipelines / MSSQL-Python-PR-Validation

Build log #L992

Bash exited with code '134'.

Check failure on line 1 in test_connection_execute_cursor_lifecycle

See this annotation in the file changed.

@azure-pipelines azure-pipelines / MSSQL-Python-PR-Validation

test_connection_execute_cursor_lifecycle

AssertionError: Cursor should be garbage collected after going out of scope
assert <mssql_python.cursor.Cursor object at 0x7fb6d5859c10> is None
 +  where <mssql_python.cursor.Cursor object at 0x7fb6d5859c10> = <weakref at 0x7fb6d62ee9d0; to 'Cursor' at 0x7fb6d5859c10>()
Raw output
db_connection = <mssql_python.connection.Connection object at 0x7fb6d2476790>

    def test_connection_execute_cursor_lifecycle(db_connection):
        """Test that cursors from execute() are properly managed throughout their lifecycle"""
        import gc
        import weakref
        import sys
    
        # Clear any existing cursors and force garbage collection
        for cursor in list(db_connection._cursors):
            try:
                cursor.close()
            except Exception:
                pass
        gc.collect()
    
        # Verify we start with a clean state
        initial_cursor_count = len(db_connection._cursors)
    
        # 1. Test that a cursor is added to tracking when created
        cursor1 = db_connection.execute("SELECT 1 AS test")
        cursor1.fetchall()  # Consume results
    
        # Verify cursor was added to tracking
        assert (
            len(db_connection._cursors) == initial_cursor_count + 1
        ), "Cursor should be added to connection tracking"
        assert (
            cursor1 in db_connection._cursors
        ), "Created cursor should be in the connection's tracking set"
    
        # 2. Test that a cursor is removed when explicitly closed
        cursor_id = id(cursor1)  # Remember the cursor's ID for later verification
        cursor1.close()
    
        # Force garbage collection to ensure WeakSet is updated
        gc.collect()
    
        # Verify cursor was removed from tracking
        remaining_cursor_ids = [id(c) for c in db_connection._cursors]
        assert (
            cursor_id not in remaining_cursor_ids
        ), "Closed cursor should be removed from connection tracking"
    
        # 3. Test that a cursor is tracked but then removed when it goes out of scope
        # Note: We'll create a cursor and verify it's tracked BEFORE leaving the scope
        temp_cursor = db_connection.execute("SELECT 2 AS test")
        temp_cursor.fetchall()  # Consume results
    
        # Get a weak reference to the cursor for checking collection later
        cursor_ref = weakref.ref(temp_cursor)
    
        # Verify cursor is tracked immediately after creation
        assert (
            len(db_connection._cursors) > initial_cursor_count
        ), "New cursor should be tracked immediately"
        assert (
            temp_cursor in db_connection._cursors
        ), "New cursor should be in the connection's tracking set"
    
        # Now remove our reference to allow garbage collection
        temp_cursor = None
    
        # Force garbage collection multiple times to ensure the cursor is collected
        for _ in range(3):
            gc.collect()
    
        # Verify cursor was eventually removed from tracking after collection
>       assert cursor_ref() is None, "Cursor should be garbage collected after going out of scope"
E       AssertionError: Cursor should be garbage collected after going out of scope
E       assert <mssql_python.cursor.Cursor object at 0x7fb6d5859c10> is None
E        +  where <mssql_python.cursor.Cursor object at 0x7fb6d5859c10> = <weakref at 0x7fb6d62ee9d0; to 'Cursor' at 0x7fb6d5859c10>()

tests/test_003_connection.py:1184: AssertionError

Check failure on line 1 in test_cursor_cleanup_without_close

See this annotation in the file changed.

@azure-pipelines azure-pipelines / MSSQL-Python-PR-Validation

test_cursor_cleanup_without_close

AssertionError: assert 1 == 0
 +  where 1 = len({<weakref at 0x7fb6d29037e0; to 'Cursor' at 0x7fb6d25fcd50>})
 +    where {<weakref at 0x7fb6d29037e0; to 'Cursor' at 0x7fb6d25fcd50>} = <mssql_python.connection.Connection object at 0x7fb6d32271d0>._cursors
Raw output
conn_str = 'Server=172.17.0.3;Database=TestDB;Uid=SA;Pwd=Azure@123!;TrustServerCertificate=yes'

    def test_cursor_cleanup_without_close(conn_str):
        """Test that cursors are properly cleaned up without closing the connection"""
        conn_new = connect(conn_str)
        cursor = conn_new.cursor()
        cursor.execute("SELECT 1")
        cursor.fetchall()
        assert len(conn_new._cursors) == 1
        del cursor  # Remove the last reference
>       assert len(conn_new._cursors) == 0  # Now the WeakSet should be empty
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E       AssertionError: assert 1 == 0
E        +  where 1 = len({<weakref at 0x7fb6d29037e0; to 'Cursor' at 0x7fb6d25fcd50>})
E        +    where {<weakref at 0x7fb6d29037e0; to 'Cursor' at 0x7fb6d25fcd50>} = <mssql_python.connection.Connection object at 0x7fb6d32271d0>._cursors

tests/test_005_connection_cursor_lifecycle.py:80: AssertionError

Check failure on line 1 in test_connection_execute_cursor_lifecycle

See this annotation in the file changed.

@azure-pipelines azure-pipelines / MSSQL-Python-PR-Validation

test_connection_execute_cursor_lifecycle

AssertionError: Cursor should be garbage collected after going out of scope
assert <mssql_python.cursor.Cursor object at 0x7f0ed114aa90> is None
 +  where <mssql_python.cursor.Cursor object at 0x7f0ed114aa90> = <weakref at 0x7f0ed2701760; to 'Cursor' at 0x7f0ed114aa90>()
Raw output
db_connection = <mssql_python.connection.Connection object at 0x7f0ed26fd350>

    def test_connection_execute_cursor_lifecycle(db_connection):
        """Test that cursors from execute() are properly managed throughout their lifecycle"""
        import gc
        import weakref
        import sys
    
        # Clear any existing cursors and force garbage collection
        for cursor in list(db_connection._cursors):
            try:
                cursor.close()
            except Exception:
                pass
        gc.collect()
    
        # Verify we start with a clean state
        initial_cursor_count = len(db_connection._cursors)
    
        # 1. Test that a cursor is added to tracking when created
        cursor1 = db_connection.execute("SELECT 1 AS test")
        cursor1.fetchall()  # Consume results
    
        # Verify cursor was added to tracking
        assert (
            len(db_connection._cursors) == initial_cursor_count + 1
        ), "Cursor should be added to connection tracking"
        assert (
            cursor1 in db_connection._cursors
        ), "Created cursor should be in the connection's tracking set"
    
        # 2. Test that a cursor is removed when explicitly closed
        cursor_id = id(cursor1)  # Remember the cursor's ID for later verification
        cursor1.close()
    
        # Force garbage collection to ensure WeakSet is updated
        gc.collect()
    
        # Verify cursor was removed from tracking
        remaining_cursor_ids = [id(c) for c in db_connection._cursors]
        assert (
            cursor_id not in remaining_cursor_ids
        ), "Closed cursor should be removed from connection tracking"
    
        # 3. Test that a cursor is tracked but then removed when it goes out of scope
        # Note: We'll create a cursor and verify it's tracked BEFORE leaving the scope
        temp_cursor = db_connection.execute("SELECT 2 AS test")
        temp_cursor.fetchall()  # Consume results
    
        # Get a weak reference to the cursor for checking collection later
        cursor_ref = weakref.ref(temp_cursor)
    
        # Verify cursor is tracked immediately after creation
        assert (
            len(db_connection._cursors) > initial_cursor_count
        ), "New cursor should be tracked immediately"
        assert (
            temp_cursor in db_connection._cursors
        ), "New cursor should be in the connection's tracking set"
    
        # Now remove our reference to allow garbage collection
        temp_cursor = None
    
        # Force garbage collection multiple times to ensure the cursor is collected
        for _ in range(3):
            gc.collect()
    
        # Verify cursor was eventually removed from tracking after collection
>       assert cursor_ref() is None, "Cursor should be garbage collected after going out of scope"
E       AssertionError: Cursor should be garbage collected after going out of scope
E       assert <mssql_python.cursor.Cursor object at 0x7f0ed114aa90> is None
E        +  where <mssql_python.cursor.Cursor object at 0x7f0ed114aa90> = <weakref at 0x7f0ed2701760; to 'Cursor' at 0x7f0ed114aa90>()

tests/test_003_connection.py:1184: AssertionError

Check failure on line 1 in internal

See this annotation in the file changed.

@azure-pipelines azure-pipelines / MSSQL-Python-PR-Validation

internal

internal error
Raw output
Traceback (most recent call last):
  File "/opt/venv/lib/python3.11/site-packages/_pytest/main.py", line 318, in wrap_session
    session.exitstatus = doit(config, session) or 0
                         ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/_pytest/main.py", line 372, in _main
    config.hook.pytest_runtestloop(session=session)
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 512, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 167, in _multicall
    raise exception
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 139, in _multicall
    teardown.throw(exception)
  File "/opt/venv/lib/python3.11/site-packages/_pytest/logging.py", line 801, in pytest_runtestloop
    return (yield)  # Run all the tests.
            ^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 139, in _multicall
    teardown.throw(exception)
  File "/opt/venv/lib/python3.11/site-packages/_pytest/terminal.py", line 707, in pytest_runtestloop
    result = yield
             ^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 139, in _multicall
    teardown.throw(exception)
  File "/opt/venv/lib/python3.11/site-packages/pytest_cov/plugin.py", line 348, in pytest_runtestloop
    result = yield
             ^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 121, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/_pytest/main.py", line 396, in pytest_runtestloop
    item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 512, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 167, in _multicall
    raise exception
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 139, in _multicall
    teardown.throw(exception)
  File "/opt/venv/lib/python3.11/site-packages/_pytest/warnings.py", line 89, in pytest_runtest_protocol
    return (yield)
            ^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 139, in _multicall
    teardown.throw(exception)
  File "/opt/venv/lib/python3.11/site-packages/_pytest/assertion/__init__.py", line 192, in pytest_runtest_protocol
    return (yield)
            ^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 139, in _multicall
    teardown.throw(exception)
  File "/opt/venv/lib/python3.11/site-packages/_pytest/unittest.py", line 591, in pytest_runtest_protocol
    return (yield)
            ^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy/_callers.py", line 139, in _multicall
    teardown.throw(exception)
  File "/opt/venv/lib/python3.11/site-packages/_pytest/faulthandler.py", line 102, in pytest_runtest_protocol
    return (yield)
            ^^^^^
  File "/opt/venv/lib/python3.11/site-packages/pluggy
... [The stack trace has been truncated as it exceeded the maximum allowed size. Please refer to the complete log available in the Test Run attachments for full details.]