Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept sentinel options even with string keys #599

Merged
merged 1 commit into from May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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