From 7703cd22eb7bbb5646cac59aed326bae64bfd5d0 Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Tue, 11 Aug 2026 18:03:51 +0900 Subject: [PATCH] test(websocket): wait for server readiness --- .../websocket/tests/test_websocket_channel.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/nanobot/channels/websocket/tests/test_websocket_channel.py b/nanobot/channels/websocket/tests/test_websocket_channel.py index 72da015cb..073aafdf6 100644 --- a/nanobot/channels/websocket/tests/test_websocket_channel.py +++ b/nanobot/channels/websocket/tests/test_websocket_channel.py @@ -129,6 +129,14 @@ def _basic_handler(bus: Any, **kw: Any) -> GatewayServices: ) +async def _connect_when_ready(url: str) -> Any: + while True: + try: + return await websockets.connect(url) + except OSError: + await asyncio.sleep(0.02) + + async def _webui_mutate( client: Any, action: str, @@ -2865,10 +2873,13 @@ async def test_end_to_end_client_receives_ready_and_agent_sees_inbound(bus: Magi channel = _ch(bus, port=port) server_task = asyncio.create_task(channel.start()) - await asyncio.sleep(0.3) try: - async with websockets.connect(f"ws://127.0.0.1:{port}/ws?client_id=tester") as client: + client = await asyncio.wait_for( + _connect_when_ready(f"ws://127.0.0.1:{port}/ws?client_id=tester"), + timeout=5, + ) + async with client: ready_raw = await client.recv() ready = json.loads(ready_raw) assert ready["event"] == "ready"