77import asyncio
88import importlib
99import threading
10- from types import SimpleNamespace
1110from typing import Any , Iterator , AsyncIterator
1211from contextlib import aclosing , nullcontext
1312from http .server import ThreadingHTTPServer , BaseHTTPRequestHandler
@@ -36,7 +35,7 @@ def http_module(request: pytest.FixtureRequest) -> Any:
3635 return importlib .import_module (request .param )
3736
3837
39- @pytest .mark .parametrize ("mode" , ["sync" , " async" , "aiohttp" ])
38+ @pytest .mark .parametrize ("mode" , ["async" , "aiohttp" ])
4039@pytest .mark .parametrize ("ending" , ["complete" , "truncate" , "stall" , "trickle" ])
4140async def test_done_http11_connection_reuse (mode : str , ending : str , http_module : Any ) -> None :
4241 if mode == "aiohttp" and http_module .__name__ == "httpx" :
@@ -68,10 +67,6 @@ def do_POST(self) -> None:
6867 b'"choices":[{"index":0,"delta":{"content":"hello"}}]}\n \n data: [DONE]\n \n '
6968 )
7069 try :
71- if requests == 2 and ending == "complete" :
72- # A cancelled watchdog must not interrupt the next request
73- # after the connection has been returned to the pool.
74- release .wait (timeout = 0.15 )
7570 self .wfile .write (f"{ len (payload ):x} \r \n " .encode () + payload + b"\r \n " )
7671 if requests == 1 :
7772 if ending == "truncate" :
@@ -108,30 +103,20 @@ def log_message(self, format: str, *args: object) -> None: # noqa: A002
108103 }
109104 transport_options : dict [str , Any ] = {"trust_env" : False , "limits" : http_module .Limits (max_connections = 1 )}
110105 try :
111- if mode == "sync" :
112- with OpenAI (** options , http_client = http_module .Client (** transport_options )) as client :
113- for _ in range (2 ):
114- with client .chat .completions .create (model = "synthetic" , messages = [], stream = True ) as stream :
115- assert next (stream ).choices [0 ].delta .content == "hello"
116- start = time .monotonic ()
117- assert list (stream ) == []
118- assert time .monotonic () - start < 0.4
119- assert stream .response .request .extensions ["timeout" ]["read" ] is None
120- else :
121- http_client = (
122- DefaultAioHttpClient (** transport_options )
123- if mode == "aiohttp"
124- else http_module .AsyncClient (** transport_options )
125- )
126- async with AsyncOpenAI (** options , http_client = http_client ) as async_client :
127- for _ in range (2 ):
128- async with await async_client .chat .completions .create (
129- model = "synthetic" , messages = [], stream = True
130- ) as async_stream :
131- assert (await async_stream .__anext__ ()).choices [0 ].delta .content == "hello"
132- start = time .monotonic ()
133- assert [chunk async for chunk in async_stream ] == []
134- assert time .monotonic () - start < 0.4
106+ http_client = (
107+ DefaultAioHttpClient (** transport_options )
108+ if mode == "aiohttp"
109+ else http_module .AsyncClient (** transport_options )
110+ )
111+ async with AsyncOpenAI (** options , http_client = http_client ) as async_client :
112+ for _ in range (2 ):
113+ async with await async_client .chat .completions .create (
114+ model = "synthetic" , messages = [], stream = True
115+ ) as async_stream :
116+ assert (await async_stream .__anext__ ()).choices [0 ].delta .content == "hello"
117+ start = time .monotonic ()
118+ assert [chunk async for chunk in async_stream ] == []
119+ assert time .monotonic () - start < 0.4
135120 finally :
136121 release .set ()
137122 server .shutdown ()
@@ -146,7 +131,7 @@ def log_message(self, format: str, *args: object) -> None: # noqa: A002
146131
147132@pytest .mark .parametrize ("sync" , [True , False ], ids = ["sync" , "async" ])
148133@pytest .mark .parametrize ("http_version" , [b"HTTP/1.1" , b"HTTP/2" ])
149- async def test_done_discards_bytes_only_for_http11 (
134+ async def test_done_discards_bytes_only_for_async_http11 (
150135 sync : bool , http_version : bytes , http_module : Any , client : OpenAI , async_client : AsyncOpenAI
151136) -> None :
152137 read_tail = False
@@ -167,26 +152,13 @@ def __iter__(self) -> Iterator[bytes]:
167152 def __aiter__ (self ) -> AsyncIterator [bytes ]:
168153 return async_iterator
169154
170- with socket .socket () as sock :
171-
172- def socket_info (_name : str ) -> socket .socket :
173- return sock
174-
175- response = http_module .Response (
176- 200 ,
177- stream = Body (),
178- request = http_module .Request ("GET" , "http://example.test" ),
179- extensions = {
180- "http_version" : http_version ,
181- "network_stream" : SimpleNamespace (get_extra_info = socket_info ),
182- },
183- )
184- if sync :
185- assert list (Stream (cast_to = object , response = response , client = client )) == []
186- else :
187- assert [event async for event in AsyncStream (cast_to = object , response = response , client = async_client )] == []
155+ response = http_module .Response (200 , stream = Body (), extensions = {"http_version" : http_version })
156+ if sync :
157+ assert list (Stream (cast_to = object , response = response , client = client )) == []
158+ else :
159+ assert [event async for event in AsyncStream (cast_to = object , response = response , client = async_client )] == []
188160 assert response .is_closed
189- assert read_tail == (http_version == b"HTTP/1.1" )
161+ assert read_tail == (not sync and http_version == b"HTTP/1.1" )
190162
191163
192164@pytest .mark .parametrize ("cancel" , [True , False ], ids = ["caller-cancel" , "deadline" ])
0 commit comments