4
4
Expects pytest-aiohttp
5
5
"""
6
6
import asyncio
7
+ import sys
7
8
from aws_xray_sdk import global_sdk_config
8
9
try :
9
10
from unittest .mock import patch
@@ -84,7 +85,10 @@ async def handle_delay(self, request: web.Request) -> web.Response:
84
85
"""
85
86
Handle /delay request
86
87
"""
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 )
88
92
return web .Response (text = "ok" )
89
93
90
94
def get_app (self ) -> web .Application :
@@ -120,15 +124,15 @@ def recorder(loop):
120
124
patcher .stop ()
121
125
122
126
123
- async def test_ok (test_client , loop , recorder ):
127
+ async def test_ok (aiohttp_client , loop , recorder ):
124
128
"""
125
129
Test a normal response
126
130
127
- :param test_client : AioHttp test client fixture
131
+ :param aiohttp_client : AioHttp test client fixture
128
132
:param loop: Eventloop fixture
129
133
:param recorder: X-Ray recorder fixture
130
134
"""
131
- client = await test_client (ServerTest .app (loop = loop ))
135
+ client = await aiohttp_client (ServerTest .app (loop = loop ))
132
136
133
137
resp = await client .get ('/' )
134
138
assert resp .status == 200
@@ -144,15 +148,15 @@ async def test_ok(test_client, loop, recorder):
144
148
assert response ['status' ] == 200
145
149
146
150
147
- async def test_ok_x_forwarded_for (test_client , loop , recorder ):
151
+ async def test_ok_x_forwarded_for (aiohttp_client , loop , recorder ):
148
152
"""
149
153
Test a normal response with x_forwarded_for headers
150
154
151
- :param test_client : AioHttp test client fixture
155
+ :param aiohttp_client : AioHttp test client fixture
152
156
:param loop: Eventloop fixture
153
157
:param recorder: X-Ray recorder fixture
154
158
"""
155
- client = await test_client (ServerTest .app (loop = loop ))
159
+ client = await aiohttp_client (ServerTest .app (loop = loop ))
156
160
157
161
resp = await client .get ('/' , headers = {'X-Forwarded-For' : 'foo' })
158
162
assert resp .status == 200
@@ -162,15 +166,15 @@ async def test_ok_x_forwarded_for(test_client, loop, recorder):
162
166
assert segment .http ['request' ]['x_forwarded_for' ]
163
167
164
168
165
- async def test_ok_content_length (test_client , loop , recorder ):
169
+ async def test_ok_content_length (aiohttp_client , loop , recorder ):
166
170
"""
167
171
Test a normal response with content length as response header
168
172
169
- :param test_client : AioHttp test client fixture
173
+ :param aiohttp_client : AioHttp test client fixture
170
174
:param loop: Eventloop fixture
171
175
:param recorder: X-Ray recorder fixture
172
176
"""
173
- client = await test_client (ServerTest .app (loop = loop ))
177
+ client = await aiohttp_client (ServerTest .app (loop = loop ))
174
178
175
179
resp = await client .get ('/?content_length=100' )
176
180
assert resp .status == 200
@@ -179,15 +183,15 @@ async def test_ok_content_length(test_client, loop, recorder):
179
183
assert segment .http ['response' ]['content_length' ] == 100
180
184
181
185
182
- async def test_error (test_client , loop , recorder ):
186
+ async def test_error (aiohttp_client , loop , recorder ):
183
187
"""
184
188
Test a 4XX response
185
189
186
- :param test_client : AioHttp test client fixture
190
+ :param aiohttp_client : AioHttp test client fixture
187
191
:param loop: Eventloop fixture
188
192
:param recorder: X-Ray recorder fixture
189
193
"""
190
- client = await test_client (ServerTest .app (loop = loop ))
194
+ client = await aiohttp_client (ServerTest .app (loop = loop ))
191
195
192
196
resp = await client .get ('/error' )
193
197
assert resp .status == 404
@@ -204,15 +208,15 @@ async def test_error(test_client, loop, recorder):
204
208
assert response ['status' ] == 404
205
209
206
210
207
- async def test_exception (test_client , loop , recorder ):
211
+ async def test_exception (aiohttp_client , loop , recorder ):
208
212
"""
209
213
Test handling an exception
210
214
211
- :param test_client : AioHttp test client fixture
215
+ :param aiohttp_client : AioHttp test client fixture
212
216
:param loop: Eventloop fixture
213
217
:param recorder: X-Ray recorder fixture
214
218
"""
215
- client = await test_client (ServerTest .app (loop = loop ))
219
+ client = await aiohttp_client (ServerTest .app (loop = loop ))
216
220
217
221
with pytest .raises (Exception ):
218
222
await client .get ('/exception' )
@@ -231,15 +235,15 @@ async def test_exception(test_client, loop, recorder):
231
235
assert exception .type == 'CancelledError'
232
236
233
237
234
- async def test_unhauthorized (test_client , loop , recorder ):
238
+ async def test_unhauthorized (aiohttp_client , loop , recorder ):
235
239
"""
236
240
Test a 401 response
237
241
238
- :param test_client : AioHttp test client fixture
242
+ :param aiohttp_client : AioHttp test client fixture
239
243
:param loop: Eventloop fixture
240
244
:param recorder: X-Ray recorder fixture
241
245
"""
242
- client = await test_client (ServerTest .app (loop = loop ))
246
+ client = await aiohttp_client (ServerTest .app (loop = loop ))
243
247
244
248
resp = await client .get ('/unauthorized' )
245
249
assert resp .status == 401
@@ -256,8 +260,8 @@ async def test_unhauthorized(test_client, loop, recorder):
256
260
assert response ['status' ] == 401
257
261
258
262
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 ))
261
265
resp = await client .get ('/' )
262
266
xray_header = resp .headers [http .XRAY_HEADER ]
263
267
segment = recorder .emitter .pop ()
@@ -266,42 +270,42 @@ async def test_response_trace_header(test_client, loop, recorder):
266
270
assert expected in xray_header
267
271
268
272
269
- async def test_concurrent (test_client , loop , recorder ):
273
+ async def test_concurrent (aiohttp_client , loop , recorder ):
270
274
"""
271
275
Test multiple concurrent requests
272
276
273
- :param test_client : AioHttp test client fixture
277
+ :param aiohttp_client : AioHttp test client fixture
274
278
:param loop: Eventloop fixture
275
279
:param recorder: X-Ray recorder fixture
276
280
"""
277
- client = await test_client (ServerTest .app (loop = loop ))
281
+ client = await aiohttp_client (ServerTest .app (loop = loop ))
278
282
279
283
recorder .emitter = CustomStubbedEmitter ()
280
284
281
285
async def get_delay ():
282
286
resp = await client .get ('/delay' )
283
287
assert resp .status == 200
284
288
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 )
289
293
290
294
# Ensure all ID's are different
291
295
ids = [item .id for item in recorder .emitter .local ]
292
296
assert len (ids ) == len (set (ids ))
293
297
294
298
295
- async def test_disabled_sdk (test_client , loop , recorder ):
299
+ async def test_disabled_sdk (aiohttp_client , loop , recorder ):
296
300
"""
297
301
Test a normal response when the SDK is disabled.
298
302
299
- :param test_client : AioHttp test client fixture
303
+ :param aiohttp_client : AioHttp test client fixture
300
304
:param loop: Eventloop fixture
301
305
:param recorder: X-Ray recorder fixture
302
306
"""
303
307
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 ))
305
309
306
310
resp = await client .get ('/' )
307
311
assert resp .status == 200
0 commit comments