15
15
pass # All tests will be skipped with incompatible versions
16
16
17
17
18
- minimum_python_37 = pytest .mark .skipif (
19
- sys .version_info < (3 , 7 ), reason = "Asyncio tests need Python >= 3.7 "
18
+ minimum_python_38 = pytest .mark .skipif (
19
+ sys .version_info < (3 , 8 ), reason = "Asyncio tests need Python >= 3.8 "
20
20
)
21
21
22
22
@@ -38,14 +38,6 @@ async def boom():
38
38
1 / 0
39
39
40
40
41
- @pytest .fixture (scope = "session" )
42
- def event_loop (request ):
43
- """Create an instance of the default event loop for each test case."""
44
- loop = asyncio .get_event_loop_policy ().new_event_loop ()
45
- yield loop
46
- loop .close ()
47
-
48
-
49
41
def get_sentry_task_factory (mock_get_running_loop ):
50
42
"""
51
43
Patches (mocked) asyncio and gets the sentry_task_factory.
@@ -57,12 +49,11 @@ def get_sentry_task_factory(mock_get_running_loop):
57
49
return patched_factory
58
50
59
51
60
- @minimum_python_37
61
- @pytest .mark .asyncio
52
+ @minimum_python_38
53
+ @pytest .mark .asyncio ( loop_scope = "module" )
62
54
async def test_create_task (
63
55
sentry_init ,
64
56
capture_events ,
65
- event_loop ,
66
57
):
67
58
sentry_init (
68
59
traces_sample_rate = 1.0 ,
@@ -76,10 +67,10 @@ async def test_create_task(
76
67
77
68
with sentry_sdk .start_transaction (name = "test_transaction_for_create_task" ):
78
69
with sentry_sdk .start_span (op = "root" , name = "not so important" ):
79
- tasks = [event_loop .create_task (foo ()), event_loop .create_task (bar ())]
70
+ tasks = [asyncio .create_task (foo ()), asyncio .create_task (bar ())]
80
71
await asyncio .wait (tasks , return_when = asyncio .FIRST_EXCEPTION )
81
72
82
- sentry_sdk .flush ()
73
+ sentry_sdk .flush ()
83
74
84
75
(transaction_event ,) = events
85
76
@@ -101,8 +92,8 @@ async def test_create_task(
101
92
)
102
93
103
94
104
- @minimum_python_37
105
- @pytest .mark .asyncio
95
+ @minimum_python_38
96
+ @pytest .mark .asyncio ( loop_scope = "module" )
106
97
async def test_gather (
107
98
sentry_init ,
108
99
capture_events ,
@@ -121,7 +112,7 @@ async def test_gather(
121
112
with sentry_sdk .start_span (op = "root" , name = "not so important" ):
122
113
await asyncio .gather (foo (), bar (), return_exceptions = True )
123
114
124
- sentry_sdk .flush ()
115
+ sentry_sdk .flush ()
125
116
126
117
(transaction_event ,) = events
127
118
@@ -143,12 +134,11 @@ async def test_gather(
143
134
)
144
135
145
136
146
- @minimum_python_37
147
- @pytest .mark .asyncio
137
+ @minimum_python_38
138
+ @pytest .mark .asyncio ( loop_scope = "module" )
148
139
async def test_exception (
149
140
sentry_init ,
150
141
capture_events ,
151
- event_loop ,
152
142
):
153
143
sentry_init (
154
144
traces_sample_rate = 1.0 ,
@@ -162,10 +152,10 @@ async def test_exception(
162
152
163
153
with sentry_sdk .start_transaction (name = "test_exception" ):
164
154
with sentry_sdk .start_span (op = "root" , name = "not so important" ):
165
- tasks = [event_loop .create_task (boom ()), event_loop .create_task (bar ())]
155
+ tasks = [asyncio .create_task (boom ()), asyncio .create_task (bar ())]
166
156
await asyncio .wait (tasks , return_when = asyncio .FIRST_EXCEPTION )
167
157
168
- sentry_sdk .flush ()
158
+ sentry_sdk .flush ()
169
159
170
160
(error_event , _ ) = events
171
161
@@ -177,8 +167,8 @@ async def test_exception(
177
167
assert error_event ["exception" ]["values" ][0 ]["mechanism" ]["type" ] == "asyncio"
178
168
179
169
180
- @minimum_python_37
181
- @pytest .mark .asyncio
170
+ @minimum_python_38
171
+ @pytest .mark .asyncio ( loop_scope = "module" )
182
172
async def test_task_result (sentry_init ):
183
173
sentry_init (
184
174
integrations = [
@@ -194,7 +184,7 @@ async def add(a, b):
194
184
195
185
196
186
@minimum_python_311
197
- @pytest .mark .asyncio
187
+ @pytest .mark .asyncio ( loop_scope = "module" )
198
188
async def test_task_with_context (sentry_init ):
199
189
"""
200
190
Integration test to ensure working context parameter in Python 3.11+
@@ -223,7 +213,7 @@ async def retrieve_value():
223
213
assert retrieve_task .result () == "changed value"
224
214
225
215
226
- @minimum_python_37
216
+ @minimum_python_38
227
217
@patch ("asyncio.get_running_loop" )
228
218
def test_patch_asyncio (mock_get_running_loop ):
229
219
"""
@@ -242,7 +232,7 @@ def test_patch_asyncio(mock_get_running_loop):
242
232
assert callable (sentry_task_factory )
243
233
244
234
245
- @minimum_python_37
235
+ @minimum_python_38
246
236
@patch ("asyncio.get_running_loop" )
247
237
@patch ("sentry_sdk.integrations.asyncio.Task" )
248
238
def test_sentry_task_factory_no_factory (MockTask , mock_get_running_loop ): # noqa: N803
@@ -271,7 +261,7 @@ def test_sentry_task_factory_no_factory(MockTask, mock_get_running_loop): # noq
271
261
assert task_kwargs ["loop" ] == mock_loop
272
262
273
263
274
- @minimum_python_37
264
+ @minimum_python_38
275
265
@patch ("asyncio.get_running_loop" )
276
266
def test_sentry_task_factory_with_factory (mock_get_running_loop ):
277
267
mock_loop = mock_get_running_loop .return_value
@@ -361,12 +351,11 @@ def test_sentry_task_factory_context_with_factory(mock_get_running_loop):
361
351
assert task_factory_kwargs ["context" ] == mock_context
362
352
363
353
364
- @minimum_python_37
365
- @pytest .mark .asyncio
354
+ @minimum_python_38
355
+ @pytest .mark .asyncio ( loop_scope = "module" )
366
356
async def test_span_origin (
367
357
sentry_init ,
368
358
capture_events ,
369
- event_loop ,
370
359
):
371
360
sentry_init (
372
361
integrations = [AsyncioIntegration ()],
@@ -377,11 +366,11 @@ async def test_span_origin(
377
366
378
367
with sentry_sdk .start_transaction (name = "something" ):
379
368
tasks = [
380
- event_loop .create_task (foo ()),
369
+ asyncio .create_task (foo ()),
381
370
]
382
371
await asyncio .wait (tasks , return_when = asyncio .FIRST_EXCEPTION )
383
372
384
- sentry_sdk .flush ()
373
+ sentry_sdk .flush ()
385
374
386
375
(event ,) = events
387
376
0 commit comments