|
1 | 1 | import asyncio
|
2 | 2 | import contextlib
|
3 |
| -import sys |
4 | 3 |
|
5 | 4 | import pytest
|
6 | 5 |
|
7 | 6 | from redis.asyncio import Redis
|
8 | 7 | from redis.asyncio.cluster import RedisCluster
|
9 | 8 |
|
10 | 9 |
|
| 10 | +@pytest.fixture |
| 11 | +def redis_addr(request): |
| 12 | + redis_url = request.config.getoption("--redis-url") |
| 13 | + assert redis_url.startswith("redis://") |
| 14 | + host, port = redis_url[8:].split(":") |
| 15 | + return host, int(port) |
| 16 | + |
| 17 | + |
11 | 18 | async def pipe(
|
12 | 19 | reader: asyncio.StreamReader,
|
13 | 20 | writer: asyncio.StreamWriter,
|
@@ -68,18 +75,16 @@ async def stop(self):
|
68 | 75 |
|
69 | 76 | @pytest.mark.onlynoncluster
|
70 | 77 | @pytest.mark.parametrize("delay", argvalues=[0.05, 0.5, 1, 2])
|
71 |
| -async def test_standalone(delay): |
| 78 | +async def test_standalone(delay, redis_addr): |
72 | 79 |
|
73 | 80 | # create a tcp socket proxy that relays data to Redis and back,
|
74 | 81 | # inserting 0.1 seconds of delay
|
75 |
| - dp = DelayProxy( |
76 |
| - addr=("localhost", 5380), redis_addr=("localhost", 6379), delay=delay * 2 |
77 |
| - ) |
| 82 | + dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr, delay=delay * 2) |
78 | 83 | await dp.start()
|
79 | 84 |
|
80 | 85 | for b in [True, False]:
|
81 | 86 | # note that we connect to proxy, rather than to Redis directly
|
82 |
| - async with Redis(host="localhost", port=5380, single_connection_client=b) as r: |
| 87 | + async with Redis(host="127.0.0.1", port=5380, single_connection_client=b) as r: |
83 | 88 |
|
84 | 89 | with dp.override():
|
85 | 90 | await r.set("foo", "foo")
|
@@ -107,13 +112,11 @@ async def test_standalone(delay):
|
107 | 112 |
|
108 | 113 | @pytest.mark.onlynoncluster
|
109 | 114 | @pytest.mark.parametrize("delay", argvalues=[0.05, 0.5, 1, 2])
|
110 |
| -async def test_standalone_pipeline(delay): |
111 |
| - dp = DelayProxy( |
112 |
| - addr=("localhost", 5380), redis_addr=("localhost", 6379), delay=delay * 2 |
113 |
| - ) |
| 115 | +async def test_standalone_pipeline(delay, redis_addr): |
| 116 | + dp = DelayProxy(addr=("127.0.0.1", 5380), redis_addr=redis_addr, delay=delay * 2) |
114 | 117 | await dp.start()
|
115 | 118 | for b in [True, False]:
|
116 |
| - async with Redis(host="localhost", port=5380, single_connection_client=b) as r: |
| 119 | + async with Redis(host="127.0.0.1", port=5380, single_connection_client=b) as r: |
117 | 120 | with dp.override():
|
118 | 121 | await r.set("foo", "foo")
|
119 | 122 | await r.set("bar", "bar")
|
@@ -156,12 +159,13 @@ async def test_standalone_pipeline(delay):
|
156 | 159 |
|
157 | 160 |
|
158 | 161 | @pytest.mark.onlycluster
|
159 |
| -async def test_cluster(request): |
| 162 | +async def test_cluster(request, redis_addr): |
160 | 163 |
|
161 |
| - dp = DelayProxy(addr=("localhost", 5381), redis_addr=("localhost", 6372), delay=0.1) |
| 164 | + redis_addr = redis_addr[0], 6372 # use the cluster port |
| 165 | + dp = DelayProxy(addr=("127.0.0.1", 5381), redis_addr=redis_addr, delay=0.1) |
162 | 166 | await dp.start()
|
163 | 167 |
|
164 |
| - r = RedisCluster.from_url("redis://localhost:5381") |
| 168 | + r = RedisCluster.from_url("redis://127.0.0.1:5381") |
165 | 169 | await r.initialize()
|
166 | 170 | with dp.override():
|
167 | 171 | await r.set("foo", "foo")
|
|
0 commit comments