@@ -41,25 +41,32 @@ def set_delay(self, delay: float = 0.0):
41
41
Allow to override the delay for parts of tests which aren't time dependent,
42
42
to speed up execution.
43
43
"""
44
- old = self .delay
44
+ old_delay = self .delay
45
45
self .delay = delay
46
46
try :
47
47
yield
48
48
finally :
49
- self .delay = old
49
+ self .delay = old_delay
50
50
51
51
async def handle (self , reader , writer ):
52
52
# establish connection to redis
53
53
redis_reader , redis_writer = await asyncio .open_connection (* self .redis_addr )
54
- pipe1 = asyncio .create_task (
55
- self .pipe (reader , redis_writer , "to redis:" , self .send_event )
56
- )
57
- pipe2 = asyncio .create_task (self .pipe (redis_reader , writer , "from redis:" ))
58
- await asyncio .gather (pipe1 , pipe2 )
54
+ try :
55
+ pipe1 = asyncio .create_task (
56
+ self .pipe (reader , redis_writer , "to redis:" , self .send_event )
57
+ )
58
+ pipe2 = asyncio .create_task (self .pipe (redis_reader , writer , "from redis:" ))
59
+ await asyncio .gather (pipe1 , pipe2 )
60
+ finally :
61
+ redis_writer .close ()
59
62
60
63
async def stop (self ):
61
64
# clean up enough so that we can reuse the looper
62
65
self .ROUTINE .cancel ()
66
+ try :
67
+ await self .ROUTINE
68
+ except asyncio .CancelledError :
69
+ pass
63
70
loop = self .server .get_loop ()
64
71
await loop .shutdown_asyncgens ()
65
72
@@ -183,25 +190,25 @@ async def test_cluster(request, redis_addr):
183
190
dp = DelayProxy (addr = ("127.0.0.1" , 5381 ), redis_addr = redis_addr )
184
191
await dp .start ()
185
192
186
- r = RedisCluster .from_url ("redis://127.0.0.1:5381" )
187
- await r .initialize ()
188
- await r .set ("foo" , "foo" )
189
- await r .set ("bar" , "bar" )
190
-
191
- async def op (r ):
192
- with dp .set_delay (delay ):
193
- return await r .get ("foo" )
194
-
195
- dp .send_event .clear ()
196
- t = asyncio .create_task (op (r ))
197
- await dp .send_event .wait ()
198
- await asyncio .sleep (0.01 )
199
- t .cancel ()
200
- with pytest .raises (asyncio .CancelledError ):
201
- await t
202
-
203
- assert await r .get ("bar" ) == b"bar"
204
- assert await r .ping ()
205
- assert await r .get ("foo" ) == b"foo"
193
+ with contextlib . closing ( RedisCluster .from_url ("redis://127.0.0.1:5381" )) as r :
194
+ await r .initialize ()
195
+ await r .set ("foo" , "foo" )
196
+ await r .set ("bar" , "bar" )
197
+
198
+ async def op (r ):
199
+ with dp .set_delay (delay ):
200
+ return await r .get ("foo" )
201
+
202
+ dp .send_event .clear ()
203
+ t = asyncio .create_task (op (r ))
204
+ await dp .send_event .wait ()
205
+ await asyncio .sleep (0.01 )
206
+ t .cancel ()
207
+ with pytest .raises (asyncio .CancelledError ):
208
+ await t
209
+
210
+ assert await r .get ("bar" ) == b"bar"
211
+ assert await r .ping ()
212
+ assert await r .get ("foo" ) == b"foo"
206
213
207
214
await dp .stop ()
0 commit comments