@@ -819,14 +819,16 @@ async def mysetup(self, r, method):
819819 "type" : "subscribe" ,
820820 }
821821
822- async def mycleanup (self ):
822+ async def myfinish (self ):
823823 message = await self .messages .get ()
824824 assert message == {
825825 "channel" : b"foo" ,
826826 "data" : 1 ,
827827 "pattern" : None ,
828828 "type" : "subscribe" ,
829829 }
830+
831+ async def mykill (self ):
830832 # kill thread
831833 async with self .cond :
832834 self .state = 4 # quit
@@ -836,41 +838,52 @@ async def test_reconnect_socket_error(self, r: redis.Redis, method):
836838 """
837839 Test that a socket error will cause reconnect
838840 """
839- async with async_timeout .timeout (self .timeout ):
840- await self .mysetup (r , method )
841- # now, disconnect the connection, and wait for it to be re-established
842- async with self .cond :
843- assert self .state == 0
844- self .state = 1
845- with mock .patch .object (self .pubsub .connection , "_parser" ) as mockobj :
846- mockobj .read_response .side_effect = socket .error
847- mockobj .can_read .side_effect = socket .error
848- # wait until task noticies the disconnect until we undo the patch
849- await self .cond .wait_for (lambda : self .state >= 2 )
850- assert not self .pubsub .connection .is_connected
851- # it is in a disconnecte state
852- # wait for reconnect
853- await self .cond .wait_for (lambda : self .pubsub .connection .is_connected )
854- assert self .state == 3
841+ try :
842+ async with async_timeout .timeout (self .timeout ):
843+ await self .mysetup (r , method )
844+ # now, disconnect the connection, and wait for it to be re-established
845+ async with self .cond :
846+ assert self .state == 0
847+ self .state = 1
848+ with mock .patch .object (self .pubsub .connection , "_parser" ) as m :
849+ m .read_response .side_effect = socket .error
850+ m .can_read .side_effect = socket .error
851+ # wait until task noticies the disconnect until we
852+ # undo the patch
853+ await self .cond .wait_for (lambda : self .state >= 2 )
854+ assert not self .pubsub .connection .is_connected
855+ # it is in a disconnecte state
856+ # wait for reconnect
857+ await self .cond .wait_for (
858+ lambda : self .pubsub .connection .is_connected
859+ )
860+ assert self .state == 3
855861
856- await self .mycleanup ()
862+ await self .myfinish ()
863+ finally :
864+ await self .mykill ()
857865
858866 async def test_reconnect_disconnect (self , r : redis .Redis , method ):
859867 """
860868 Test that a manual disconnect() will cause reconnect
861869 """
862- async with async_timeout .timeout (self .timeout ):
863- await self .mysetup (r , method )
864- # now, disconnect the connection, and wait for it to be re-established
865- async with self .cond :
866- self .state = 1
867- await self .pubsub .connection .disconnect ()
868- assert not self .pubsub .connection .is_connected
869- # wait for reconnect
870- await self .cond .wait_for (lambda : self .pubsub .connection .is_connected )
871- assert self .state == 3
872-
873- await self .mycleanup ()
870+ try :
871+ async with async_timeout .timeout (self .timeout ):
872+ await self .mysetup (r , method )
873+ # now, disconnect the connection, and wait for it to be re-established
874+ async with self .cond :
875+ self .state = 1
876+ await self .pubsub .connection .disconnect ()
877+ assert not self .pubsub .connection .is_connected
878+ # wait for reconnect
879+ await self .cond .wait_for (
880+ lambda : self .pubsub .connection .is_connected
881+ )
882+ assert self .state == 3
883+
884+ await self .myfinish ()
885+ finally :
886+ await self .mykill ()
874887
875888 async def loop (self ):
876889 # reader loop, performing state transitions as it
0 commit comments