Skip to content

Commit 0b95bc0

Browse files
authored
Merge pull request #816 from supercaracal/use-real-sentinel-for-test
Use real sentinel for test
2 parents 4b086b1 + e9821d0 commit 0b95bc0

File tree

3 files changed

+98
-59
lines changed

3 files changed

+98
-59
lines changed

makefile

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ REDIS_TRIB := ${BUILD_DIR}/src/redis-trib.rb
99
PID_PATH := ${BUILD_DIR}/redis.pid
1010
SOCKET_PATH := ${BUILD_DIR}/redis.sock
1111
PORT := 6381
12+
SLAVE_PORT := 6382
13+
SLAVE_PID_PATH := ${BUILD_DIR}/redis_slave.pid
14+
SLAVE_SOCKET_PATH := ${BUILD_DIR}/redis_slave.sock
15+
SENTINEL_PORTS := 6400 6401 6402
16+
SENTINEL_PID_PATHS := $(addprefix ${TMP}/redis,$(addsuffix .pid,${SENTINEL_PORTS}))
1217
CLUSTER_PORTS := 7000 7001 7002 7003 7004 7005
1318
CLUSTER_PID_PATHS := $(addprefix ${TMP}/redis,$(addsuffix .pid,${CLUSTER_PORTS}))
1419
CLUSTER_CONF_PATHS := $(addprefix ${TMP}/nodes,$(addsuffix .conf,${CLUSTER_PORTS}))
@@ -19,10 +24,20 @@ define kill-redis
1924
endef
2025

2126
all:
27+
make start_all
28+
make test
29+
make stop_all
30+
31+
start_all:
2232
make start
33+
make start_slave
34+
make start_sentinel
2335
make start_cluster
2436
make create_cluster
25-
make test
37+
38+
stop_all:
39+
make stop_sentinel
40+
make stop_slave
2641
make stop
2742
make stop_cluster
2843

@@ -46,6 +61,37 @@ start: ${BINARY}
4661
--port ${PORT} \
4762
--unixsocket ${SOCKET_PATH}
4863

64+
stop_slave:
65+
$(call kill-redis,${SLAVE_PID_PATH})
66+
67+
start_slave: ${BINARY}
68+
${BINARY}\
69+
--daemonize yes\
70+
--pidfile ${SLAVE_PID_PATH}\
71+
--port ${SLAVE_PORT}\
72+
--unixsocket ${SLAVE_SOCKET_PATH}\
73+
--slaveof 127.0.0.1 ${PORT}
74+
75+
stop_sentinel:
76+
$(call kill-redis,${SENTINEL_PID_PATHS})
77+
rm -f ${TMP}/sentinel*.conf || true
78+
79+
start_sentinel: ${BINARY}
80+
for port in ${SENTINEL_PORTS}; do\
81+
conf=${TMP}/sentinel$$port.conf;\
82+
touch $$conf;\
83+
echo '' > $$conf;\
84+
echo 'sentinel monitor master1 127.0.0.1 ${PORT} 2' >> $$conf;\
85+
echo 'sentinel down-after-milliseconds master1 5000' >> $$conf;\
86+
echo 'sentinel failover-timeout master1 30000' >> $$conf;\
87+
echo 'sentinel parallel-syncs master1 1' >> $$conf;\
88+
${BINARY} $$conf\
89+
--daemonize yes\
90+
--pidfile ${TMP}/redis$$port.pid\
91+
--port $$port\
92+
--sentinel;\
93+
done
94+
4995
stop_cluster:
5096
$(call kill-redis,${CLUSTER_PID_PATHS})
5197
rm -f appendonly.aof || true
@@ -71,4 +117,5 @@ create_cluster:
71117
clean:
72118
(test -d ${BUILD_DIR} && cd ${BUILD_DIR}/src && make clean distclean) || true
73119

74-
.PHONY: all test stop start stop_cluster start_cluster create_cluster clean
120+
.PHONY: all test stop start stop_slave start_slave stop_sentinel start_sentinel\
121+
stop_cluster start_cluster create_cluster stop_all start_all clean

test/publish_subscribe_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def test_pubsub_with_channels_and_numsub_subcommnads
119119
Wire.pass while !@subscribed
120120
redis = Redis.new(OPTIONS)
121121
channels_result = redis.pubsub(:channels)
122+
channels_result.delete('__sentinel__:hello')
122123
numsub_result = redis.pubsub(:numsub, 'foo', 'boo')
123124

124125
redis.publish("foo", "s1")

test/sentinel_command_test.rb

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,69 @@
1-
require_relative "helper"
1+
# frozen_string_literal: true
22

3-
class SentinelCommandsTest < Test::Unit::TestCase
3+
require_relative 'helper'
44

5+
# @see https://redis.io/topics/sentinel#sentinel-commands Sentinel commands
6+
class SentinelCommandsTest < Test::Unit::TestCase
57
include Helper::Client
68

7-
def test_sentinel_command_master
9+
MASTER_PORT = PORT.to_s
10+
SLAVE_PORT = '6382'
11+
SENTINEL_PORT = '6400'
12+
MASTER_NAME = 'master1'
13+
LOCALHOST = '127.0.0.1'
814

9-
handler = lambda do |id|
10-
{
11-
:sentinel => lambda do |command, *args|
12-
["name", "master1", "ip", "127.0.0.1"]
13-
end
14-
}
15-
end
15+
def build_sentinel_client
16+
Redis.new(host: LOCALHOST, port: SENTINEL_PORT, timeout: TIMEOUT)
17+
end
1618

17-
RedisMock.start(handler.call(:s1)) do |port|
18-
redis = Redis.new(:host => "127.0.0.1", :port => port)
19+
def test_sentinel_command_master
20+
redis = build_sentinel_client
21+
result = redis.sentinel('master', MASTER_NAME)
1922

20-
result = redis.sentinel('master', 'master1')
21-
assert_equal result, { "name" => "master1", "ip" => "127.0.0.1" }
22-
end
23+
assert_equal result['name'], MASTER_NAME
24+
assert_equal result['ip'], LOCALHOST
2325
end
2426

2527
def test_sentinel_command_masters
28+
redis = build_sentinel_client
29+
result = redis.sentinel('masters')
2630

27-
handler = lambda do |id|
28-
{
29-
:sentinel => lambda do |command, *args|
30-
[%w[name master1 ip 127.0.0.1 port 6381], %w[name master1 ip 127.0.0.1 port 6382]]
31-
end
32-
}
33-
end
34-
35-
RedisMock.start(handler.call(:s1)) do |port|
36-
redis = Redis.new(:host => "127.0.0.1", :port => port)
37-
38-
result = redis.sentinel('masters')
39-
assert_equal result[0], { "name" => "master1", "ip" => "127.0.0.1", "port" => "6381" }
40-
assert_equal result[1], { "name" => "master1", "ip" => "127.0.0.1", "port" => "6382" }
41-
end
31+
assert_equal result[0]['name'], MASTER_NAME
32+
assert_equal result[0]['ip'], LOCALHOST
33+
assert_equal result[0]['port'], MASTER_PORT
4234
end
4335

44-
def test_sentinel_command_get_master_by_name
36+
def test_sentinel_command_slaves
37+
redis = build_sentinel_client
38+
result = redis.sentinel('slaves', MASTER_NAME)
4539

46-
handler = lambda do |id|
47-
{
48-
:sentinel => lambda do |command, *args|
49-
["127.0.0.1", "6381"]
50-
end
51-
}
52-
end
40+
assert_equal result[0]['name'], "#{LOCALHOST}:#{SLAVE_PORT}"
41+
assert_equal result[0]['ip'], LOCALHOST
42+
assert_equal result[0]['port'], SLAVE_PORT
43+
end
5344

54-
RedisMock.start(handler.call(:s1)) do |port|
55-
redis = Redis.new(:host => "127.0.0.1", :port => port)
45+
def test_sentinel_command_sentinels
46+
redis = build_sentinel_client
47+
result = redis.sentinel('sentinels', MASTER_NAME)
5648

57-
result = redis.sentinel('get-master-addr-by-name', 'master1')
58-
assert_equal result, ["127.0.0.1", "6381"]
59-
end
49+
assert_equal result[0]['ip'], LOCALHOST
50+
51+
actual_ports = result.map { |r| r['port'] }.sort
52+
expected_ports = (SENTINEL_PORT.to_i + 1..SENTINEL_PORT.to_i + 2).map(&:to_s)
53+
assert_equal actual_ports, expected_ports
54+
end
55+
56+
def test_sentinel_command_get_master_by_name
57+
redis = build_sentinel_client
58+
result = redis.sentinel('get-master-addr-by-name', MASTER_NAME)
59+
60+
assert_equal result, [LOCALHOST, MASTER_PORT]
6061
end
6162

6263
def test_sentinel_command_ckquorum
63-
handler = lambda do |id|
64-
{
65-
:sentinel => lambda do |command, *args|
66-
"+OK 2 usable Sentinels. Quorum and failover authorization can be reached"
67-
end
68-
}
69-
end
70-
71-
RedisMock.start(handler.call(:s1)) do |port|
72-
redis = Redis.new(:host => "127.0.0.1", :port => port)
73-
74-
result = redis.sentinel('ckquorum', 'master1')
75-
assert_equal result, "OK 2 usable Sentinels. Quorum and failover authorization can be reached"
76-
end
64+
redis = build_sentinel_client
65+
result = redis.sentinel('ckquorum', MASTER_NAME)
66+
67+
assert_equal result, 'OK 3 usable Sentinels. Quorum and failover authorization can be reached'
7768
end
7869
end

0 commit comments

Comments
 (0)