55from typing import Optional
66from unittest .mock import patch
77
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+
913import pytest
1014import pytest_asyncio
1115
@@ -21,7 +25,7 @@ def with_timeout(t):
2125 def wrapper (corofunc ):
2226 @functools .wraps (corofunc )
2327 async def run (* args , ** kwargs ):
24- async with async_timeout . timeout (t ):
28+ async with async_timeout (t ):
2529 return await corofunc (* args , ** kwargs )
2630
2731 return run
@@ -648,7 +652,7 @@ async def test_reconnect_listen(self, r: redis.Redis, pubsub):
648652
649653 async def loop ():
650654 # must make sure the task exits
651- async with async_timeout . timeout (2 ):
655+ async with async_timeout (2 ):
652656 nonlocal interrupt
653657 await pubsub .subscribe ("foo" )
654658 while True :
@@ -677,7 +681,7 @@ async def loop_step():
677681
678682 task = asyncio .get_running_loop ().create_task (loop ())
679683 # get the initial connect message
680- async with async_timeout . timeout (1 ):
684+ async with async_timeout (1 ):
681685 message = await messages .get ()
682686 assert message == {
683687 "channel" : b"foo" ,
@@ -776,7 +780,7 @@ def callback(message):
776780 if n == 1 :
777781 break
778782 await asyncio .sleep (0.1 )
779- async with async_timeout . timeout (0.1 ):
783+ async with async_timeout (0.1 ):
780784 message = await messages .get ()
781785 task .cancel ()
782786 # we expect a cancelled error, not the Runtime error
@@ -839,7 +843,7 @@ async def test_reconnect_socket_error(self, r: redis.Redis, method):
839843 Test that a socket error will cause reconnect
840844 """
841845 try :
842- async with async_timeout . timeout (self .timeout ):
846+ async with async_timeout (self .timeout ):
843847 await self .mysetup (r , method )
844848 # now, disconnect the connection, and wait for it to be re-established
845849 async with self .cond :
@@ -868,7 +872,7 @@ async def test_reconnect_disconnect(self, r: redis.Redis, method):
868872 Test that a manual disconnect() will cause reconnect
869873 """
870874 try :
871- async with async_timeout . timeout (self .timeout ):
875+ async with async_timeout (self .timeout ):
872876 await self .mysetup (r , method )
873877 # now, disconnect the connection, and wait for it to be re-established
874878 async with self .cond :
@@ -923,7 +927,7 @@ async def loop_step_get_message(self):
923927 async def loop_step_listen (self ):
924928 # get a single message via listen()
925929 try :
926- async with async_timeout . timeout (0.1 ):
930+ async with async_timeout (0.1 ):
927931 async for message in self .pubsub .listen ():
928932 await self .messages .put (message )
929933 return True
@@ -947,7 +951,7 @@ async def test_outer_timeout(self, r: redis.Redis):
947951 assert pubsub .connection .is_connected
948952
949953 async def get_msg_or_timeout (timeout = 0.1 ):
950- async with async_timeout . timeout (timeout ):
954+ async with async_timeout (timeout ):
951955 # blocking method to return messages
952956 while True :
953957 response = await pubsub .parse_response (block = True )
0 commit comments