44Expects pytest-aiohttp
55"""
66import asyncio
7+ import sys
78from aws_xray_sdk import global_sdk_config
89try :
910 from unittest .mock import patch
@@ -84,7 +85,10 @@ async def handle_delay(self, request: web.Request) -> web.Response:
8485 """
8586 Handle /delay request
8687 """
87- await asyncio .sleep (0.3 , loop = self ._loop )
88+ if sys .version_info >= (3 , 8 ):
89+ await asyncio .sleep (0.3 )
90+ else :
91+ await asyncio .sleep (0.3 , loop = self ._loop )
8892 return web .Response (text = "ok" )
8993
9094 def get_app (self ) -> web .Application :
@@ -120,15 +124,15 @@ def recorder(loop):
120124 patcher .stop ()
121125
122126
123- async def test_ok (test_client , loop , recorder ):
127+ async def test_ok (aiohttp_client , loop , recorder ):
124128 """
125129 Test a normal response
126130
127- :param test_client : AioHttp test client fixture
131+ :param aiohttp_client : AioHttp test client fixture
128132 :param loop: Eventloop fixture
129133 :param recorder: X-Ray recorder fixture
130134 """
131- client = await test_client (ServerTest .app (loop = loop ))
135+ client = await aiohttp_client (ServerTest .app (loop = loop ))
132136
133137 resp = await client .get ('/' )
134138 assert resp .status == 200
@@ -144,15 +148,15 @@ async def test_ok(test_client, loop, recorder):
144148 assert response ['status' ] == 200
145149
146150
147- async def test_ok_x_forwarded_for (test_client , loop , recorder ):
151+ async def test_ok_x_forwarded_for (aiohttp_client , loop , recorder ):
148152 """
149153 Test a normal response with x_forwarded_for headers
150154
151- :param test_client : AioHttp test client fixture
155+ :param aiohttp_client : AioHttp test client fixture
152156 :param loop: Eventloop fixture
153157 :param recorder: X-Ray recorder fixture
154158 """
155- client = await test_client (ServerTest .app (loop = loop ))
159+ client = await aiohttp_client (ServerTest .app (loop = loop ))
156160
157161 resp = await client .get ('/' , headers = {'X-Forwarded-For' : 'foo' })
158162 assert resp .status == 200
@@ -162,15 +166,15 @@ async def test_ok_x_forwarded_for(test_client, loop, recorder):
162166 assert segment .http ['request' ]['x_forwarded_for' ]
163167
164168
165- async def test_ok_content_length (test_client , loop , recorder ):
169+ async def test_ok_content_length (aiohttp_client , loop , recorder ):
166170 """
167171 Test a normal response with content length as response header
168172
169- :param test_client : AioHttp test client fixture
173+ :param aiohttp_client : AioHttp test client fixture
170174 :param loop: Eventloop fixture
171175 :param recorder: X-Ray recorder fixture
172176 """
173- client = await test_client (ServerTest .app (loop = loop ))
177+ client = await aiohttp_client (ServerTest .app (loop = loop ))
174178
175179 resp = await client .get ('/?content_length=100' )
176180 assert resp .status == 200
@@ -179,15 +183,15 @@ async def test_ok_content_length(test_client, loop, recorder):
179183 assert segment .http ['response' ]['content_length' ] == 100
180184
181185
182- async def test_error (test_client , loop , recorder ):
186+ async def test_error (aiohttp_client , loop , recorder ):
183187 """
184188 Test a 4XX response
185189
186- :param test_client : AioHttp test client fixture
190+ :param aiohttp_client : AioHttp test client fixture
187191 :param loop: Eventloop fixture
188192 :param recorder: X-Ray recorder fixture
189193 """
190- client = await test_client (ServerTest .app (loop = loop ))
194+ client = await aiohttp_client (ServerTest .app (loop = loop ))
191195
192196 resp = await client .get ('/error' )
193197 assert resp .status == 404
@@ -204,15 +208,15 @@ async def test_error(test_client, loop, recorder):
204208 assert response ['status' ] == 404
205209
206210
207- async def test_exception (test_client , loop , recorder ):
211+ async def test_exception (aiohttp_client , loop , recorder ):
208212 """
209213 Test handling an exception
210214
211- :param test_client : AioHttp test client fixture
215+ :param aiohttp_client : AioHttp test client fixture
212216 :param loop: Eventloop fixture
213217 :param recorder: X-Ray recorder fixture
214218 """
215- client = await test_client (ServerTest .app (loop = loop ))
219+ client = await aiohttp_client (ServerTest .app (loop = loop ))
216220
217221 with pytest .raises (Exception ):
218222 await client .get ('/exception' )
@@ -231,15 +235,15 @@ async def test_exception(test_client, loop, recorder):
231235 assert exception .type == 'CancelledError'
232236
233237
234- async def test_unhauthorized (test_client , loop , recorder ):
238+ async def test_unhauthorized (aiohttp_client , loop , recorder ):
235239 """
236240 Test a 401 response
237241
238- :param test_client : AioHttp test client fixture
242+ :param aiohttp_client : AioHttp test client fixture
239243 :param loop: Eventloop fixture
240244 :param recorder: X-Ray recorder fixture
241245 """
242- client = await test_client (ServerTest .app (loop = loop ))
246+ client = await aiohttp_client (ServerTest .app (loop = loop ))
243247
244248 resp = await client .get ('/unauthorized' )
245249 assert resp .status == 401
@@ -256,8 +260,8 @@ async def test_unhauthorized(test_client, loop, recorder):
256260 assert response ['status' ] == 401
257261
258262
259- async def test_response_trace_header (test_client , loop , recorder ):
260- client = await test_client (ServerTest .app (loop = loop ))
263+ async def test_response_trace_header (aiohttp_client , loop , recorder ):
264+ client = await aiohttp_client (ServerTest .app (loop = loop ))
261265 resp = await client .get ('/' )
262266 xray_header = resp .headers [http .XRAY_HEADER ]
263267 segment = recorder .emitter .pop ()
@@ -266,42 +270,42 @@ async def test_response_trace_header(test_client, loop, recorder):
266270 assert expected in xray_header
267271
268272
269- async def test_concurrent (test_client , loop , recorder ):
273+ async def test_concurrent (aiohttp_client , loop , recorder ):
270274 """
271275 Test multiple concurrent requests
272276
273- :param test_client : AioHttp test client fixture
277+ :param aiohttp_client : AioHttp test client fixture
274278 :param loop: Eventloop fixture
275279 :param recorder: X-Ray recorder fixture
276280 """
277- client = await test_client (ServerTest .app (loop = loop ))
281+ client = await aiohttp_client (ServerTest .app (loop = loop ))
278282
279283 recorder .emitter = CustomStubbedEmitter ()
280284
281285 async def get_delay ():
282286 resp = await client .get ('/delay' )
283287 assert resp .status == 200
284288
285- await asyncio . wait ([ get_delay (), get_delay (), get_delay (),
286- get_delay (), get_delay (), get_delay (),
287- get_delay (), get_delay (), get_delay ()],
288- loop = loop )
289+ if sys . version_info >= ( 3 , 8 ):
290+ await asyncio . wait ([ loop . create_task ( get_delay ()) for i in range ( 9 )])
291+ else :
292+ await asyncio . wait ([ loop . create_task ( get_delay ()) for i in range ( 9 )], loop = loop )
289293
290294 # Ensure all ID's are different
291295 ids = [item .id for item in recorder .emitter .local ]
292296 assert len (ids ) == len (set (ids ))
293297
294298
295- async def test_disabled_sdk (test_client , loop , recorder ):
299+ async def test_disabled_sdk (aiohttp_client , loop , recorder ):
296300 """
297301 Test a normal response when the SDK is disabled.
298302
299- :param test_client : AioHttp test client fixture
303+ :param aiohttp_client : AioHttp test client fixture
300304 :param loop: Eventloop fixture
301305 :param recorder: X-Ray recorder fixture
302306 """
303307 global_sdk_config .set_sdk_enabled (False )
304- client = await test_client (ServerTest .app (loop = loop ))
308+ client = await aiohttp_client (ServerTest .app (loop = loop ))
305309
306310 resp = await client .get ('/' )
307311 assert resp .status == 200
0 commit comments