MCPcopy Index your code
hub / github.com/nonebot/nonebot2 / test_cross_context

Function test_cross_context

tests/test_driver.py:175–220  ·  view source on GitHub ↗
(app: App, driver: Driver)

Source from the content-addressed store, hash-verified

173 indirect=True,
174)
175async def test_cross_context(app: App, driver: Driver):
176 assert isinstance(driver, ASGIMixin)
177
178 ws: WebSocket | None = None
179 ws_ready = anyio.Event()
180 ws_should_close = anyio.Event()
181
182 # create a background task before the ws connection established
183 async def background_task():
184 try:
185 await ws_ready.wait()
186 assert ws is not None
187
188 await ws.send("ping")
189 data = await ws.receive()
190 assert data == "pong"
191 finally:
192 ws_should_close.set()
193
194 async def _handle_ws(websocket: WebSocket) -> None:
195 nonlocal ws
196 await websocket.accept()
197 ws = websocket
198 ws_ready.set()
199
200 await ws_should_close.wait()
201 await websocket.close()
202
203 ws_setup = WebSocketServerSetup(URL("/ws_test"), "ws_test", _handle_ws)
204 driver.setup_websocket_server(ws_setup)
205
206 async with anyio.create_task_group() as tg, app.test_server(driver.asgi) as ctx:
207 tg.start_soon(background_task)
208
209 client = ctx.get_client()
210
211 async with client.websocket_connect("/ws_test") as websocket:
212 try:
213 data = await websocket.receive_text()
214 assert data == "ping"
215 await websocket.send_text("pong")
216 except Exception as e:
217 if not e.args or "websocket.close" not in str(e.args[0]):
218 raise
219
220 await anyio.sleep(1)
221
222
223@pytest.mark.anyio

Callers

nothing calls this directly

Calls 4

receive_textMethod · 0.45
send_textMethod · 0.45

Tested by

no test coverage detected