@@ -79,6 +79,15 @@ def populate_metrics(server, request):
7979 SCOPED_METRICS .append (("External/localhost:%d/httpx/%s" % (server .port , method ), 2 ))
8080
8181
82+ def exercise_sync_client (server , client , method ):
83+ with client as client :
84+ resolved_method = getattr (client , method )
85+ resolved_method ("http://localhost:%s" % server .port )
86+ response = resolved_method ("http://localhost:%s" % server .port )
87+
88+ return response
89+
90+
8291@pytest .mark .parametrize (
8392 "method" ,
8493 (
@@ -102,12 +111,18 @@ def test_sync_client(httpx, server, method):
102111 global CAT_RESPONSE_CODE
103112 CAT_RESPONSE_CODE = 200
104113
105- with httpx .Client () as client :
114+ assert exercise_sync_client (server , httpx .Client (), method ).status_code == 200
115+
116+
117+ async def exercise_async_client (server , client , method ):
118+ async with client as client :
106119 resolved_method = getattr (client , method )
107- resolved_method ("http://localhost:%s" % server .port )
108- response = resolved_method ("http://localhost:%s" % server .port )
120+ responses = await asyncio .gather (
121+ resolved_method ("http://localhost:%s" % server .port ),
122+ resolved_method ("http://localhost:%s" % server .port ),
123+ )
109124
110- assert response . status_code == 200
125+ return responses
111126
112127
113128@pytest .mark .parametrize (
@@ -133,17 +148,7 @@ def test_async_client(httpx, server, loop, method):
133148 global CAT_RESPONSE_CODE
134149 CAT_RESPONSE_CODE = 200
135150
136- async def test_async_client ():
137- async with httpx .AsyncClient () as client :
138- resolved_method = getattr (client , method )
139- responses = await asyncio .gather (
140- resolved_method ("http://localhost:%s" % server .port ),
141- resolved_method ("http://localhost:%s" % server .port ),
142- )
143-
144- return responses
145-
146- responses = loop .run_until_complete (test_async_client ())
151+ responses = loop .run_until_complete (exercise_async_client (server , httpx .AsyncClient (), method ))
147152 assert all (response .status_code == 200 for response in responses )
148153
149154
@@ -474,4 +479,36 @@ async def _test():
474479 trace = current_trace ()
475480 response = loop .run_until_complete (_test ())
476481 assert response .status_code == 200
477- assert trace is None
482+ assert trace is None
483+
484+
485+ @pytest .mark .parametrize ('client' , (
486+ 'Client' ,
487+ 'AsyncClient' ,
488+ ))
489+ def test_invalid_import_order_client (monkeypatch , httpx , server , loop , client ):
490+ global CAT_RESPONSE_CODE
491+ CAT_RESPONSE_CODE = 200
492+
493+ if 'Async' in client :
494+ is_async = True
495+ else :
496+ is_async = False
497+
498+ client = getattr (httpx , client )
499+
500+ # Force the client class into the state as if instrumentation had not run
501+ monkeypatch .setattr (client , '_event_hooks' , None )
502+
503+ # Instantiate a client
504+ client = client ()
505+
506+ # Remove monkeypatching of _event_hooks to restore instrumentation
507+ monkeypatch .undo ()
508+
509+ if is_async :
510+ responses = loop .run_until_complete (exercise_async_client (server , client , "get" ))
511+ assert all (response .status_code == 200 for response in responses )
512+ else :
513+ response = exercise_sync_client (server , client , "get" )
514+ assert response .status_code == 200
0 commit comments