@@ -131,32 +131,36 @@ async def server_and_redis(redis_server_and_app, server_port: int):
131131 app = app , host = "127.0.0.1" , port = server_port , log_level = "error"
132132 )
133133 server = uvicorn .Server (config = config )
134- AppStatus .should_exit = False
135- AppStatus .should_exit_event = None
136- # Run server in a task group
137- async with anyio .create_task_group () as tg :
138- # Start server in background
139- tg .start_soon (server .serve )
140-
141- # Wait for server to be ready
142- max_attempts = 20
143- attempt = 0
144- while attempt < max_attempts :
134+ try :
135+ async with anyio .create_task_group () as tg :
136+ # Start server in background
137+ tg .start_soon (server .serve )
138+
139+ # Wait for server to be ready
140+ max_attempts = 20
141+ attempt = 0
142+ while attempt < max_attempts :
143+ try :
144+ with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
145+ s .connect (("127.0.0.1" , server_port ))
146+ break
147+ except ConnectionRefusedError :
148+ await anyio .sleep (0.1 )
149+ attempt += 1
150+ else :
151+ raise RuntimeError (f"Server failed to start after { max_attempts } attempts" )
152+
153+ # Yield Redis for tests
145154 try :
146- with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
147- s .connect (("127.0.0.1" , server_port ))
148- break
149- except ConnectionRefusedError :
150- await anyio .sleep (0.1 )
151- attempt += 1
152- else :
153- raise RuntimeError (f"Server failed to start after { max_attempts } attempts" )
154-
155- # Yield Redis for tests
156- try :
157- yield mock_redis , message_dispatch
158- finally :
159- server .should_exit = True
155+ yield mock_redis , message_dispatch
156+ finally :
157+ server .should_exit = True
158+ finally :
159+ # These class variables are set top-level in starlette-sse
160+ # It isn't designed to be run multiple times in a single
161+ # Python process so we need to manually reset them.
162+ AppStatus .should_exit = False
163+ AppStatus .should_exit_event = None
160164
161165
162166@pytest .fixture ()
0 commit comments