Skip to content

Commit 443b2ee

Browse files
nikithag19claude
andcommitted
TestCancelPath: run against the real event loop
Cython Future creation calls loop.get_debug(), and _set_nodelay needs a transport-like socket -- so the earlier FakeLoop shape failed under pytest. Swap FakeLoop for the running loop with create_connection / create_unix_connection monkey-patched, and keep the same recorded-branch assertions. All 5 TestCancelPath cases now pass, and the direct-TLS case fails as expected when the fix in _cancel is reverted -- confirming it catches the MagicStack#1211 regression. Co-Authored-By: Claude <[email protected]>
1 parent a223a48 commit 443b2ee

1 file changed

Lines changed: 48 additions & 28 deletions

File tree

‎tests/test_connect.py‎

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,8 +1671,9 @@ def _make_params(self, ssl_ctx, sslmode, ssl_negotiation):
16711671
)
16721672

16731673
async def _run_cancel(self, addr, params):
1674-
# Fake loop that records how the cancel path attempted to connect,
1675-
# then hands back a transport that immediately signals disconnect.
1674+
# Uses the real running loop but stubs out the connection-creation
1675+
# methods so we can observe which branch _cancel took without
1676+
# opening a real socket.
16761677
recorded = {}
16771678

16781679
class FakeTransport:
@@ -1683,32 +1684,31 @@ def close(self):
16831684
recorded['closed'] = True
16841685

16851686
def get_extra_info(self, name):
1686-
# Return a duck-typed socket-like object that satisfies
1687-
# _set_nodelay without hitting a real socket.
1687+
# Duck-typed socket that satisfies _set_nodelay.
16881688
class _S:
16891689
family = socket.AF_INET
16901690
def setsockopt(self, *a, **kw):
16911691
pass
16921692
return _S()
16931693

1694-
class FakeLoop:
1695-
async def create_connection(self, factory, host=None, port=None,
1696-
*, ssl=None):
1697-
recorded['kind'] = 'direct_ssl' if ssl else 'plain'
1698-
recorded['ssl'] = ssl
1699-
recorded['host'] = host
1700-
recorded['port'] = port
1701-
proto = factory()
1702-
# Fulfil on_disconnect so _cancel returns promptly.
1703-
proto.on_disconnect.set_result(True)
1704-
return FakeTransport(), proto
1705-
1706-
async def create_unix_connection(self, factory, path):
1707-
recorded['kind'] = 'unix'
1708-
recorded['path'] = path
1709-
proto = factory()
1710-
proto.on_disconnect.set_result(True)
1711-
return FakeTransport(), proto
1694+
loop = asyncio.get_running_loop()
1695+
1696+
async def fake_create_connection(factory, host=None, port=None,
1697+
*, ssl=None, **kw):
1698+
recorded['kind'] = 'direct_ssl' if ssl else 'plain'
1699+
recorded['ssl'] = ssl
1700+
recorded['host'] = host
1701+
recorded['port'] = port
1702+
proto = factory()
1703+
proto.on_disconnect.set_result(True)
1704+
return FakeTransport(), proto
1705+
1706+
async def fake_create_unix_connection(factory, path, **kw):
1707+
recorded['kind'] = 'unix'
1708+
recorded['path'] = path
1709+
proto = factory()
1710+
proto.on_disconnect.set_result(True)
1711+
return FakeTransport(), proto
17121712

17131713
async def fake_ssl_conn(protocol_factory, host, port, *,
17141714
loop, ssl_context, ssl_is_advisory=False):
@@ -1720,12 +1720,32 @@ async def fake_ssl_conn(protocol_factory, host, port, *,
17201720
proto.on_disconnect.set_result(True)
17211721
return FakeTransport(), proto
17221722

1723-
with unittest.mock.patch.object(
1724-
connect_utils, '_create_ssl_connection', fake_ssl_conn
1725-
):
1726-
await connect_utils._cancel(
1727-
loop=FakeLoop(), addr=addr, params=params,
1728-
backend_pid=1, backend_secret=2)
1723+
orig_cc = loop.create_connection
1724+
orig_cuc = loop.create_unix_connection if hasattr(
1725+
loop, 'create_unix_connection') else None
1726+
loop.create_connection = fake_create_connection
1727+
if orig_cuc is not None:
1728+
loop.create_unix_connection = fake_create_unix_connection
1729+
else:
1730+
# Windows loops lack create_unix_connection; add a stub so
1731+
# UNIX-socket branch can still be tested.
1732+
loop.create_unix_connection = fake_create_unix_connection
1733+
try:
1734+
with unittest.mock.patch.object(
1735+
connect_utils, '_create_ssl_connection', fake_ssl_conn
1736+
):
1737+
await connect_utils._cancel(
1738+
loop=loop, addr=addr, params=params,
1739+
backend_pid=1, backend_secret=2)
1740+
finally:
1741+
loop.create_connection = orig_cc
1742+
if orig_cuc is not None:
1743+
loop.create_unix_connection = orig_cuc
1744+
else:
1745+
try:
1746+
del loop.create_unix_connection
1747+
except AttributeError:
1748+
pass
17291749
return recorded
17301750

17311751
async def test_cancel_direct_tls_uses_direct_transport(self):

0 commit comments

Comments
 (0)