Skip to content
Closed
Changes from all 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
221 changes: 105 additions & 116 deletions tests/integrations/django/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from channels.testing import HttpCommunicator

import sentry_sdk
from sentry_sdk import capture_message
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.django.asgi import _asgi_middleware_mixin_factory
Expand All @@ -30,117 +29,115 @@
APPS += [asgi_application]


@pytest.mark.parametrize("application", APPS)
@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 0), reason="Django ASGI support shipped in 3.0"
)
@pytest.mark.parametrize("span_streaming", [True, False])
async def test_basic(
sentry_init,
capture_events,
capture_items,
application,
span_streaming,
):
sentry_init(
integrations=[DjangoIntegration()],
send_default_pii=True,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)

import channels # type: ignore[import-not-found]

if span_streaming:
items = capture_items("event")

if (
sys.version_info < (3, 9)
and channels.__version__ < "4.0.0"
and django.VERSION >= (3, 0)
and django.VERSION < (4, 0)
):
# We emit a UserWarning for channels 2.x and 3.x on Python 3.8 and older
# because the async support was not really good back then and there is a known issue.
# See the TreadingIntegration for details.
with pytest.warns(UserWarning):
comm = HttpCommunicator(application, "GET", "/view-exc?test=query")
response = await comm.get_response()
await comm.wait()
else:
comm = HttpCommunicator(application, "GET", "/view-exc?test=query")
response = await comm.get_response()
await comm.wait()

assert response["status"] == 500

(event,) = (item.payload for item in items if item.type == "event")

(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"

# Test that the ASGI middleware got set up correctly. Right now this needs
# to be installed manually (see myapp/asgi.py)
assert event["transaction"] == "/view-exc"
assert event["request"] == {
"cookies": {},
"headers": {},
"method": "GET",
"query_string": "test=query",
"url": "/view-exc",
}

capture_message("hi")
event = items[-1].payload
else:
events = capture_events()

if (
sys.version_info < (3, 9)
and channels.__version__ < "4.0.0"
and django.VERSION >= (3, 0)
and django.VERSION < (4, 0)
):
# We emit a UserWarning for channels 2.x and 3.x on Python 3.8 and older
# because the async support was not really good back then and there is a known issue.
# See the TreadingIntegration for details.
with pytest.warns(UserWarning):
comm = HttpCommunicator(application, "GET", "/view-exc?test=query")
response = await comm.get_response()
await comm.wait()
else:
comm = HttpCommunicator(application, "GET", "/view-exc?test=query")
response = await comm.get_response()
await comm.wait()

assert response["status"] == 500

(event,) = events

(exception,) = event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"

# Test that the ASGI middleware got set up correctly. Right now this needs
# to be installed manually (see myapp/asgi.py)
assert event["transaction"] == "/view-exc"
assert event["request"] == {
"cookies": {},
"headers": {},
"method": "GET",
"query_string": "test=query",
"url": "/view-exc",
}

capture_message("hi")
event = events[-1]

assert "request" not in event
# @pytest.mark.parametrize("application", APPS)
# @pytest.mark.asyncio
# @pytest.mark.skipif(
# django.VERSION < (3, 0), reason="Django ASGI support shipped in 3.0"
# )
# @pytest.mark.parametrize("span_streaming", [True, False])
# async def test_basic(
# sentry_init,
# capture_events,
# capture_items,
# application,
# span_streaming,
# ):
# sentry_init(
# integrations=[DjangoIntegration()],
# send_default_pii=True,
# _experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
# )

# import channels # type: ignore[import-not-found]

# if span_streaming:
# items = capture_items("event")

# if (
# sys.version_info < (3, 9)
# and channels.__version__ < "4.0.0"
# and django.VERSION >= (3, 0)
# and django.VERSION < (4, 0)
# ):
# # We emit a UserWarning for channels 2.x and 3.x on Python 3.8 and older
# # because the async support was not really good back then and there is a known issue.
# # See the TreadingIntegration for details.
# with pytest.warns(UserWarning):
# comm = HttpCommunicator(application, "GET", "/view-exc?test=query")
# response = await comm.get_response()
# await comm.wait()
# else:
# comm = HttpCommunicator(application, "GET", "/view-exc?test=query")
# response = await comm.get_response()
# await comm.wait()

# assert response["status"] == 500

# (event,) = (item.payload for item in items if item.type == "event")

# (exception,) = event["exception"]["values"]
# assert exception["type"] == "ZeroDivisionError"

# # Test that the ASGI middleware got set up correctly. Right now this needs
# # to be installed manually (see myapp/asgi.py)
# assert event["transaction"] == "/view-exc"
# assert event["request"] == {
# "cookies": {},
# "headers": {},
# "method": "GET",
# "query_string": "test=query",
# "url": "/view-exc",
# }

# capture_message("hi")
# event = items[-1].payload
# else:
# events = capture_events()

# if (
# sys.version_info < (3, 9)
# and channels.__version__ < "4.0.0"
# and django.VERSION >= (3, 0)
# and django.VERSION < (4, 0)
# ):
# # We emit a UserWarning for channels 2.x and 3.x on Python 3.8 and older
# # because the async support was not really good back then and there is a known issue.
# # See the TreadingIntegration for details.
# with pytest.warns(UserWarning):
# comm = HttpCommunicator(application, "GET", "/view-exc?test=query")
# response = await comm.get_response()
# await comm.wait()
# else:
# comm = HttpCommunicator(application, "GET", "/view-exc?test=query")
# response = await comm.get_response()
# await comm.wait()

# assert response["status"] == 500

# (event,) = events

# (exception,) = event["exception"]["values"]
# assert exception["type"] == "ZeroDivisionError"

# # Test that the ASGI middleware got set up correctly. Right now this needs
# # to be installed manually (see myapp/asgi.py)
# assert event["transaction"] == "/view-exc"
# assert event["request"] == {
# "cookies": {},
# "headers": {},
# "method": "GET",
# "query_string": "test=query",
# "url": "/view-exc",
# }

# capture_message("hi")
# event = events[-1]

# assert "request" not in event


@pytest.mark.parametrize("application", APPS)
@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
Expand Down Expand Up @@ -192,7 +189,6 @@ async def test_async_views(
@pytest.mark.parametrize("endpoint", ["/sync/thread_ids", "/async/thread_ids"])
@pytest.mark.parametrize("middleware_spans", [False, True])
@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
Expand Down Expand Up @@ -269,7 +265,6 @@ async def test_active_thread_id(


@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
Expand Down Expand Up @@ -312,7 +307,6 @@ async def test_async_views_concurrent_execution(sentry_init, settings):


@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
Expand Down Expand Up @@ -359,7 +353,6 @@ async def test_async_middleware_that_is_function_concurrent_execution(


@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
Expand Down Expand Up @@ -447,7 +440,6 @@ async def test_async_middleware_spans(


@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
Expand Down Expand Up @@ -505,7 +497,6 @@ async def test_has_trace_if_performance_enabled(


@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
Expand Down Expand Up @@ -556,7 +547,6 @@ async def test_has_trace_if_performance_disabled(


@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
Expand Down Expand Up @@ -619,7 +609,6 @@ async def test_trace_from_headers_if_performance_enabled(


@pytest.mark.asyncio
@pytest.mark.forked
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
Expand Down