Skip to content

Commit 23bf026

Browse files
Brutus5000claude
andcommitted
refactor(tests): bound the connection-registration wait
Address CodeRabbit feedback: replace `while not ctx.connections` spin loops with a helper that polls up to a fixed iteration count and raises AssertionError on exhaustion, so tests fail fast instead of hanging if the server never registers the connection. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent af4f67c commit 23bf026

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

tests/integration_tests/test_servercontext.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
from tests.utils import exhaust_callbacks, fast_forward
1313

1414

15+
async def wait_for_connection_registered(ctx, max_iters=1000):
16+
for _ in range(max_iters):
17+
if ctx.connections:
18+
return
19+
await asyncio.sleep(0)
20+
raise AssertionError(
21+
"Server did not register the connection within the allotted iterations"
22+
)
23+
24+
1525
class MockConnection:
1626
def __init__(self):
1727
self.protocol = None
@@ -91,8 +101,7 @@ async def test_connection_broken_external(context):
91101
"""
92102
srv, ctx = context
93103
_, writer = await asyncio.open_connection(*srv.sockets[0].getsockname())
94-
while not ctx.connections:
95-
await asyncio.sleep(0)
104+
await wait_for_connection_registered(ctx)
96105
writer.close()
97106
# Need this sleep for test to work, otherwise closed protocol isn't detected
98107
await asyncio.sleep(0)
@@ -147,8 +156,7 @@ async def test_unexpected_exception_in_connection_lost(context, caplog):
147156
async def test_drain_connections(context):
148157
srv, ctx = context
149158
_, writer = await asyncio.open_connection(*srv.sockets[0].getsockname())
150-
while not ctx.connections:
151-
await asyncio.sleep(0)
159+
await wait_for_connection_registered(ctx)
152160

153161
with pytest.raises(asyncio.TimeoutError):
154162
await asyncio.wait_for(

0 commit comments

Comments
 (0)