From 27cb96ea4e60da72142a21ab898e8a5b4832172e Mon Sep 17 00:00:00 2001 From: Greg Date: Tue, 18 Oct 2016 16:42:52 +0100 Subject: [PATCH] Add new master_name param for sentinel. Fixes #531 --- lib/redis/client.rb | 2 +- test/sentinel_test.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/redis/client.rb b/lib/redis/client.rb index c867f633b..d3f20eefe 100644 --- a/lib/redis/client.rb +++ b/lib/redis/client.rb @@ -509,7 +509,7 @@ def initialize(options) @sentinels = @options.delete(:sentinels).dup @role = @options.fetch(:role, "master").to_s - @master = @options[:host] + @master = @options[:master_name] || @options[:host] end def check(client) diff --git a/test/sentinel_test.rb b/test/sentinel_test.rb index 823e0762c..7e9b4e005 100644 --- a/test/sentinel_test.rb +++ b/test/sentinel_test.rb @@ -196,6 +196,32 @@ def test_sentinel_role_mismatch assert_match(/Instance role mismatch/, ex.message) end + def test_sentinel_master_name + sentinels = [{:host => "127.0.0.1", :port => 26381}] + + commands = { + :s1 => [], + } + + handler = lambda do |id| + { + :sentinel => lambda do |command, *args| + commands[id] << [command, *args] + ["127.0.0.1", "6381"] + end + } + end + + RedisMock.start(handler.call(:s1)) do |s1_port| + sentinels[0][:port] = s1_port + redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master, :master_name => :new_master) + + assert redis.ping + end + + assert_equal commands[:s1], [%w[get-master-addr-by-name new_master]] + end + def test_sentinel_retries sentinels = [{:host => "127.0.0.1", :port => 26381}, {:host => "127.0.0.1", :port => 26382}]