@@ -142,30 +142,28 @@ def server(server_port: int) -> Generator[None, None, None]:
142
142
143
143
yield
144
144
145
- print ("shutting down server gracefully" )
146
- # Try graceful shutdown first
147
- proc .terminate ()
148
- try :
149
- proc .join (timeout = 5 )
150
- except Exception :
151
- print ("Graceful shutdown failed, forcing kill" )
152
- proc .kill ()
153
- proc .join (timeout = 2 )
154
-
145
+ print ("killing server" )
146
+ # Signal the server to stop
147
+ proc .kill ()
148
+ proc .join (timeout = 2 )
155
149
if proc .is_alive ():
156
150
print ("server process failed to terminate" )
157
- proc .kill () # Force kill as last resort
158
151
159
152
153
+ @pytest .fixture ()
154
+ async def http_client (server , server_url ) -> AsyncGenerator [httpx .AsyncClient , None ]:
155
+ """Create test client"""
156
+ async with httpx .AsyncClient (base_url = server_url ) as client :
157
+ yield client
158
+
160
159
160
+ # Tests
161
161
@pytest .mark .anyio
162
- @pytest .mark .skip (
163
- "fails in CI, but works locally. Need to investigate why."
164
- )
165
- async def test_raw_sse_connection (server , server_url ) -> None :
162
+ async def test_raw_sse_connection (http_client : httpx .AsyncClient ) -> None :
166
163
"""Test the SSE connection establishment simply with an HTTP client."""
167
- try :
168
- async with httpx .AsyncClient (base_url = server_url ) as http_client :
164
+ async with anyio .create_task_group ():
165
+
166
+ async def connection_test () -> None :
169
167
async with http_client .stream ("GET" , "/sse" ) as response :
170
168
assert response .status_code == 200
171
169
assert (
@@ -183,11 +181,12 @@ async def test_raw_sse_connection(server, server_url) -> None:
183
181
return
184
182
line_number += 1
185
183
186
- except Exception as e :
187
- pytest .fail (f"{ e } " )
184
+ # Add timeout to prevent test from hanging if it fails
185
+ with anyio .fail_after (3 ):
186
+ await connection_test ()
188
187
189
- @pytest .mark .anyio
190
188
189
+ @pytest .mark .anyio
191
190
async def test_sse_client_basic_connection (server : None , server_url : str ) -> None :
192
191
async with sse_client (server_url + "/sse" ) as streams :
193
192
async with ClientSession (* streams ) as session :
@@ -200,6 +199,7 @@ async def test_sse_client_basic_connection(server: None, server_url: str) -> Non
200
199
ping_result = await session .send_ping ()
201
200
assert isinstance (ping_result , EmptyResult )
202
201
202
+
203
203
@pytest .fixture
204
204
async def initialized_sse_client_session (
205
205
server , server_url : str
@@ -211,9 +211,6 @@ async def initialized_sse_client_session(
211
211
212
212
213
213
@pytest .mark .anyio
214
- @pytest .mark .skip (
215
- "fails in CI, but works locally. Need to investigate why."
216
- )
217
214
async def test_sse_client_happy_request_and_response (
218
215
initialized_sse_client_session : ClientSession ,
219
216
) -> None :
@@ -225,9 +222,6 @@ async def test_sse_client_happy_request_and_response(
225
222
226
223
227
224
@pytest .mark .anyio
228
- @pytest .mark .skip (
229
- "fails in CI, but works locally. Need to investigate why."
230
- )
231
225
async def test_sse_client_exception_handling (
232
226
initialized_sse_client_session : ClientSession ,
233
227
) -> None :
@@ -255,4 +249,4 @@ async def test_sse_client_timeout(
255
249
# we should receive an error here
256
250
return
257
251
258
- pytest .fail ("the client should have timed out and returned an error already" )
252
+ pytest .fail ("the client should have timed out and returned an error already" )
0 commit comments