From c452de8ea10cc709c0a18ffeb3adf2e72b24e2ad Mon Sep 17 00:00:00 2001 From: Luca Ongaro Date: Wed, 16 Mar 2016 11:43:54 +0100 Subject: [PATCH] accept sentinel options even with string keys Before, the sentinel options were assumed to have symbol keys, so string keys were ignored. This is surprising, as other Redis options can be indifferently passed as symbols or strings. Now string keys are allowed too. --- lib/redis/client.rb | 16 +++++++++------- test/sentinel_test.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/lib/redis/client.rb b/lib/redis/client.rb index ea6be56eb..b42998e82 100644 --- a/lib/redis/client.rb +++ b/lib/redis/client.rb @@ -22,7 +22,9 @@ class Client :reconnect_attempts => 1, :reconnect_delay => 0, :reconnect_delay_max => 0.5, - :inherit_socket => false + :inherit_socket => false, + :sentinels => nil, + :role => nil } attr_reader :options @@ -89,7 +91,7 @@ def initialize(options = {}) @pending_reads = 0 @connector = - if options.include?(:sentinels) + if !@options[:sentinels].nil? Connector::Sentinel.new(@options) elsif options.include?(:connector) && options[:connector].respond_to?(:new) options.delete(:connector).new(@options) @@ -539,7 +541,7 @@ def initialize(options) @options[:db] = DEFAULTS.fetch(:db) @sentinels = @options.delete(:sentinels).dup - @role = @options.fetch(:role, "master").to_s + @role = (@options[:role] || "master").to_s @master = @options[:host] end @@ -576,10 +578,10 @@ def resolve def sentinel_detect @sentinels.each do |sentinel| client = Client.new(@options.merge({ - :host => sentinel[:host], - :port => sentinel[:port], - password: sentinel[:password], - :reconnect_attempts => 0, + host: sentinel[:host] || sentinel["host"], + port: sentinel[:port] || sentinel["port"], + password: sentinel[:password] || sentinel["password"], + reconnect_attempts: 0 })) begin diff --git a/test/sentinel_test.rb b/test/sentinel_test.rb index f4aaab2b2..26830b19b 100644 --- a/test/sentinel_test.rb +++ b/test/sentinel_test.rb @@ -346,4 +346,35 @@ def test_sentinel_retries assert_match(/No sentinels available/, ex.message) end + + def test_sentinel_with_string_option_keys + commands = [] + + master = { + role: lambda do + ['master'] + end + } + + sentinel = lambda do |port| + { + sentinel: lambda do |command, *args| + commands << [command, *args] + ['127.0.0.1', port.to_s] + end + } + end + + RedisMock.start(master) do |master_port| + RedisMock.start(sentinel.call(master_port)) do |sen_port| + sentinels = [{ 'host' => '127.0.0.1', 'port' => sen_port }] + + redis = Redis.new(url: 'redis://master1', 'sentinels' => sentinels, 'role' => :master) + + assert redis.ping + end + end + + assert_equal [%w[get-master-addr-by-name master1]], commands + end end