Skip to content

Commit 89aba81

Browse files
committed
Adding new sentinel integration tests. Fixing the boolean return value type for sentinel's execute_command
1 parent 5d7fd79 commit 89aba81

File tree

4 files changed

+145
-13
lines changed

4 files changed

+145
-13
lines changed

redis/asyncio/sentinel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ async def execute_command(self, *args, **kwargs):
248248
if return_responses:
249249
return responses
250250

251-
return reduce(lambda x, y: x and y, responses)
251+
return bool(reduce(lambda x, y: x and y, responses))
252252

253253
def __repr__(self):
254254
sentinel_addresses = []

redis/sentinel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def execute_command(self, *args, **kwargs):
275275
if return_responses:
276276
return responses
277277

278-
return reduce(lambda x, y: x and y, responses)
278+
return bool(reduce(lambda x, y: x and y, responses))
279279

280280
def __repr__(self):
281281
sentinel_addresses = []

tests/test_asyncio/test_sentinel.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,35 @@ def sentinel(request, cluster):
8484
return Sentinel([("foo", 26379), ("bar", 26379)])
8585

8686

87+
@pytest.fixture()
88+
async def deployed_sentinel(request):
89+
sentinel_ips = request.config.getoption("--sentinels")
90+
sentinel_endpoints = [
91+
(ip.strip(), int(port.strip()))
92+
for ip, port in (endpoint.split(":") for endpoint in sentinel_ips.split(","))
93+
]
94+
kwargs = {}
95+
decode_responses = True
96+
97+
sentinel_kwargs = {"decode_responses": decode_responses}
98+
force_master_ip = "localhost"
99+
100+
protocol = request.config.getoption("--protocol", 2)
101+
102+
sentinel = Sentinel(
103+
sentinel_endpoints,
104+
force_master_ip=force_master_ip,
105+
sentinel_kwargs=sentinel_kwargs,
106+
socket_timeout=0.1,
107+
protocol=protocol,
108+
decode_responses=decode_responses,
109+
**kwargs,
110+
)
111+
yield sentinel
112+
for s in sentinel.sentinels:
113+
await s.close()
114+
115+
87116
@pytest.mark.onlynoncluster
88117
async def test_discover_master(sentinel, master_ip):
89118
address = await sentinel.discover_master("mymaster")
@@ -226,19 +255,22 @@ async def test_slave_round_robin(cluster, sentinel, master_ip):
226255

227256

228257
@pytest.mark.onlynoncluster
229-
async def test_ckquorum(cluster, sentinel):
230-
assert await sentinel.sentinel_ckquorum("mymaster")
258+
async def test_ckquorum(sentinel):
259+
resp = await sentinel.sentinel_ckquorum("mymaster")
260+
assert resp is True
231261

232262

233263
@pytest.mark.onlynoncluster
234-
async def test_flushconfig(cluster, sentinel):
235-
assert await sentinel.sentinel_flushconfig()
264+
async def test_flushconfig(sentinel):
265+
resp = await sentinel.sentinel_flushconfig()
266+
assert resp is True
236267

237268

238269
@pytest.mark.onlynoncluster
239270
async def test_reset(cluster, sentinel):
240271
cluster.master["is_odown"] = True
241-
assert await sentinel.sentinel_reset("mymaster")
272+
resp = await sentinel.sentinel_reset("mymaster")
273+
assert resp is True
242274

243275

244276
@pytest.mark.onlynoncluster
@@ -284,3 +316,37 @@ async def test_repr_correctly_represents_connection_object(sentinel):
284316
str(connection)
285317
== "<redis.asyncio.sentinel.SentinelManagedConnection,host=127.0.0.1,port=6379)>" # noqa: E501
286318
)
319+
320+
321+
# Tests against real sentinel instances
322+
@pytest.mark.onlynoncluster
323+
async def test_get_sentinels(deployed_sentinel):
324+
resps = await deployed_sentinel.sentinel_sentinels("redis-py-test")
325+
326+
# validate that the original command response is returned
327+
assert isinstance(resps, list)
328+
329+
# validate that the command has been executed against all sentinels
330+
# each response from each sentinel is returned
331+
assert len(resps) > 1
332+
333+
334+
@pytest.mark.onlynoncluster
335+
async def test_get_master_addr_by_name(deployed_sentinel):
336+
resps = await deployed_sentinel.sentinel_get_master_addr_by_name("redis-py-test")
337+
338+
# validate that the original command response is returned
339+
assert isinstance(resps, list)
340+
341+
# validate that the command has been executed just once
342+
# when executed once, only one response element is returned
343+
assert len(resps) == 1
344+
345+
assert isinstance(resps[0], tuple)
346+
347+
348+
@pytest.mark.onlynoncluster
349+
async def test_redis_master_usage(deployed_sentinel):
350+
r = await deployed_sentinel.master_for("redis-py-test", db=0)
351+
await r.set("foo", "bar")
352+
assert (await r.get("foo")) == "bar"

tests/test_sentinel.py

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ def sentinel(request, cluster):
8686
return Sentinel([("foo", 26379), ("bar", 26379)])
8787

8888

89+
@pytest.fixture()
90+
def deployed_sentinel(request):
91+
sentinel_ips = request.config.getoption("--sentinels")
92+
sentinel_endpoints = [
93+
(ip.strip(), int(port.strip()))
94+
for ip, port in (endpoint.split(":") for endpoint in sentinel_ips.split(","))
95+
]
96+
kwargs = {}
97+
decode_responses = True
98+
99+
sentinel_kwargs = {"decode_responses": decode_responses}
100+
force_master_ip = "localhost"
101+
102+
protocol = request.config.getoption("--protocol", 2)
103+
104+
sentinel = Sentinel(
105+
sentinel_endpoints,
106+
force_master_ip=force_master_ip,
107+
sentinel_kwargs=sentinel_kwargs,
108+
socket_timeout=0.1,
109+
protocol=protocol,
110+
decode_responses=decode_responses,
111+
**kwargs,
112+
)
113+
yield sentinel
114+
for s in sentinel.sentinels:
115+
s.close()
116+
117+
89118
@pytest.mark.onlynoncluster
90119
def test_discover_master(sentinel, master_ip):
91120
address = sentinel.discover_master("mymaster")
@@ -184,7 +213,7 @@ def test_discover_slaves(cluster, sentinel):
184213

185214

186215
@pytest.mark.onlynoncluster
187-
def test_master_for(cluster, sentinel, master_ip):
216+
def test_master_for(sentinel, master_ip):
188217
master = sentinel.master_for("mymaster", db=9)
189218
assert master.ping()
190219
assert master.connection_pool.master_address == (master_ip, 6379)
@@ -228,19 +257,22 @@ def test_slave_round_robin(cluster, sentinel, master_ip):
228257

229258

230259
@pytest.mark.onlynoncluster
231-
def test_ckquorum(cluster, sentinel):
232-
assert sentinel.sentinel_ckquorum("mymaster")
260+
def test_ckquorum(sentinel):
261+
resp = sentinel.sentinel_ckquorum("mymaster")
262+
assert resp is True
233263

234264

235265
@pytest.mark.onlynoncluster
236-
def test_flushconfig(cluster, sentinel):
237-
assert sentinel.sentinel_flushconfig()
266+
def test_flushconfig(sentinel):
267+
resp = sentinel.sentinel_flushconfig()
268+
assert resp is True
238269

239270

240271
@pytest.mark.onlynoncluster
241272
def test_reset(cluster, sentinel):
242273
cluster.master["is_odown"] = True
243-
assert sentinel.sentinel_reset("mymaster")
274+
resp = sentinel.sentinel_reset("mymaster")
275+
assert resp is True
244276

245277

246278
@pytest.mark.onlynoncluster
@@ -266,3 +298,37 @@ def mock_disconnect():
266298

267299
assert calls == 1
268300
pool.disconnect()
301+
302+
303+
# Tests against real sentinel instances
304+
@pytest.mark.onlynoncluster
305+
def test_get_sentinels(deployed_sentinel):
306+
resps = deployed_sentinel.sentinel_sentinels("redis-py-test")
307+
308+
# validate that the original command response is returned
309+
assert isinstance(resps, list)
310+
311+
# validate that the command has been executed against all sentinels
312+
# each response from each sentinel is returned
313+
assert len(resps) > 1
314+
315+
316+
@pytest.mark.onlynoncluster
317+
def test_get_master_addr_by_name(deployed_sentinel):
318+
resps = deployed_sentinel.sentinel_get_master_addr_by_name("redis-py-test")
319+
320+
# validate that the original command response is returned
321+
assert isinstance(resps, list)
322+
323+
# validate that the command has been executed just once
324+
# when executed once, only one response element is returned
325+
assert len(resps) == 1
326+
327+
assert isinstance(resps[0], tuple)
328+
329+
330+
@pytest.mark.onlynoncluster
331+
def test_redis_master_usage(deployed_sentinel):
332+
r = deployed_sentinel.master_for("redis-py-test", db=0)
333+
r.set("foo", "bar")
334+
assert r.get("foo") == "bar"

0 commit comments

Comments
 (0)