diff --git a/.travis.yml b/.travis.yml index 438d89596..dcfd6e5c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ rvm: - 2.1 - 2.2 - 2.3.0 + - 2.4.1 - jruby-18mode - jruby-19mode - jruby-9.0.5.0 @@ -16,6 +17,9 @@ gemfile: ".travis/Gemfile" sudo: false +before_script: +- if (ruby -e "exit RUBY_VERSION.to_f >= 2.4"); then export RUBYOPT="--enable-frozen-string-literal"; fi; echo $RUBYOPT + env: global: - VERBOSE=true diff --git a/.travis/Gemfile b/.travis/Gemfile index 3fd116321..9e4a237cf 100644 --- a/.travis/Gemfile +++ b/.travis/Gemfile @@ -9,3 +9,9 @@ when "synchrony" gem "hiredis" gem "em-synchrony" end + +if RUBY_VERSION.to_f < 1.9 + gem 'test-unit', '3.1.5' +else + gem 'test-unit', '>= 3.2.5' +end diff --git a/Gemfile b/Gemfile index 73d38bc2e..b14f14cdd 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,9 @@ source 'https://rubygems.org' gemspec + +if RUBY_VERSION.to_f < 1.9 + gem 'test-unit', '3.1.5' +else + gem 'test-unit', '>= 3.2.5' +end diff --git a/lib/redis.rb b/lib/redis.rb index c61d4838e..b83407748 100644 --- a/lib/redis.rb +++ b/lib/redis.rb @@ -44,6 +44,7 @@ def self.current=(redis) # @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not # @option options [Array] :sentinels List of sentinels to contact # @option options [Symbol] :role (:master) Role to fetch via Sentinel, either `:master` or `:slave` + # @option options [String] :master_name Master group name according to the `monitor` line in Sentinel config # # @return [Redis] a new client instance def initialize(options = {}) diff --git a/lib/redis/client.rb b/lib/redis/client.rb index 31be2de33..67aca78cc 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/lib/redis/connection/ruby.rb b/lib/redis/connection/ruby.rb index 96f1d6a59..c01edd913 100644 --- a/lib/redis/connection/ruby.rb +++ b/lib/redis/connection/ruby.rb @@ -39,7 +39,7 @@ def initialize(*args) super(*args) @timeout = @write_timeout = nil - @buffer = "" + @buffer = "".dup end def timeout=(timeout) diff --git a/redis.gemspec b/redis.gemspec index 3099b6d9b..a661410dd 100644 --- a/redis.gemspec +++ b/redis.gemspec @@ -40,5 +40,5 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.add_development_dependency("rake", "<11.0.0") - s.add_development_dependency("test-unit", "3.1.5") + s.add_development_dependency("test-unit", ">= 3.1.5") end 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}]