Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion sentry_sdk/integrations/django/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def _asgi_middleware_mixin_factory(_check_middleware_span):
"""

class SentryASGIMixin:
if MYPY:
_inner = None

def __init__(self, get_response):
# type: (Callable[..., Any]) -> None
self.get_response = get_response
Expand Down Expand Up @@ -132,7 +135,10 @@ async def __acall__(self, *args, **kwargs):
# type: (*Any, **Any) -> Any
f = self._acall_method
if f is None:
self._acall_method = f = self._inner.__acall__ # type: ignore
if hasattr(self._inner, "__acall__"):
self._acall_method = f = self._inner.__acall__ # type: ignore
else:
self._acall_method = f = self._inner

middleware_span = _check_middleware_span(old_method=f)

Expand Down
37 changes: 37 additions & 0 deletions tests/integrations/django/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,43 @@ async def test_async_views_concurrent_execution(sentry_init, capture_events, set
assert end - start < 1.5


@pytest.mark.asyncio
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
)
async def test_async_middleware_that_is_function_concurrent_execution(
sentry_init, capture_events, settings
):
import asyncio
import time

settings.MIDDLEWARE = [
"tests.integrations.django.myapp.middleware.simple_middleware"
]
asgi_application.load_middleware(is_async=True)

sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)

comm = HttpCommunicator(asgi_application, "GET", "/my_async_view")
comm2 = HttpCommunicator(asgi_application, "GET", "/my_async_view")

loop = asyncio.get_event_loop()

start = time.time()

r1 = loop.create_task(comm.get_response(timeout=5))
r2 = loop.create_task(comm2.get_response(timeout=5))

(resp1, resp2), _ = await asyncio.wait({r1, r2})

end = time.time()

assert resp1.result()["status"] == 200
assert resp2.result()["status"] == 200

assert end - start < 1.5


@pytest.mark.asyncio
@pytest.mark.skipif(
django.VERSION < (3, 1), reason="async views have been introduced in Django 3.1"
Expand Down
19 changes: 19 additions & 0 deletions tests/integrations/django/myapp/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import asyncio
from django.utils.decorators import sync_and_async_middleware


@sync_and_async_middleware
def simple_middleware(get_response):
if asyncio.iscoroutinefunction(get_response):

async def middleware(request):
response = await get_response(request)
return response

else:

def middleware(request):
response = get_response(request)
return response

return middleware