Skip to content

Commit 7ec29db

Browse files
committed
test the signal receiver is registered and works properly
1 parent bb657b9 commit 7ec29db

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

tests/tests_async/test_requests.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import pytest
2+
3+
from django.core import signals
4+
from django.core.cache import cache, close_caches
5+
6+
from django_valkey.base import close_async_caches
7+
8+
9+
class TestWithOldSignal:
10+
@pytest.fixture(autouse=True)
11+
def setup(self):
12+
signals.request_finished.disconnect(close_async_caches)
13+
signals.request_finished.connect(close_caches)
14+
yield
15+
signals.request_finished.disconnect(close_caches)
16+
signals.request_finished.connect(close_async_caches)
17+
18+
@pytest.mark.asyncio(loop_scope="session")
19+
async def test_warning_output_when_request_finished(self, async_client):
20+
with pytest.warns(
21+
RuntimeWarning,
22+
match="coroutine 'AsyncBackendCommands.close' was never awaited",
23+
) as record:
24+
await async_client.get("/async/")
25+
26+
assert (
27+
str(record[0].message)
28+
== "coroutine 'AsyncBackendCommands.close' was never awaited"
29+
)
30+
31+
@pytest.mark.asyncio(loop_scope="session")
32+
async def test_manually_await_signal(self, recwarn):
33+
await signals.request_finished.asend(self.__class__)
34+
assert len(recwarn) == 1
35+
36+
assert (
37+
str(recwarn[0].message)
38+
== "coroutine 'AsyncBackendCommands.close' was never awaited"
39+
)
40+
41+
def test_manually_call_signal(self, recwarn):
42+
signals.request_finished.send(self.__class__)
43+
assert len(recwarn) == 1
44+
45+
assert (
46+
str(recwarn[0].message)
47+
== "coroutine 'AsyncBackendCommands.close' was never awaited"
48+
)
49+
50+
51+
class TestWithNewSignal:
52+
@pytest.mark.asyncio(loop_scope="session")
53+
async def test_warning_output_when_request_finished(self, async_client, recwarn):
54+
await async_client.get("/async/")
55+
56+
assert len(recwarn) == 0
57+
58+
@pytest.mark.asyncio(loop_scope="session")
59+
async def test_manually_await_signal(self, recwarn):
60+
await signals.request_finished.asend(self.__class__)
61+
assert len(recwarn) == 0
62+
63+
def test_manually_call_signal(self, recwarn):
64+
signals.request_finished.send(self.__class__)
65+
assert len(recwarn) == 0
66+
67+
def test_receiver_is_registered_and_old_receiver_unregistered(self):
68+
sync_receivers, async_receivers = signals.request_finished._live_receivers(None)
69+
assert close_async_caches in async_receivers
70+
assert close_caches not in sync_receivers
71+
72+
@pytest.mark.asyncio(loop_scope="session")
73+
async def test_close_is_called_by_signal(self, mocker):
74+
close_spy = mocker.spy(cache, "close")
75+
await signals.request_finished.asend(self.__class__)
76+
assert close_spy.await_count == 1
77+
assert close_spy.call_count == 1

0 commit comments

Comments
 (0)