Skip to content

Commit d9d2bed

Browse files
committed
Use provided redis address. Bind to IPv4
1 parent db9a85c commit d9d2bed

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

tests/test_asyncio/test_cwe_404.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55

66
from redis.asyncio import Redis
77
from redis.asyncio.cluster import RedisCluster
8+
from redis.asyncio.connection import async_timeout
9+
10+
11+
@pytest.fixture
12+
def redis_addr(request):
13+
redis_url = request.config.getoption("--redis-url")
14+
assert redis_url.startswith("redis://")
15+
host, port = redis_url[8:].split(":")
16+
port = port.split("/")[0]
17+
return host, int(port)
818

919

1020
async def pipe(
@@ -26,6 +36,10 @@ def __init__(self, addr, redis_addr, delay: float):
2636
self.delay = delay
2737

2838
async def start(self):
39+
# test that we can connect to redis
40+
async with async_timeout(2):
41+
_, redis_writer = await asyncio.open_connection(*self.redis_addr)
42+
redis_writer.close()
2943
self.server = await asyncio.start_server(self.handle, *self.addr)
3044
self.ROUTINE = asyncio.create_task(self.server.serve_forever())
3145

@@ -47,18 +61,16 @@ async def stop(self):
4761

4862
@pytest.mark.onlynoncluster
4963
@pytest.mark.parametrize("delay", argvalues=[0.05, 0.5, 1, 2])
50-
async def test_standalone(delay):
64+
async def test_standalone(delay, redis_addr):
5165

5266
# create a tcp socket proxy that relays data to Redis and back,
5367
# inserting 0.1 seconds of delay
54-
dp = DelayProxy(
55-
addr=("localhost", 5380), redis_addr=("localhost", 6379), delay=delay * 2
56-
)
68+
dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr, delay=delay * 2)
5769
await dp.start()
5870

5971
for b in [True, False]:
6072
# note that we connect to proxy, rather than to Redis directly
61-
async with Redis(host="localhost", port=5380, single_connection_client=b) as r:
73+
async with Redis(host="127.0.0.1", port=5380, single_connection_client=b) as r:
6274

6375
await r.set("foo", "foo")
6476
await r.set("bar", "bar")
@@ -83,13 +95,11 @@ async def test_standalone(delay):
8395

8496
@pytest.mark.onlynoncluster
8597
@pytest.mark.parametrize("delay", argvalues=[0.05, 0.5, 1, 2])
86-
async def test_standalone_pipeline(delay):
87-
dp = DelayProxy(
88-
addr=("localhost", 5380), redis_addr=("localhost", 6379), delay=delay * 2
89-
)
98+
async def test_standalone_pipeline(delay, redis_addr):
99+
dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr, delay=delay * 2)
90100
await dp.start()
91101
for b in [True, False]:
92-
async with Redis(host="localhost", port=5380, single_connection_client=b) as r:
102+
async with Redis(host="127.0.0.1", port=5380, single_connection_client=b) as r:
93103
await r.set("foo", "foo")
94104
await r.set("bar", "bar")
95105

@@ -122,12 +132,13 @@ async def test_standalone_pipeline(delay):
122132

123133

124134
@pytest.mark.onlycluster
125-
async def test_cluster(request):
135+
async def test_cluster(request, redis_addr):
126136

127-
dp = DelayProxy(addr=("localhost", 5381), redis_addr=("localhost", 6372), delay=0.1)
137+
redis_addr = redis_addr[0], 6372 # use the cluster port
138+
dp = DelayProxy(addr=("127.0.0.1", 5381), redis_addr=redis_addr, delay=0.1)
128139
await dp.start()
129140

130-
r = RedisCluster.from_url("redis://localhost:5381")
141+
r = RedisCluster.from_url("redis://127.0.0.1:5381")
131142
await r.initialize()
132143
await r.set("foo", "foo")
133144
await r.set("bar", "bar")

0 commit comments

Comments
 (0)