Skip to content

Commit fb700d2

Browse files
committed
Add more tests for sentinel failover.
1 parent 0616961 commit fb700d2

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

test/sentinel_test.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,82 @@ def test_sentinel_connection
3535
assert_equal commands[:s1], [%w[get-master-addr-by-name master1]]
3636
assert_equal commands[:s2], []
3737
end
38+
39+
def test_sentinel_failover
40+
sentinels = [{:host => "127.0.0.1", :port => 26381},
41+
{:host => "127.0.0.1", :port => 26382}]
42+
43+
commands = {
44+
:s1 => [],
45+
:s2 => [],
46+
}
47+
48+
s1 = {
49+
:sentinel => lambda do |command, *args|
50+
commands[:s1] << [command, *args]
51+
"$-1" # Nil
52+
end
53+
}
54+
55+
s2 = {
56+
:sentinel => lambda do |command, *args|
57+
commands[:s2] << [command, *args]
58+
["127.0.0.1", "6381"]
59+
end
60+
}
61+
62+
Thread.abort_on_exception = true
63+
64+
RedisMock.start(s1, {}, 26381) do
65+
RedisMock.start(s2, {}, 26382) do
66+
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
67+
68+
assert redis.ping
69+
end
70+
end
71+
72+
assert_equal commands[:s1], [%w[get-master-addr-by-name master1]]
73+
assert_equal commands[:s2], [%w[get-master-addr-by-name master1]]
74+
end
75+
76+
def test_sentinel_failover_prioritize_healthy_sentinel
77+
sentinels = [{:host => "127.0.0.1", :port => 26381},
78+
{:host => "127.0.0.1", :port => 26382}]
79+
80+
commands = {
81+
:s1 => [],
82+
:s2 => [],
83+
}
84+
85+
s1 = {
86+
:sentinel => lambda do |command, *args|
87+
commands[:s1] << [command, *args]
88+
"$-1" # Nil
89+
end
90+
}
91+
92+
s2 = {
93+
:sentinel => lambda do |command, *args|
94+
commands[:s2] << [command, *args]
95+
["127.0.0.1", "6381"]
96+
end
97+
}
98+
99+
Thread.abort_on_exception = true
100+
101+
RedisMock.start(s1, {}, 26381) do
102+
RedisMock.start(s2, {}, 26382) do
103+
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
104+
105+
assert redis.ping
106+
107+
redis.quit
108+
109+
assert redis.ping
110+
end
111+
end
112+
113+
assert_equal commands[:s1], [%w[get-master-addr-by-name master1]]
114+
assert_equal commands[:s2], [%w[get-master-addr-by-name master1], %w[get-master-addr-by-name master1]]
115+
end
38116
end

0 commit comments

Comments
 (0)