Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix asyncio testing setup #3832

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[pytest]
addopts = -vvv -rfEs -s --durations=5 --cov=./sentry_sdk --cov-branch --cov-report= --tb=short --junitxml=.junitxml
asyncio_mode = strict
asyncio_default_fixture_loop_scope = function
markers =
tests_internal_exceptions: Handle internal exceptions just as the SDK does, to test it. (Otherwise internal exceptions are recorded and reraised.)

Expand Down
37 changes: 13 additions & 24 deletions tests/integrations/asyncio/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ async def boom():
1 / 0


@pytest.fixture(scope="session")
def event_loop(request):
"""Create an instance of the default event loop for each test case."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()


def get_sentry_task_factory(mock_get_running_loop):
"""
Patches (mocked) asyncio and gets the sentry_task_factory.
Expand All @@ -58,11 +50,10 @@ def get_sentry_task_factory(mock_get_running_loop):


@minimum_python_37
@pytest.mark.asyncio
@pytest.mark.asyncio(loop_scope="module")
async def test_create_task(
sentry_init,
capture_events,
event_loop,
):
sentry_init(
traces_sample_rate=1.0,
Expand All @@ -76,10 +67,10 @@ async def test_create_task(

with sentry_sdk.start_transaction(name="test_transaction_for_create_task"):
with sentry_sdk.start_span(op="root", name="not so important"):
tasks = [event_loop.create_task(foo()), event_loop.create_task(bar())]
tasks = [asyncio.create_task(foo()), asyncio.create_task(bar())]
await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)

sentry_sdk.flush()
sentry_sdk.flush()

(transaction_event,) = events

Expand All @@ -102,7 +93,7 @@ async def test_create_task(


@minimum_python_37
@pytest.mark.asyncio
@pytest.mark.asyncio(loop_scope="module")
async def test_gather(
sentry_init,
capture_events,
Expand All @@ -121,7 +112,7 @@ async def test_gather(
with sentry_sdk.start_span(op="root", name="not so important"):
await asyncio.gather(foo(), bar(), return_exceptions=True)

sentry_sdk.flush()
sentry_sdk.flush()

(transaction_event,) = events

Expand All @@ -144,11 +135,10 @@ async def test_gather(


@minimum_python_37
@pytest.mark.asyncio
@pytest.mark.asyncio(loop_scope="module")
async def test_exception(
sentry_init,
capture_events,
event_loop,
):
sentry_init(
traces_sample_rate=1.0,
Expand All @@ -162,10 +152,10 @@ async def test_exception(

with sentry_sdk.start_transaction(name="test_exception"):
with sentry_sdk.start_span(op="root", name="not so important"):
tasks = [event_loop.create_task(boom()), event_loop.create_task(bar())]
tasks = [asyncio.create_task(boom()), asyncio.create_task(bar())]
await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)

sentry_sdk.flush()
sentry_sdk.flush()

(error_event, _) = events

Expand All @@ -178,7 +168,7 @@ async def test_exception(


@minimum_python_37
@pytest.mark.asyncio
@pytest.mark.asyncio(loop_scope="module")
async def test_task_result(sentry_init):
sentry_init(
integrations=[
Expand All @@ -194,7 +184,7 @@ async def add(a, b):


@minimum_python_311
@pytest.mark.asyncio
@pytest.mark.asyncio(loop_scope="module")
async def test_task_with_context(sentry_init):
"""
Integration test to ensure working context parameter in Python 3.11+
Expand Down Expand Up @@ -362,11 +352,10 @@ def test_sentry_task_factory_context_with_factory(mock_get_running_loop):


@minimum_python_37
@pytest.mark.asyncio
@pytest.mark.asyncio(loop_scope="module")
async def test_span_origin(
sentry_init,
capture_events,
event_loop,
):
sentry_init(
integrations=[AsyncioIntegration()],
Expand All @@ -377,11 +366,11 @@ async def test_span_origin(

with sentry_sdk.start_transaction(name="something"):
tasks = [
event_loop.create_task(foo()),
asyncio.create_task(foo()),
]
await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)

sentry_sdk.flush()
sentry_sdk.flush()

(event,) = events

Expand Down
16 changes: 4 additions & 12 deletions tests/integrations/grpc/test_grpc_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,14 @@
AIO_PORT += os.getpid() % 100 # avoid port conflicts when running tests in parallel


@pytest.fixture(scope="function")
def event_loop(request):
"""Create an instance of the default event loop for each test case."""
loop = asyncio.new_event_loop()
yield loop
loop.close()


@pytest_asyncio.fixture(scope="function")
async def grpc_server(sentry_init, event_loop):
async def grpc_server(sentry_init):
sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()])
server = grpc.aio.server()
server.add_insecure_port("[::]:{}".format(AIO_PORT))
add_gRPCTestServiceServicer_to_server(TestService, server)

await event_loop.create_task(server.start())
await asyncio.create_task(server.start())

try:
yield server
Expand All @@ -45,12 +37,12 @@ async def grpc_server(sentry_init, event_loop):


@pytest.mark.asyncio
async def test_noop_for_unimplemented_method(event_loop, sentry_init, capture_events):
async def test_noop_for_unimplemented_method(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0, integrations=[GRPCIntegration()])
server = grpc.aio.server()
server.add_insecure_port("[::]:{}".format(AIO_PORT))

await event_loop.create_task(server.start())
await asyncio.create_task(server.start())

events = capture_events()
try:
Expand Down
Loading