Skip to content

Commit 25485b9

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 9d20929 commit 25485b9

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

tests/test_asyncio/test_cwe_404.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import contextlib
23
import urllib.parse
34

45
import pytest
@@ -20,7 +21,7 @@ def redis_addr(request):
2021

2122

2223
class DelayProxy:
23-
def __init__(self, addr, redis_addr, delay: float):
24+
def __init__(self, addr, redis_addr, delay: float = 0.0):
2425
self.addr = addr
2526
self.redis_addr = redis_addr
2627
self.delay = delay
@@ -34,6 +35,19 @@ async def start(self):
3435
self.server = await asyncio.start_server(self.handle, *self.addr)
3536
self.ROUTINE = asyncio.create_task(self.server.serve_forever())
3637

38+
@contextlib.contextmanager
39+
def set_delay(self, delay: float = 0.0):
40+
"""
41+
Allow to override the delay for parts of tests which aren't time dependent,
42+
to speed up execution.
43+
"""
44+
old = self.delay
45+
self.delay = delay
46+
try:
47+
yield
48+
finally:
49+
self.delay = old
50+
3751
async def handle(self, reader, writer):
3852
# establish connection to redis
3953
redis_reader, redis_writer = await asyncio.open_connection(*self.redis_addr)
@@ -74,7 +88,7 @@ async def test_standalone(delay, redis_addr):
7488

7589
# create a tcp socket proxy that relays data to Redis and back,
7690
# inserting 0.1 seconds of delay
77-
dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr, delay=delay * 2)
91+
dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr)
7892
await dp.start()
7993

8094
for b in [True, False]:
@@ -84,8 +98,14 @@ async def test_standalone(delay, redis_addr):
8498
await r.set("foo", "foo")
8599
await r.set("bar", "bar")
86100

101+
async def op(r):
102+
with dp.set_delay(delay * 2):
103+
return await r.get(
104+
"foo"
105+
) # <-- this is the operation we want to cancel
106+
87107
dp.send_event.clear()
88-
t = asyncio.create_task(r.get("foo"))
108+
t = asyncio.create_task(op(r))
89109
# Wait until the task has sent, and then some, to make sure it has
90110
# settled on the read.
91111
await dp.send_event.wait()
@@ -106,7 +126,7 @@ async def test_standalone(delay, redis_addr):
106126
@pytest.mark.onlynoncluster
107127
@pytest.mark.parametrize("delay", argvalues=[0.05, 0.5, 1, 2])
108128
async def test_standalone_pipeline(delay, redis_addr):
109-
dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr, delay=delay * 2)
129+
dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr)
110130
await dp.start()
111131
for b in [True, False]:
112132
async with Redis(host="127.0.0.1", port=5380, single_connection_client=b) as r:
@@ -120,8 +140,14 @@ async def test_standalone_pipeline(delay, redis_addr):
120140
pipe2.ping()
121141
pipe2.get("foo")
122142

143+
async def op(pipe):
144+
with dp.set_delay(delay * 2):
145+
return await pipe.get(
146+
"foo"
147+
).execute() # <-- this is the operation we want to cancel
148+
123149
dp.send_event.clear()
124-
t = asyncio.create_task(pipe.get("foo").execute())
150+
t = asyncio.create_task(op(pipe))
125151
# wait until task has settled on the read
126152
await dp.send_event.wait()
127153
await asyncio.sleep(0.01)
@@ -153,16 +179,21 @@ async def test_standalone_pipeline(delay, redis_addr):
153179
async def test_cluster(request, redis_addr):
154180

155181
redis_addr = redis_addr[0], 6372 # use the cluster port
156-
dp = DelayProxy(addr=("127.0.0.1", 5381), redis_addr=redis_addr, delay=0.1)
182+
delay = 0.1
183+
dp = DelayProxy(addr=("127.0.0.1", 5381), redis_addr=redis_addr)
157184
await dp.start()
158185

159186
r = RedisCluster.from_url("redis://127.0.0.1:5381")
160187
await r.initialize()
161188
await r.set("foo", "foo")
162189
await r.set("bar", "bar")
163190

191+
async def op(r):
192+
with dp.set_delay(delay):
193+
return await r.get("foo")
194+
164195
dp.send_event.clear()
165-
t = asyncio.create_task(r.get("foo"))
196+
t = asyncio.create_task(op(r))
166197
await dp.send_event.wait()
167198
await asyncio.sleep(0.01)
168199
t.cancel()

0 commit comments

Comments
 (0)