@@ -252,3 +252,69 @@ async def test_sse_client_timeout(
252
252
return
253
253
254
254
pytest .fail ("the client should have timed out and returned an error already" )
255
+
256
+
257
+ def run_mounted_server (server_port : int ) -> None :
258
+ app = make_server_app ()
259
+ main_app = Starlette (routes = [Mount ("/mounted_app" , app = app )])
260
+ server = uvicorn .Server (
261
+ config = uvicorn .Config (
262
+ app = main_app , host = "127.0.0.1" , port = server_port , log_level = "error"
263
+ )
264
+ )
265
+ print (f"starting server on { server_port } " )
266
+ server .run ()
267
+
268
+ # Give server time to start
269
+ while not server .started :
270
+ print ("waiting for server to start" )
271
+ time .sleep (0.5 )
272
+
273
+
274
+ @pytest .fixture ()
275
+ def mounted_server (server_port : int ) -> Generator [None , None , None ]:
276
+ proc = multiprocessing .Process (
277
+ target = run_mounted_server , kwargs = {"server_port" : server_port }, daemon = True
278
+ )
279
+ print ("starting process" )
280
+ proc .start ()
281
+
282
+ # Wait for server to be running
283
+ max_attempts = 20
284
+ attempt = 0
285
+ print ("waiting for server to start" )
286
+ while attempt < max_attempts :
287
+ try :
288
+ with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
289
+ s .connect (("127.0.0.1" , server_port ))
290
+ break
291
+ except ConnectionRefusedError :
292
+ time .sleep (0.1 )
293
+ attempt += 1
294
+ else :
295
+ raise RuntimeError (f"Server failed to start after { max_attempts } attempts" )
296
+
297
+ yield
298
+
299
+ print ("killing server" )
300
+ # Signal the server to stop
301
+ proc .kill ()
302
+ proc .join (timeout = 2 )
303
+ if proc .is_alive ():
304
+ print ("server process failed to terminate" )
305
+
306
+
307
+ @pytest .mark .anyio
308
+ async def test_sse_client_basic_connection_mounted_app (
309
+ mounted_server : None , server_url : str
310
+ ) -> None :
311
+ async with sse_client (server_url + "/mounted_app/sse" ) as streams :
312
+ async with ClientSession (* streams ) as session :
313
+ # Test initialization
314
+ result = await session .initialize ()
315
+ assert isinstance (result , InitializeResult )
316
+ assert result .serverInfo .name == SERVER_NAME
317
+
318
+ # Test ping
319
+ ping_result = await session .send_ping ()
320
+ assert isinstance (ping_result , EmptyResult )
0 commit comments