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

Async integration tests #2001

Merged
merged 15 commits into from
Sep 19, 2024
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
45 changes: 43 additions & 2 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import contextvars
from typing import Optional

import html5lib
from asgiref.local import Local
from django.http import HttpResponse
from django.test import Client, RequestFactory, TestCase, TransactionTestCase
from django.test import (
AsyncClient,
AsyncRequestFactory,
Client,
RequestFactory,
TestCase,
TransactionTestCase,
)

from debug_toolbar.panels import Panel
from debug_toolbar.toolbar import DebugToolbar

data_contextvar = contextvars.ContextVar("djdt_toolbar_test_client")


class ToolbarTestClient(Client):
def request(self, **request):
Expand All @@ -29,11 +39,35 @@ def handle_toolbar_created(sender, toolbar=None, **kwargs):
return response


class AsyncToolbarTestClient(AsyncClient):
async def request(self, **request):
# Use a thread/async task context-local variable to guard against a
# concurrent _created signal from a different thread/task.
salty-ivy marked this conversation as resolved.
Show resolved Hide resolved
# In cases testsuite will have both regular and async tests or
# multiple async tests running in an eventloop making async_client calls.
data_contextvar.set(None)

def handle_toolbar_created(sender, toolbar=None, **kwargs):
data_contextvar.set(toolbar)

DebugToolbar._created.connect(handle_toolbar_created)
try:
response = await super().request(**request)
finally:
DebugToolbar._created.disconnect(handle_toolbar_created)
response.toolbar = data_contextvar.get()

return response


rf = RequestFactory()
arf = AsyncRequestFactory()


class BaseMixin:
_is_async = False
client_class = ToolbarTestClient
async_client_class = AsyncToolbarTestClient

panel: Optional[Panel] = None
panel_id = None
Expand All @@ -42,7 +76,11 @@ def setUp(self):
super().setUp()
self._get_response = lambda request: HttpResponse()
self.request = rf.get("/")
self.toolbar = DebugToolbar(self.request, self.get_response)
if self._is_async:
self.request = arf.get("/")
self.toolbar = DebugToolbar(self.request, self.get_response_async)
else:
self.toolbar = DebugToolbar(self.request, self.get_response)
self.toolbar.stats = {}

if self.panel_id:
Expand All @@ -59,6 +97,9 @@ def tearDown(self):
def get_response(self, request):
return self._get_response(request)

async def get_response_async(self, request):
return self._get_response(request)

def assertValidHTML(self, content):
parser = html5lib.HTMLParser()
parser.parseFragment(content)
Expand Down
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"tests",
]


USE_GIS = os.getenv("DB_BACKEND") == "postgis"

if USE_GIS:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_sql_page(self):
def test_async_sql_page(self):
response = self.client.get("/async_execute_sql/")
self.assertEqual(
len(response.toolbar.get_panel_by_id("SQLPanel").get_stats()["queries"]), 1
len(response.toolbar.get_panel_by_id("SQLPanel").get_stats()["queries"]), 2
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved
)

def test_concurrent_async_sql_page(self):
Expand Down
Loading