Skip to content

Commit a028e0f

Browse files
authored
Merge pull request #364 from michael-k/async
Fix deprecation warnings related to asyncio
2 parents 275c131 + a289bd6 commit a028e0f

File tree

5 files changed

+83
-61
lines changed

5 files changed

+83
-61
lines changed

tests/ext/aiobotocore/test_aiobotocore.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212

1313

1414
@pytest.fixture(scope='function')
15-
def recorder(loop):
15+
def recorder(event_loop):
1616
"""
1717
Clean up before and after each test run
1818
"""
19-
xray_recorder.configure(service='test', sampling=False, context=AsyncContext(loop=loop))
19+
xray_recorder.configure(
20+
service='test', sampling=False, context=AsyncContext(loop=event_loop)
21+
)
2022
xray_recorder.clear_trace_entities()
2123
yield xray_recorder
2224
xray_recorder.clear_trace_entities()
2325

2426

25-
async def test_describe_table(loop, recorder):
27+
async def test_describe_table(event_loop, recorder):
2628
segment = recorder.begin_segment('name')
2729

2830
req_id = '1234'
@@ -45,7 +47,7 @@ async def test_describe_table(loop, recorder):
4547
assert aws_meta['operation'] == 'DescribeTable'
4648

4749

48-
async def test_s3_parameter_capture(loop, recorder):
50+
async def test_s3_parameter_capture(event_loop, recorder):
4951
segment = recorder.begin_segment('name')
5052

5153
bucket_name = 'mybucket'
@@ -70,7 +72,7 @@ async def test_s3_parameter_capture(loop, recorder):
7072
assert aws_meta['operation'] == 'GetObject'
7173

7274

73-
async def test_list_parameter_counting(loop, recorder):
75+
async def test_list_parameter_counting(event_loop, recorder):
7476
"""
7577
Test special parameters that have shape of list are recorded
7678
as count based on `para_whitelist.json`
@@ -103,7 +105,7 @@ async def test_list_parameter_counting(loop, recorder):
103105
assert aws_meta['queue_name_prefix'] == queue_name_prefix
104106

105107

106-
async def test_map_parameter_grouping(loop, recorder):
108+
async def test_map_parameter_grouping(event_loop, recorder):
107109
"""
108110
Test special parameters that have shape of map are recorded
109111
as a list of keys based on `para_whitelist.json`
@@ -131,9 +133,10 @@ async def test_map_parameter_grouping(loop, recorder):
131133
assert sorted(aws_meta['table_names']) == ['table1', 'table2']
132134

133135

134-
async def test_context_missing_not_swallow_return(loop, recorder):
136+
async def test_context_missing_not_swallow_return(event_loop, recorder):
135137
xray_recorder.configure(service='test', sampling=False,
136-
context=AsyncContext(loop=loop), context_missing='LOG_ERROR')
138+
context=AsyncContext(loop=event_loop),
139+
context_missing='LOG_ERROR')
137140

138141
response = {'ResponseMetadata': {'RequestId': '1234', 'HTTPStatusCode': 403}}
139142

@@ -146,9 +149,10 @@ async def test_context_missing_not_swallow_return(loop, recorder):
146149
assert actual_resp == response
147150

148151

149-
async def test_context_missing_not_suppress_exception(loop, recorder):
152+
async def test_context_missing_not_suppress_exception(event_loop, recorder):
150153
xray_recorder.configure(service='test', sampling=False,
151-
context=AsyncContext(loop=loop), context_missing='LOG_ERROR')
154+
context=AsyncContext(loop=event_loop),
155+
context_missing='LOG_ERROR')
152156

153157
session = get_session()
154158
async with session.create_client('dynamodb', region_name='eu-west-2') as client:

tests/ext/aiohttp/test_middleware.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Expects pytest-aiohttp
55
"""
66
import asyncio
7+
import sys
78
from aws_xray_sdk import global_sdk_config
89
try:
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

tests/test_async_local_storage.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import asyncio
22
import random
3+
import sys
34

45
from aws_xray_sdk.core.async_context import TaskLocalStorage
56

67

7-
def test_localstorage_isolation(loop):
8-
local_storage = TaskLocalStorage(loop=loop)
8+
def test_localstorage_isolation(event_loop):
9+
local_storage = TaskLocalStorage(loop=event_loop)
910

1011
async def _test():
1112
"""
@@ -19,7 +20,10 @@ async def _test():
1920
random_int = random.random()
2021
local_storage.randint = random_int
2122

22-
await asyncio.sleep(0.0, loop=loop)
23+
if sys.version_info >= (3, 8):
24+
await asyncio.sleep(0.0)
25+
else:
26+
await asyncio.sleep(0.0, loop=event_loop)
2327

2428
current_random_int = local_storage.randint
2529
assert random_int == current_random_int
@@ -29,9 +33,17 @@ async def _test():
2933
return False
3034

3135
# Run loads of concurrent tasks
32-
results = loop.run_until_complete(
33-
asyncio.wait([_test() for _ in range(0, 100)], loop=loop)
34-
)
36+
if sys.version_info >= (3, 8):
37+
results = event_loop.run_until_complete(
38+
asyncio.wait([event_loop.create_task(_test()) for _ in range(0, 100)])
39+
)
40+
else:
41+
results = event_loop.run_until_complete(
42+
asyncio.wait(
43+
[event_loop.create_task(_test()) for _ in range(0, 100)],
44+
loop=event_loop,
45+
)
46+
)
3547
results = [item.result() for item in results[0]]
3648

3749
# Double check all is good

tests/test_async_recorder.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ async def async_method():
1919
await async_method2()
2020

2121

22-
async def test_capture(loop):
23-
xray_recorder.configure(service='test', sampling=False, context=AsyncContext(loop=loop))
22+
async def test_capture(event_loop):
23+
xray_recorder.configure(
24+
service='test', sampling=False, context=AsyncContext(loop=event_loop)
25+
)
2426

2527
segment = xray_recorder.begin_segment('name')
2628

@@ -44,8 +46,10 @@ async def test_capture(loop):
4446
assert platform.python_implementation() == service.get('runtime')
4547
assert platform.python_version() == service.get('runtime_version')
4648

47-
async def test_concurrent_calls(loop):
48-
xray_recorder.configure(service='test', sampling=False, context=AsyncContext(loop=loop))
49+
async def test_concurrent_calls(event_loop):
50+
xray_recorder.configure(
51+
service='test', sampling=False, context=AsyncContext(loop=event_loop)
52+
)
4953
async with xray_recorder.in_segment_async('segment') as segment:
5054
global counter
5155
counter = 0
@@ -67,8 +71,10 @@ async def assert_task():
6771
assert subseg_parent_id == segment.id
6872

6973

70-
async def test_async_context_managers(loop):
71-
xray_recorder.configure(service='test', sampling=False, context=AsyncContext(loop=loop))
74+
async def test_async_context_managers(event_loop):
75+
xray_recorder.configure(
76+
service='test', sampling=False, context=AsyncContext(loop=event_loop)
77+
)
7278

7379
async with xray_recorder.in_segment_async('segment') as segment:
7480
async with xray_recorder.capture_async('aio_capture') as subsegment:

tox.ini

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,13 @@ deps =
6868
py34: typing >= 3.7.4.3
6969

7070
; Python 3.5+ only deps
71-
; for some reason pytest-aiohttp is required for "core" tests
72-
; TODO: find and replace by more direct dependency
73-
py{35,36,37,38,39}: pytest-aiohttp
71+
py{35,36,37,38,39}: pytest-asyncio
7472

7573
ext-aiobotocore: aiobotocore >= 0.10.0
76-
ext-aiobotocore: pytest-aiohttp
74+
ext-aiobotocore: pytest-asyncio
7775

78-
ext-aiohttp: aiohttp >= 3.0.0
79-
; Breaking change where the `test_client` fixture was renamed.
80-
; Also, the stable version is only supported for Python 3.7+
81-
ext-aiohttp: pytest-aiohttp < 1.0.0
76+
ext-aiohttp: aiohttp >= 3.3.0
77+
ext-aiohttp: pytest-aiohttp
8278

8379
ext-httpx: httpx >= 0.20
8480
ext-httpx: pytest-asyncio >= 0.19

0 commit comments

Comments
 (0)