Skip to content

Commit bddf9b5

Browse files
committed
Merge remote-tracking branch 'origin/master' into potel-base
2 parents 16abb1a + 6bd7e08 commit bddf9b5

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[pytest]
22
addopts = -vvv -rfEs -s --durations=5 --cov=./sentry_sdk --cov-branch --cov-report= --tb=short --junitxml=.junitxml
33
asyncio_mode = strict
4+
asyncio_default_fixture_loop_scope = function
45
markers =
56
tests_internal_exceptions: Handle internal exceptions just as the SDK does, to test it. (Otherwise internal exceptions are recorded and reraised.)
67

tests/integrations/asyncio/test_asyncio.py

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
pass # All tests will be skipped with incompatible versions
1616

1717

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"
2020
)
2121

2222

@@ -38,14 +38,6 @@ async def boom():
3838
1 / 0
3939

4040

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-
4941
def get_sentry_task_factory(mock_get_running_loop):
5042
"""
5143
Patches (mocked) asyncio and gets the sentry_task_factory.
@@ -57,12 +49,11 @@ def get_sentry_task_factory(mock_get_running_loop):
5749
return patched_factory
5850

5951

60-
@minimum_python_37
61-
@pytest.mark.asyncio
52+
@minimum_python_38
53+
@pytest.mark.asyncio(loop_scope="module")
6254
async def test_create_task(
6355
sentry_init,
6456
capture_events,
65-
event_loop,
6657
):
6758
sentry_init(
6859
traces_sample_rate=1.0,
@@ -76,10 +67,10 @@ async def test_create_task(
7667

7768
with sentry_sdk.start_span(name="test_transaction_for_create_task"):
7869
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())]
8071
await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)
8172

82-
sentry_sdk.flush()
73+
sentry_sdk.flush()
8374

8475
(transaction_event,) = events
8576

@@ -101,8 +92,8 @@ async def test_create_task(
10192
)
10293

10394

104-
@minimum_python_37
105-
@pytest.mark.asyncio
95+
@minimum_python_38
96+
@pytest.mark.asyncio(loop_scope="module")
10697
async def test_gather(
10798
sentry_init,
10899
capture_events,
@@ -121,7 +112,7 @@ async def test_gather(
121112
with sentry_sdk.start_span(op="root", name="not so important"):
122113
await asyncio.gather(foo(), bar(), return_exceptions=True)
123114

124-
sentry_sdk.flush()
115+
sentry_sdk.flush()
125116

126117
(transaction_event,) = events
127118

@@ -143,12 +134,11 @@ async def test_gather(
143134
)
144135

145136

146-
@minimum_python_37
147-
@pytest.mark.asyncio
137+
@minimum_python_38
138+
@pytest.mark.asyncio(loop_scope="module")
148139
async def test_exception(
149140
sentry_init,
150141
capture_events,
151-
event_loop,
152142
):
153143
sentry_init(
154144
traces_sample_rate=1.0,
@@ -163,10 +153,10 @@ async def test_exception(
163153
with sentry_sdk.start_span(name="test_exception"):
164154
sentry_sdk.get_isolation_scope().set_transaction_name("test_exception")
165155
with sentry_sdk.start_span(op="root", name="not so important"):
166-
tasks = [event_loop.create_task(boom()), event_loop.create_task(bar())]
156+
tasks = [asyncio.create_task(boom()), asyncio.create_task(bar())]
167157
await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)
168158

169-
sentry_sdk.flush()
159+
sentry_sdk.flush()
170160

171161
(error_event, _) = events
172162

@@ -178,8 +168,8 @@ async def test_exception(
178168
assert error_event["exception"]["values"][0]["mechanism"]["type"] == "asyncio"
179169

180170

181-
@minimum_python_37
182-
@pytest.mark.asyncio
171+
@minimum_python_38
172+
@pytest.mark.asyncio(loop_scope="module")
183173
async def test_task_result(sentry_init):
184174
sentry_init(
185175
integrations=[
@@ -195,7 +185,7 @@ async def add(a, b):
195185

196186

197187
@minimum_python_311
198-
@pytest.mark.asyncio
188+
@pytest.mark.asyncio(loop_scope="module")
199189
async def test_task_with_context(sentry_init):
200190
"""
201191
Integration test to ensure working context parameter in Python 3.11+
@@ -224,7 +214,7 @@ async def retrieve_value():
224214
assert retrieve_task.result() == "changed value"
225215

226216

227-
@minimum_python_37
217+
@minimum_python_38
228218
@patch("asyncio.get_running_loop")
229219
def test_patch_asyncio(mock_get_running_loop):
230220
"""
@@ -243,7 +233,7 @@ def test_patch_asyncio(mock_get_running_loop):
243233
assert callable(sentry_task_factory)
244234

245235

246-
@minimum_python_37
236+
@minimum_python_38
247237
@patch("asyncio.get_running_loop")
248238
@patch("sentry_sdk.integrations.asyncio.Task")
249239
def test_sentry_task_factory_no_factory(MockTask, mock_get_running_loop): # noqa: N803
@@ -272,7 +262,7 @@ def test_sentry_task_factory_no_factory(MockTask, mock_get_running_loop): # noq
272262
assert task_kwargs["loop"] == mock_loop
273263

274264

275-
@minimum_python_37
265+
@minimum_python_38
276266
@patch("asyncio.get_running_loop")
277267
def test_sentry_task_factory_with_factory(mock_get_running_loop):
278268
mock_loop = mock_get_running_loop.return_value
@@ -362,12 +352,11 @@ def test_sentry_task_factory_context_with_factory(mock_get_running_loop):
362352
assert task_factory_kwargs["context"] == mock_context
363353

364354

365-
@minimum_python_37
366-
@pytest.mark.asyncio
355+
@minimum_python_38
356+
@pytest.mark.asyncio(loop_scope="module")
367357
async def test_span_origin(
368358
sentry_init,
369359
capture_events,
370-
event_loop,
371360
):
372361
sentry_init(
373362
integrations=[AsyncioIntegration()],
@@ -378,11 +367,11 @@ async def test_span_origin(
378367

379368
with sentry_sdk.start_span(name="something"):
380369
tasks = [
381-
event_loop.create_task(foo()),
370+
asyncio.create_task(foo()),
382371
]
383372
await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)
384373

385-
sentry_sdk.flush()
374+
sentry_sdk.flush()
386375

387376
(event,) = events
388377

tests/integrations/grpc/test_grpc_aio.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,14 @@
2121
AIO_PORT += os.getpid() % 100 # avoid port conflicts when running tests in parallel
2222

2323

24-
@pytest.fixture(scope="function")
25-
def event_loop(request):
26-
"""Create an instance of the default event loop for each test case."""
27-
loop = asyncio.new_event_loop()
28-
yield loop
29-
loop.close()
30-
31-
3224
@pytest_asyncio.fixture(scope="function")
33-
async def grpc_server(sentry_init, event_loop):
25+
async def grpc_server(sentry_init):
3426
sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()])
3527
server = grpc.aio.server()
3628
server.add_insecure_port("[::]:{}".format(AIO_PORT))
3729
add_gRPCTestServiceServicer_to_server(TestService, server)
3830

39-
await event_loop.create_task(server.start())
31+
await asyncio.create_task(server.start())
4032

4133
try:
4234
yield server
@@ -45,12 +37,12 @@ async def grpc_server(sentry_init, event_loop):
4537

4638

4739
@pytest.mark.asyncio
48-
async def test_noop_for_unimplemented_method(event_loop, sentry_init, capture_events):
40+
async def test_noop_for_unimplemented_method(sentry_init, capture_events):
4941
sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()])
5042
server = grpc.aio.server()
5143
server.add_insecure_port("[::]:{}".format(AIO_PORT))
5244

53-
await event_loop.create_task(server.start())
45+
await asyncio.create_task(server.start())
5446

5547
events = capture_events()
5648
try:

0 commit comments

Comments
 (0)