|
| 1 | +"""Interrupted-connection marks match only the connection that was interrupted.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from sqlbuild.runtime.execution_limits.classes.interrupted_connections import ( |
| 8 | + InterruptedConnections, |
| 9 | +) |
| 10 | +from tests.unit.src.sqlbuild.runtime.execution_limits._test_types import ( |
| 11 | + InterruptedConnectionReportsTestCase, |
| 12 | + InterruptedIdentityReuseTestCase, |
| 13 | +) |
| 14 | +from tests.unit.src.sqlbuild.runtime.execution_limits.helpers import PlaceholderConnection |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize( |
| 18 | + "test_case", |
| 19 | + [ |
| 20 | + InterruptedConnectionReportsTestCase( |
| 21 | + description="weakly referenced connection is reported once", |
| 22 | + new_connection=PlaceholderConnection, |
| 23 | + expected_reports=(True, False), |
| 24 | + ), |
| 25 | + InterruptedConnectionReportsTestCase( |
| 26 | + description="connection without weak references is reported once", |
| 27 | + new_connection=object, |
| 28 | + expected_reports=(True, False), |
| 29 | + ), |
| 30 | + ], |
| 31 | + ids=lambda case: case.description, |
| 32 | +) |
| 33 | +def test_given_marked_connection_when_checked_twice_then_reported_once( |
| 34 | + test_case: InterruptedConnectionReportsTestCase, |
| 35 | +) -> None: |
| 36 | + connections: InterruptedConnections = InterruptedConnections() |
| 37 | + connection: object = test_case.new_connection() |
| 38 | + connections.mark(connection) |
| 39 | + |
| 40 | + reports: tuple[bool, bool] = (connections.take(connection), connections.take(connection)) |
| 41 | + |
| 42 | + assert reports == test_case.expected_reports |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + "test_case", |
| 47 | + [ |
| 48 | + InterruptedIdentityReuseTestCase( |
| 49 | + description="new connection at a collected interrupted identity is not reported", |
| 50 | + expected_reuse_and_report=(True, False), |
| 51 | + ), |
| 52 | + ], |
| 53 | + ids=lambda case: case.description, |
| 54 | +) |
| 55 | +def test_given_collected_marked_connection_when_new_connection_reuses_identity_then_not_reported( |
| 56 | + test_case: InterruptedIdentityReuseTestCase, |
| 57 | +) -> None: |
| 58 | + connections: InterruptedConnections = InterruptedConnections() |
| 59 | + interrupted: PlaceholderConnection = PlaceholderConnection() |
| 60 | + interrupted_identity: int = id(interrupted) |
| 61 | + connections.mark(interrupted) |
| 62 | + del interrupted |
| 63 | + |
| 64 | + reused: PlaceholderConnection = PlaceholderConnection() |
| 65 | + |
| 66 | + assert ( |
| 67 | + id(reused) == interrupted_identity, |
| 68 | + connections.take(reused), |
| 69 | + ) == test_case.expected_reuse_and_report |
0 commit comments