5
5
from typing import Optional
6
6
from unittest .mock import patch
7
7
8
- import async_timeout
8
+ if sys .version_info .major >= 3 and sys .version_info .minor >= 11 :
9
+ from asyncio import timeout as async_timeout
10
+ else :
11
+ from async_timeout import timeout as async_timeout
12
+
9
13
import pytest
10
14
import pytest_asyncio
11
15
@@ -21,7 +25,7 @@ def with_timeout(t):
21
25
def wrapper (corofunc ):
22
26
@functools .wraps (corofunc )
23
27
async def run (* args , ** kwargs ):
24
- async with async_timeout . timeout (t ):
28
+ async with async_timeout (t ):
25
29
return await corofunc (* args , ** kwargs )
26
30
27
31
return run
@@ -648,7 +652,7 @@ async def test_reconnect_listen(self, r: redis.Redis, pubsub):
648
652
649
653
async def loop ():
650
654
# must make sure the task exits
651
- async with async_timeout . timeout (2 ):
655
+ async with async_timeout (2 ):
652
656
nonlocal interrupt
653
657
await pubsub .subscribe ("foo" )
654
658
while True :
@@ -677,7 +681,7 @@ async def loop_step():
677
681
678
682
task = asyncio .get_running_loop ().create_task (loop ())
679
683
# get the initial connect message
680
- async with async_timeout . timeout (1 ):
684
+ async with async_timeout (1 ):
681
685
message = await messages .get ()
682
686
assert message == {
683
687
"channel" : b"foo" ,
@@ -776,7 +780,7 @@ def callback(message):
776
780
if n == 1 :
777
781
break
778
782
await asyncio .sleep (0.1 )
779
- async with async_timeout . timeout (0.1 ):
783
+ async with async_timeout (0.1 ):
780
784
message = await messages .get ()
781
785
task .cancel ()
782
786
# we expect a cancelled error, not the Runtime error
@@ -839,7 +843,7 @@ async def test_reconnect_socket_error(self, r: redis.Redis, method):
839
843
Test that a socket error will cause reconnect
840
844
"""
841
845
try :
842
- async with async_timeout . timeout (self .timeout ):
846
+ async with async_timeout (self .timeout ):
843
847
await self .mysetup (r , method )
844
848
# now, disconnect the connection, and wait for it to be re-established
845
849
async with self .cond :
@@ -868,7 +872,7 @@ async def test_reconnect_disconnect(self, r: redis.Redis, method):
868
872
Test that a manual disconnect() will cause reconnect
869
873
"""
870
874
try :
871
- async with async_timeout . timeout (self .timeout ):
875
+ async with async_timeout (self .timeout ):
872
876
await self .mysetup (r , method )
873
877
# now, disconnect the connection, and wait for it to be re-established
874
878
async with self .cond :
@@ -923,7 +927,7 @@ async def loop_step_get_message(self):
923
927
async def loop_step_listen (self ):
924
928
# get a single message via listen()
925
929
try :
926
- async with async_timeout . timeout (0.1 ):
930
+ async with async_timeout (0.1 ):
927
931
async for message in self .pubsub .listen ():
928
932
await self .messages .put (message )
929
933
return True
@@ -947,7 +951,7 @@ async def test_outer_timeout(self, r: redis.Redis):
947
951
assert pubsub .connection .is_connected
948
952
949
953
async def get_msg_or_timeout (timeout = 0.1 ):
950
- async with async_timeout . timeout (timeout ):
954
+ async with async_timeout (timeout ):
951
955
# blocking method to return messages
952
956
while True :
953
957
response = await pubsub .parse_response (block = True )
0 commit comments