1
1
import asyncio
2
2
import sys
3
+ import urllib .parse
3
4
4
5
import pytest
5
6
6
7
from redis .asyncio import Redis
7
8
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"
8
21
9
22
10
23
async def pipe (
@@ -26,6 +39,10 @@ def __init__(self, addr, redis_addr, delay: float):
26
39
self .delay = delay
27
40
28
41
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 ()
29
46
self .server = await asyncio .start_server (self .handle , * self .addr )
30
47
self .ROUTINE = asyncio .create_task (self .server .serve_forever ())
31
48
@@ -47,18 +64,16 @@ async def stop(self):
47
64
48
65
@pytest .mark .onlynoncluster
49
66
@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 ):
51
68
52
69
# create a tcp socket proxy that relays data to Redis and back,
53
70
# 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 )
57
72
await dp .start ()
58
73
59
74
for b in [True , False ]:
60
75
# 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 :
62
77
63
78
await r .set ("foo" , "foo" )
64
79
await r .set ("bar" , "bar" )
@@ -83,13 +98,11 @@ async def test_standalone(delay):
83
98
84
99
@pytest .mark .onlynoncluster
85
100
@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 )
90
103
await dp .start ()
91
104
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 :
93
106
await r .set ("foo" , "foo" )
94
107
await r .set ("bar" , "bar" )
95
108
@@ -122,12 +135,13 @@ async def test_standalone_pipeline(delay):
122
135
123
136
124
137
@pytest .mark .onlycluster
125
- async def test_cluster (request ):
138
+ async def test_cluster (request , redis_addr ):
126
139
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 )
128
142
await dp .start ()
129
143
130
- r = RedisCluster .from_url ("redis://localhost :5381" )
144
+ r = RedisCluster .from_url ("redis://127.0.0.1 :5381" )
131
145
await r .initialize ()
132
146
await r .set ("foo" , "foo" )
133
147
await r .set ("bar" , "bar" )
0 commit comments