Skip to content

Commit bbbf5ba

Browse files
committed
set delay to 0 except for operation we want to cancel
This speeds up the unit tests considerably by eliminating unnecessary delay.
1 parent 7d7e5d7 commit bbbf5ba

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

tests/test_asyncio/test_cwe_404.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import contextlib
23

34
import pytest
45

@@ -17,7 +18,7 @@ def redis_addr(request):
1718

1819

1920
class DelayProxy:
20-
def __init__(self, addr, redis_addr, delay: float):
21+
def __init__(self, addr, redis_addr, delay: float = 0.0):
2122
self.addr = addr
2223
self.redis_addr = redis_addr
2324
self.delay = delay
@@ -31,6 +32,19 @@ async def start(self):
3132
self.server = await asyncio.start_server(self.handle, *self.addr)
3233
self.ROUTINE = asyncio.create_task(self.server.serve_forever())
3334

35+
@contextlib.contextmanager
36+
def set_delay(self, delay: float = 0.0):
37+
"""
38+
Allow to override the delay for parts of tests which aren't time dependent,
39+
to speed up execution.
40+
"""
41+
old = self.delay
42+
self.delay = delay
43+
try:
44+
yield
45+
finally:
46+
self.delay = old
47+
3448
async def handle(self, reader, writer):
3549
# establish connection to redis
3650
redis_reader, redis_writer = await asyncio.open_connection(*self.redis_addr)
@@ -70,7 +84,7 @@ async def test_standalone(delay, redis_addr):
7084

7185
# create a tcp socket proxy that relays data to Redis and back,
7286
# inserting 0.1 seconds of delay
73-
dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr, delay=delay * 2)
87+
dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr)
7488
await dp.start()
7589

7690
for b in [True, False]:
@@ -80,8 +94,14 @@ async def test_standalone(delay, redis_addr):
8094
await r.set("foo", "foo")
8195
await r.set("bar", "bar")
8296

97+
async def op(r):
98+
with dp.set_delay(delay * 2):
99+
return await r.get(
100+
"foo"
101+
) # <-- this is the operation we want to cancel
102+
83103
dp.send_event.clear()
84-
t = asyncio.create_task(r.get("foo"))
104+
t = asyncio.create_task(op(r))
85105
# Wait until the task has sent, and then some, to make sure it has
86106
# settled on the read.
87107
await dp.send_event.wait()
@@ -116,8 +136,14 @@ async def test_standalone_pipeline(delay, redis_addr):
116136
pipe2.ping()
117137
pipe2.get("foo")
118138

139+
async def op(pipe):
140+
with dp.set_delay(delay * 2):
141+
return await pipe.get(
142+
"foo"
143+
).execute() # <-- this is the operation we want to cancel
144+
119145
dp.send_event.clear()
120-
t = asyncio.create_task(pipe.get("foo").execute())
146+
t = asyncio.create_task(op(pipe))
121147
# wait until task has settled on the read
122148
await dp.send_event.wait()
123149
await asyncio.sleep(0.01)
@@ -149,16 +175,21 @@ async def test_standalone_pipeline(delay, redis_addr):
149175
async def test_cluster(request, redis_addr):
150176

151177
redis_addr = redis_addr[0], 6372 # use the cluster port
152-
dp = DelayProxy(addr=("127.0.0.1", 5381), redis_addr=redis_addr, delay=0.1)
178+
delay = 0.1
179+
dp = DelayProxy(addr=("127.0.0.1", 5381), redis_addr=redis_addr)
153180
await dp.start()
154181

155182
r = RedisCluster.from_url("redis://127.0.0.1:5381")
156183
await r.initialize()
157184
await r.set("foo", "foo")
158185
await r.set("bar", "bar")
159186

187+
async def op(r):
188+
with dp.set_delay(delay):
189+
return await r.get("foo")
190+
160191
dp.send_event.clear()
161-
t = asyncio.create_task(r.get("foo"))
192+
t = asyncio.create_task(op(r))
162193
await dp.send_event.wait()
163194
await asyncio.sleep(0.01)
164195
t.cancel()

0 commit comments

Comments
 (0)