1
- import asyncio
2
1
import contextlib
3
2
import datetime
4
3
import threading
8
7
from unittest .mock import patch , AsyncMock
9
8
10
9
import pytest
11
- import pytest_asyncio
12
10
from pytest_django .fixtures import SettingsWrapper
13
11
from pytest_mock import MockerFixture
14
12
13
+ import anyio
14
+
15
15
from django .core .cache import caches
16
16
from django .core .cache .backends .base import DEFAULT_TIMEOUT
17
17
from django .test import override_settings
22
22
from django_valkey .serializers .msgpack import MSGPackSerializer
23
23
24
24
25
- @pytest_asyncio .fixture (loop_scope = "session" )
25
+ pytestmark = pytest .mark .anyio
26
+
27
+
28
+ @pytest .fixture
26
29
async def patch_itersize_setting () -> Iterable [None ]:
27
30
del caches ["default" ]
28
31
with override_settings (DJANGO_VALKEY_SCAN_ITERSIZE = 30 ):
@@ -31,7 +34,6 @@ async def patch_itersize_setting() -> Iterable[None]:
31
34
del caches ["default" ]
32
35
33
36
34
- @pytest .mark .asyncio (loop_scope = "session" )
35
37
class TestAsyncDjangoValkeyCache :
36
38
async def test_set_int (self , cache : AsyncValkeyCache ):
37
39
if isinstance (cache .client , AsyncHerdClient ):
@@ -72,15 +74,15 @@ async def test_setnx_timeout(self, cache: AsyncValkeyCache):
72
74
# test that timeout still works for nx=True
73
75
res = await cache .aset ("test_key_nx" , 1 , timeout = 2 , nx = True )
74
76
assert res is True
75
- await asyncio .sleep (3 )
77
+ await anyio .sleep (3 )
76
78
res = await cache .aget ("test_key_nx" )
77
79
assert res is None
78
80
79
81
# test that timeout will not affect key, if it was there
80
82
await cache .aset ("test_key_nx" , 1 )
81
83
res = await cache .aset ("test_key_nx" , 2 , timeout = 2 , nx = True )
82
84
assert res is None
83
- await asyncio .sleep (3 )
85
+ await anyio .sleep (3 )
84
86
res = await cache .aget ("test_key_nx" )
85
87
assert res == 1
86
88
@@ -151,7 +153,7 @@ async def test_save_float(self, cache: AsyncValkeyCache):
151
153
152
154
async def test_timeout (self , cache : AsyncValkeyCache ):
153
155
await cache .aset ("test_key" , 222 , timeout = 3 )
154
- await asyncio .sleep (4 )
156
+ await anyio .sleep (4 )
155
157
156
158
res = await cache .aget ("test_key" )
157
159
assert res is None
@@ -170,7 +172,7 @@ async def test_timeout_parameter_as_positional_argument(
170
172
171
173
await cache .aset ("test_key" , 222 , 1 )
172
174
res1 = await cache .aget ("test_key" )
173
- await asyncio .sleep (2 )
175
+ await anyio .sleep (2 )
174
176
res2 = await cache .aget ("test_key" )
175
177
assert res1 == 222
176
178
assert res2 is None
@@ -731,7 +733,7 @@ async def test_lock_released_by_thread(self, cache: AsyncValkeyCache):
731
733
async def release_lock (lock_ ):
732
734
await lock_ .release ()
733
735
734
- t = threading .Thread (target = asyncio .run , args = [release_lock ( lock ) ])
736
+ t = threading .Thread (target = anyio .run , args = [release_lock , lock ])
735
737
t .start ()
736
738
t .join ()
737
739
@@ -801,7 +803,7 @@ async def test_touch_positive_timeout(self, cache: AsyncValkeyCache):
801
803
802
804
assert await cache .atouch ("test_key" , 2 ) is True
803
805
assert await cache .aget ("test_key" ) == 222
804
- await asyncio .sleep (3 )
806
+ await anyio .sleep (3 )
805
807
assert await cache .aget ("test_key" ) is None
806
808
807
809
async def test_touch_negative_timeout (self , cache : AsyncValkeyCache ):
@@ -819,7 +821,7 @@ async def test_touch_forever(self, cache: AsyncValkeyCache):
819
821
result = await cache .atouch ("test_key" , None )
820
822
assert result is True
821
823
assert await cache .attl ("test_key" ) is None
822
- await asyncio .sleep (2 )
824
+ await anyio .sleep (2 )
823
825
assert await cache .aget ("test_key" ) == "foo"
824
826
825
827
async def test_touch_forever_nonexistent (self , cache : AsyncValkeyCache ):
@@ -830,7 +832,7 @@ async def test_touch_default_timeout(self, cache: AsyncValkeyCache):
830
832
await cache .aset ("test_key" , "foo" , timeout = 1 )
831
833
result = await cache .atouch ("test_key" )
832
834
assert result is True
833
- await asyncio .sleep (2 )
835
+ await anyio .sleep (2 )
834
836
assert await cache .aget ("test_key" ) == "foo"
835
837
836
838
async def test_clear (self , cache : AsyncValkeyCache ):
0 commit comments