2424import warnings
2525from unittest import mock
2626
27+ import anyio
2728import pytest
29+ import sniffio
2830
2931from elastic_transport import (
3032 AiohttpHttpNode ,
4547from tests .conftest import AsyncDummyNode
4648
4749
48- @pytest .mark .asyncio
50+ @pytest .mark .anyio
4951async def test_async_transport_httpbin (httpbin_node_config , httpbin ):
50- t = AsyncTransport ([httpbin_node_config ], meta_header = False )
52+ t = AsyncTransport (
53+ [httpbin_node_config ], meta_header = False , node_class = HttpxAsyncHttpNode
54+ )
5155 resp , data = await t .perform_request ("GET" , "/anything?key=value" )
5256
5357 assert resp .status == 200
@@ -57,6 +61,8 @@ async def test_async_transport_httpbin(httpbin_node_config, httpbin):
5761
5862 data ["headers" ].pop ("X-Amzn-Trace-Id" , None )
5963 assert data ["headers" ] == {
64+ "Accept" : "*/*" ,
65+ "Accept-Encoding" : "gzip, deflate, br" ,
6066 "User-Agent" : DEFAULT_USER_AGENT ,
6167 "Connection" : "keep-alive" ,
6268 "Host" : f"{ httpbin .host } :{ httpbin .port } " ,
@@ -66,15 +72,15 @@ async def test_async_transport_httpbin(httpbin_node_config, httpbin):
6672@pytest .mark .skipif (
6773 sys .version_info < (3 , 8 ), reason = "Mock didn't support async before Python 3.8"
6874)
69- @pytest .mark .asyncio
75+ @pytest .mark .anyio
7076async def test_transport_close_node_pool ():
7177 t = AsyncTransport ([NodeConfig ("http" , "localhost" , 443 )])
7278 with mock .patch .object (t .node_pool .all ()[0 ], "close" ) as node_close :
7379 await t .close ()
7480 node_close .assert_called_with ()
7581
7682
77- @pytest .mark .asyncio
83+ @pytest .mark .anyio
7884async def test_request_with_custom_user_agent_header ():
7985 t = AsyncTransport (
8086 [NodeConfig ("http" , "localhost" , 80 )],
@@ -91,7 +97,7 @@ async def test_request_with_custom_user_agent_header():
9197 } == t .node_pool .get ().calls [0 ][1 ]
9298
9399
94- @pytest .mark .asyncio
100+ @pytest .mark .anyio
95101async def test_body_gets_encoded_into_bytes ():
96102 t = AsyncTransport ([NodeConfig ("http" , "localhost" , 80 )], node_class = AsyncDummyNode )
97103
@@ -105,7 +111,7 @@ async def test_body_gets_encoded_into_bytes():
105111 assert kwargs ["body" ] == b'{"key":"\xe4 \xbd \xa0 \xe5 \xa5 \xbd "}'
106112
107113
108- @pytest .mark .asyncio
114+ @pytest .mark .anyio
109115async def test_body_bytes_get_passed_untouched ():
110116 t = AsyncTransport ([NodeConfig ("http" , "localhost" , 80 )], node_class = AsyncDummyNode )
111117
@@ -131,7 +137,7 @@ def test_kwargs_passed_on_to_node_pool():
131137 assert dt is t .node_pool .max_dead_node_backoff
132138
133139
134- @pytest .mark .asyncio
140+ @pytest .mark .anyio
135141async def test_request_will_fail_after_x_retries ():
136142 t = AsyncTransport (
137143 [
@@ -154,7 +160,7 @@ async def test_request_will_fail_after_x_retries():
154160
155161
156162@pytest .mark .parametrize ("retry_on_timeout" , [True , False ])
157- @pytest .mark .asyncio
163+ @pytest .mark .anyio
158164async def test_retry_on_timeout (retry_on_timeout ):
159165 t = AsyncTransport (
160166 [
@@ -189,7 +195,7 @@ async def test_retry_on_timeout(retry_on_timeout):
189195 assert len (e .value .errors ) == 0
190196
191197
192- @pytest .mark .asyncio
198+ @pytest .mark .anyio
193199async def test_retry_on_status ():
194200 t = AsyncTransport (
195201 [
@@ -233,7 +239,7 @@ async def test_retry_on_status():
233239 ]
234240
235241
236- @pytest .mark .asyncio
242+ @pytest .mark .anyio
237243async def test_failed_connection_will_be_marked_as_dead ():
238244 t = AsyncTransport (
239245 [
@@ -262,7 +268,7 @@ async def test_failed_connection_will_be_marked_as_dead():
262268 assert all (isinstance (error , ConnectionError ) for error in e .value .errors )
263269
264270
265- @pytest .mark .asyncio
271+ @pytest .mark .anyio
266272async def test_resurrected_connection_will_be_marked_as_live_on_success ():
267273 for method in ("GET" , "HEAD" ):
268274 t = AsyncTransport (
@@ -283,7 +289,7 @@ async def test_resurrected_connection_will_be_marked_as_live_on_success():
283289 assert 1 == len (t .node_pool ._dead_nodes .queue )
284290
285291
286- @pytest .mark .asyncio
292+ @pytest .mark .anyio
287293async def test_mark_dead_error_doesnt_raise ():
288294 t = AsyncTransport (
289295 [
@@ -303,7 +309,7 @@ async def test_mark_dead_error_doesnt_raise():
303309 mark_dead .assert_called_with (bad_node )
304310
305311
306- @pytest .mark .asyncio
312+ @pytest .mark .anyio
307313async def test_node_class_as_string ():
308314 t = AsyncTransport ([NodeConfig ("http" , "localhost" , 80 )], node_class = "aiohttp" )
309315 assert isinstance (t .node_pool .get (), AiohttpHttpNode )
@@ -320,7 +326,7 @@ async def test_node_class_as_string():
320326
321327
322328@pytest .mark .parametrize (["status" , "boolean" ], [(200 , True ), (299 , True )])
323- @pytest .mark .asyncio
329+ @pytest .mark .anyio
324330async def test_head_response_true (status , boolean ):
325331 t = AsyncTransport (
326332 [NodeConfig ("http" , "localhost" , 80 , _extras = {"status" : status , "body" : b"" })],
@@ -331,7 +337,7 @@ async def test_head_response_true(status, boolean):
331337 assert data is None
332338
333339
334- @pytest .mark .asyncio
340+ @pytest .mark .anyio
335341async def test_head_response_false ():
336342 t = AsyncTransport (
337343 [NodeConfig ("http" , "localhost" , 80 , _extras = {"status" : 404 , "body" : b"" })],
@@ -353,7 +359,7 @@ async def test_head_response_false():
353359 (HttpxAsyncHttpNode , "hx" ),
354360 ],
355361)
356- @pytest .mark .asyncio
362+ @pytest .mark .anyio
357363async def test_transport_client_meta_node_class (node_class , client_short_name ):
358364 t = AsyncTransport ([NodeConfig ("http" , "localhost" , 80 )], node_class = node_class )
359365 assert (
@@ -366,7 +372,7 @@ async def test_transport_client_meta_node_class(node_class, client_short_name):
366372 )
367373
368374
369- @pytest .mark .asyncio
375+ @pytest .mark .anyio
370376async def test_transport_default_client_meta_node_class ():
371377 # Defaults to aiohttp
372378 t = AsyncTransport (
@@ -635,7 +641,7 @@ async def test_sniff_on_start_no_results_errors(sniff_callback):
635641
636642
637643@pytest .mark .parametrize ("pool_size" , [1 , 8 ])
638- @pytest .mark .asyncio
644+ @pytest .mark .anyio
639645async def test_multiple_tasks_test (pool_size ):
640646 node_configs = [
641647 NodeConfig ("http" , "localhost" , 80 ),
@@ -648,34 +654,43 @@ async def sniff_callback(*_):
648654 await asyncio .sleep (random .random ())
649655 return node_configs
650656
657+ kwargs = {}
658+ if sniffio .current_async_library () == "asyncio" :
659+ kwargs = {
660+ "sniff_on_start" : True ,
661+ "sniff_before_requests" : True ,
662+ "sniff_on_node_failure" : True ,
663+ "sniff_callback" : sniff_callback ,
664+ }
665+
651666 t = AsyncTransport (
652667 node_configs ,
653668 retry_on_status = [500 ],
654669 max_retries = 5 ,
655670 node_class = AsyncDummyNode ,
656- sniff_on_start = True ,
657- sniff_before_requests = True ,
658- sniff_on_node_failure = True ,
659- sniff_callback = sniff_callback ,
671+ ** kwargs ,
660672 )
661673
662- loop = asyncio .get_running_loop ()
663- start = loop .time ()
674+ start = time .monotonic ()
675+
676+ successful_requests = 0
664677
665678 async def run_requests ():
666- successful_requests = 0
667- while loop . time () - start < 2 :
679+ nonlocal successful_requests
680+ while time . monotonic () - start < 2 :
668681 await t .perform_request ("GET" , "/" )
669682 successful_requests += 1
670683 return successful_requests
671684
672- tasks = [loop .create_task (run_requests ()) for _ in range (pool_size * 2 )]
673- assert sum ([await task for task in tasks ]) >= 1000
685+ async with anyio .create_task_group () as tg :
686+ for _ in range (pool_size * 2 ):
687+ tg .start_soon (run_requests )
688+ assert successful_requests >= 1000
674689
675690
676- @pytest .mark .asyncio
691+ @pytest .mark .anyio
677692async def test_httpbin (httpbin_node_config ):
678- t = AsyncTransport ([httpbin_node_config ])
693+ t = AsyncTransport ([httpbin_node_config ], node_class = HttpxAsyncHttpNode )
679694 resp = await t .perform_request ("GET" , "/anything" )
680695 assert resp .meta .status == 200
681696 assert isinstance (resp .body , dict )
0 commit comments