Skip to content

Commit fe31b6d

Browse files
committed
Ensure instance role mismatch errors are raised correctly.
1 parent 2c07679 commit fe31b6d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/redis/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def check(client)
501501
end
502502

503503
if role != @role
504-
disconnect
504+
client.disconnect
505505
raise ConnectionError, "Instance role mismatch. Expected #{@role}, got #{role}."
506506
end
507507
end

test/sentinel_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,32 @@ def test_sentinel_with_auth
147147
assert_equal commands[:s1], [%w[get-master-addr-by-name master1]]
148148
assert_equal commands[:m1], [%w[auth foo], %w[role]]
149149
end
150+
151+
def test_sentinel_role_mismatch
152+
sentinels = [{:host => "127.0.0.1", :port => 26381}]
153+
154+
sentinel = {
155+
:sentinel => lambda do |command, *args|
156+
["127.0.0.1", "6382"]
157+
end
158+
}
159+
160+
master = {
161+
:role => lambda do
162+
["slave"]
163+
end
164+
}
165+
166+
ex = assert_raise(Redis::ConnectionError) do
167+
RedisMock.start(master, {}, 6382) do
168+
RedisMock.start(sentinel, {}, 26381) do
169+
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
170+
171+
assert redis.ping
172+
end
173+
end
174+
end
175+
176+
assert_match /Instance role mismatch/, ex.message
177+
end
150178
end

0 commit comments

Comments
 (0)