Skip to content

Commit bb07d61

Browse files
committed
Use provided redis address. Bind to IPv4
1 parent 904bef5 commit bb07d61

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

tests/test_asyncio/test_cwe_404.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import asyncio
22
import sys
3+
import urllib.parse
34

45
import pytest
56

67
from redis.asyncio import Redis
78
from redis.asyncio.cluster import RedisCluster
9+
from redis.asyncio.connection import async_timeout
10+
11+
12+
@pytest.fixture
13+
def redis_addr(request):
14+
redis_url = request.config.getoption("--redis-url")
15+
scheme, netloc = urllib.parse.urlparse(redis_url)[:2]
16+
assert scheme == "redis"
17+
if ":" in netloc:
18+
return netloc.split(":")
19+
else:
20+
return netloc, "6379"
821

922

1023
async def pipe(
@@ -26,6 +39,10 @@ def __init__(self, addr, redis_addr, delay: float):
2639
self.delay = delay
2740

2841
async def start(self):
42+
# test that we can connect to redis
43+
async with async_timeout(2):
44+
_, redis_writer = await asyncio.open_connection(*self.redis_addr)
45+
redis_writer.close()
2946
self.server = await asyncio.start_server(self.handle, *self.addr)
3047
self.ROUTINE = asyncio.create_task(self.server.serve_forever())
3148

@@ -47,18 +64,16 @@ async def stop(self):
4764

4865
@pytest.mark.onlynoncluster
4966
@pytest.mark.parametrize("delay", argvalues=[0.05, 0.5, 1, 2])
50-
async def test_standalone(delay):
67+
async def test_standalone(delay, redis_addr):
5168

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

5974
for b in [True, False]:
6075
# 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:
76+
async with Redis(host="127.0.0.1", port=5380, single_connection_client=b) as r:
6277

6378
await r.set("foo", "foo")
6479
await r.set("bar", "bar")
@@ -83,13 +98,11 @@ async def test_standalone(delay):
8398

8499
@pytest.mark.onlynoncluster
85100
@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-
)
101+
async def test_standalone_pipeline(delay, redis_addr):
102+
dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr, delay=delay * 2)
90103
await dp.start()
91104
for b in [True, False]:
92-
async with Redis(host="localhost", port=5380, single_connection_client=b) as r:
105+
async with Redis(host="127.0.0.1", port=5380, single_connection_client=b) as r:
93106
await r.set("foo", "foo")
94107
await r.set("bar", "bar")
95108

@@ -122,12 +135,13 @@ async def test_standalone_pipeline(delay):
122135

123136

124137
@pytest.mark.onlycluster
125-
async def test_cluster(request):
138+
async def test_cluster(request, redis_addr):
126139

127-
dp = DelayProxy(addr=("localhost", 5381), redis_addr=("localhost", 6372), delay=0.1)
140+
redis_addr = redis_addr[0], 6372 # use the cluster port
141+
dp = DelayProxy(addr=("127.0.0.1", 5381), redis_addr=redis_addr, delay=0.1)
128142
await dp.start()
129143

130-
r = RedisCluster.from_url("redis://localhost:5381")
144+
r = RedisCluster.from_url("redis://127.0.0.1:5381")
131145
await r.initialize()
132146
await r.set("foo", "foo")
133147
await r.set("bar", "bar")

0 commit comments

Comments
 (0)