5
5
6
6
from redis .asyncio import Redis
7
7
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 )
8
18
9
19
10
20
async def pipe (
@@ -26,6 +36,10 @@ def __init__(self, addr, redis_addr, delay: float):
26
36
self .delay = delay
27
37
28
38
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 ()
29
43
self .server = await asyncio .start_server (self .handle , * self .addr )
30
44
self .ROUTINE = asyncio .create_task (self .server .serve_forever ())
31
45
@@ -47,18 +61,16 @@ async def stop(self):
47
61
48
62
@pytest .mark .onlynoncluster
49
63
@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 ):
51
65
52
66
# create a tcp socket proxy that relays data to Redis and back,
53
67
# 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 )
57
69
await dp .start ()
58
70
59
71
for b in [True , False ]:
60
72
# 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 :
62
74
63
75
await r .set ("foo" , "foo" )
64
76
await r .set ("bar" , "bar" )
@@ -83,13 +95,11 @@ async def test_standalone(delay):
83
95
84
96
@pytest .mark .onlynoncluster
85
97
@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 )
90
100
await dp .start ()
91
101
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 :
93
103
await r .set ("foo" , "foo" )
94
104
await r .set ("bar" , "bar" )
95
105
@@ -122,12 +132,13 @@ async def test_standalone_pipeline(delay):
122
132
123
133
124
134
@pytest .mark .onlycluster
125
- async def test_cluster (request ):
135
+ async def test_cluster (request , redis_addr ):
126
136
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 )
128
139
await dp .start ()
129
140
130
- r = RedisCluster .from_url ("redis://localhost :5381" )
141
+ r = RedisCluster .from_url ("redis://127.0.0.1 :5381" )
131
142
await r .initialize ()
132
143
await r .set ("foo" , "foo" )
133
144
await r .set ("bar" , "bar" )
0 commit comments