11import asyncio
22from concurrent .futures import ThreadPoolExecutor
3+ from typing import Any
34
45import pytest
56
@@ -27,6 +28,10 @@ def run_in_worker_thread(fn):
2728 return executor .submit (fn ).result ()
2829
2930
31+ def transport_uses_http2 (transport : Any ) -> bool :
32+ return bool (getattr (transport ._pool , "_http2" ))
33+
34+
3035def test_sync_api_client_proxy_uses_explicit_transport (test_api_key ):
3136 reset_sync_api_transports ()
3237 config = ConnectionConfig (
@@ -39,32 +44,47 @@ def test_sync_api_client_proxy_uses_explicit_transport(test_api_key):
3944
4045 try :
4146 assert "proxy" not in api_client ._httpx_args
42- assert httpx_client ._transport is get_sync_transport (config )
47+ assert httpx_client ._transport is get_sync_transport (config , http2 = False )
4348 assert httpx_client ._mounts == {}
4449 finally :
4550 httpx_client .close ()
4651 reset_sync_api_transports ()
4752
4853
49- def test_sync_get_transport_http2_opt_out_returns_distinct_instance (test_api_key ):
54+ def test_sync_get_transport_http2_opt_in_returns_distinct_instance (test_api_key ):
5055 reset_sync_api_transports ()
5156 config = ConnectionConfig (api_key = test_api_key )
5257
5358 try :
54- http2_transport = get_sync_transport (config )
55- http1_transport = get_sync_transport (config , http2 = False )
59+ http1_transport = get_sync_transport (config )
60+ http2_transport = get_sync_transport (config , http2 = True )
5661
57- assert http2_transport is not http1_transport
58- assert http2_transport . _pool . _http2 is True
59- assert http1_transport . _pool . _http2 is False
62+ assert transport_uses_http2 ( http1_transport ) is False
63+ assert transport_uses_http2 ( http2_transport ) is True
64+ assert http1_transport is not http2_transport
6065 # Subsequent calls with the same http2 flag return the cached
6166 # instance.
62- assert get_sync_transport (config ) is http2_transport
63- assert get_sync_transport (config , http2 = False ) is http1_transport
67+ assert get_sync_transport (config ) is http1_transport
68+ assert get_sync_transport (config , http2 = True ) is http2_transport
6469 finally :
6570 reset_sync_api_transports ()
6671
6772
73+ def test_sync_api_client_respects_connection_config_http2 (test_api_key ):
74+ reset_sync_api_transports ()
75+ config = ConnectionConfig (api_key = test_api_key , http2 = True )
76+
77+ api_client = get_sync_api_client (config )
78+ httpx_client = api_client .get_httpx_client ()
79+
80+ try :
81+ assert httpx_client ._transport is get_sync_transport (config , http2 = True )
82+ assert transport_uses_http2 (httpx_client ._transport ) is True
83+ finally :
84+ httpx_client .close ()
85+ reset_sync_api_transports ()
86+
87+
6888def test_sync_envd_transport_uses_separate_cache (test_api_key ):
6989 reset_sync_api_transports ()
7090 reset_sync_envd_transports ()
@@ -77,7 +97,7 @@ def test_sync_envd_transport_uses_separate_cache(test_api_key):
7797 assert api_transport is not envd_transport
7898 assert get_sync_transport (config ) is api_transport
7999 assert get_sync_envd_transport (config ) is envd_transport
80- assert envd_transport . _pool . _http2 is True
100+ assert transport_uses_http2 ( envd_transport ) is False
81101 finally :
82102 reset_sync_api_transports ()
83103 reset_sync_envd_transports ()
@@ -112,8 +132,8 @@ def test_sync_envd_transport_cache_is_thread_local(test_api_key):
112132
113133 assert main_transport is get_sync_envd_transport (config )
114134 assert thread_transport is not main_transport
115- assert main_transport . _pool . _http2 is True
116- assert thread_transport . _pool . _http2 is True
135+ assert transport_uses_http2 ( main_transport ) is False
136+ assert transport_uses_http2 ( thread_transport ) is False
117137 finally :
118138 reset_sync_envd_transports ()
119139
@@ -129,7 +149,7 @@ async def test_async_api_client_proxy_uses_explicit_transport(test_api_key):
129149 api_client = get_async_api_client (config )
130150 httpx_client = api_client .get_async_httpx_client ()
131151 transport = AsyncTransportWithLogger ._instances [
132- (id (asyncio .get_running_loop ()), True )
152+ (id (asyncio .get_running_loop ()), False )
133153 ]
134154
135155 try :
@@ -142,27 +162,43 @@ async def test_async_api_client_proxy_uses_explicit_transport(test_api_key):
142162
143163
144164@pytest .mark .asyncio
145- async def test_async_get_transport_http2_opt_out_returns_distinct_instance (
165+ async def test_async_get_transport_http2_opt_in_returns_distinct_instance (
146166 test_api_key ,
147167):
148168 AsyncTransportWithLogger ._instances .clear ()
149169 config = ConnectionConfig (api_key = test_api_key )
150170
151171 try :
152- http2_transport = get_async_transport (config )
153- http1_transport = get_async_transport (config , http2 = False )
172+ http1_transport = get_async_transport (config )
173+ http2_transport = get_async_transport (config , http2 = True )
154174
155- assert http2_transport is not http1_transport
156- assert http2_transport . _pool . _http2 is True
157- assert http1_transport . _pool . _http2 is False
175+ assert transport_uses_http2 ( http1_transport ) is False
176+ assert transport_uses_http2 ( http2_transport ) is True
177+ assert http1_transport is not http2_transport
158178 # Subsequent calls with the same http2 flag return the cached
159179 # instance.
160- assert get_async_transport (config ) is http2_transport
161- assert get_async_transport (config , http2 = False ) is http1_transport
180+ assert get_async_transport (config ) is http1_transport
181+ assert get_async_transport (config , http2 = True ) is http2_transport
162182 finally :
163183 AsyncTransportWithLogger ._instances .clear ()
164184
165185
186+ @pytest .mark .asyncio
187+ async def test_async_api_client_respects_connection_config_http2 (test_api_key ):
188+ AsyncTransportWithLogger ._instances .clear ()
189+ config = ConnectionConfig (api_key = test_api_key , http2 = True )
190+
191+ api_client = get_async_api_client (config )
192+ httpx_client = api_client .get_async_httpx_client ()
193+
194+ try :
195+ assert httpx_client ._transport is get_async_transport (config , http2 = True )
196+ assert transport_uses_http2 (httpx_client ._transport ) is True
197+ finally :
198+ await httpx_client .aclose ()
199+ AsyncTransportWithLogger ._instances .clear ()
200+
201+
166202@pytest .mark .asyncio
167203async def test_async_envd_transport_uses_separate_cache (test_api_key ):
168204 AsyncTransportWithLogger ._instances .clear ()
@@ -176,7 +212,7 @@ async def test_async_envd_transport_uses_separate_cache(test_api_key):
176212 assert api_transport is not envd_transport
177213 assert get_async_transport (config ) is api_transport
178214 assert get_async_envd_transport (config ) is envd_transport
179- assert envd_transport . _pool . _http2 is True
215+ assert transport_uses_http2 ( envd_transport ) is False
180216 finally :
181217 AsyncTransportWithLogger ._instances .clear ()
182218 AsyncEnvdTransportWithLogger ._instances .clear ()
0 commit comments