Skip to content

Commit

Permalink
accept sentinel options even with string keys
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lucaong committed May 22, 2020
1 parent f597f21 commit c452de8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/redis/client.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/sentinel_test.rb
Expand Up @@ -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

0 comments on commit c452de8

Please sign in to comment.