Skip to content

Commit

Permalink
Merge #640 #701
Browse files Browse the repository at this point in the history
640: Add new master_name param for sentinel. Fixes #531 r=badboy

Associated with issue #531.
Based on #534 with the addition of tests.
Didn't know how to keep @nguyenductung 's commit sadly but this PR does fix the issue and is tested.


701: Get tests passing with frozen-string-literals enabled. r=badboy

This one simple change ensure that all string literals can be frozen (as per the optional feature in MRI 2.3 and onwards). @twalpole has (again) beaten me to such a patch (in #590), though mine (again) does not add the pragma comment to all files. Getting their or my PRs merged in would be excellent :)

As an alternative to the pragma comment, I would recommend adding the following to your .travis.yml file to ensure regressions aren't introduced:

```yml
before_script:
- if (ruby -e "exit RUBY_VERSION.to_f >= 2.4"); then export RUBYOPT="--enable-frozen-string-literal"; fi; echo $RUBYOPT
```

This will add the flag when the tests are run on MRI 2.4 or newer (while the feature was introduced in 2.3, it doesn't seem to work reliably until 2.4). Please note: tests will currently fail when this flag is set unless test-unit is also updated (as noted in test-unit/test-unit#149).
  • Loading branch information
not-a-robot[bot] committed Jul 17, 2017
3 parents ea9f1d2 + ef41358 + 8033911 commit 5343b53
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -7,6 +7,7 @@ rvm:
- 2.1
- 2.2
- 2.3.0
- 2.4.1
- jruby-18mode
- jruby-19mode
- jruby-9.0.5.0
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions .travis/Gemfile
Expand Up @@ -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
6 changes: 6 additions & 0 deletions Gemfile
Expand Up @@ -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
1 change: 1 addition & 0 deletions lib/redis.rb
Expand Up @@ -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 = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/client.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/connection/ruby.rb
Expand Up @@ -39,7 +39,7 @@ def initialize(*args)
super(*args)

@timeout = @write_timeout = nil
@buffer = ""
@buffer = "".dup
end

def timeout=(timeout)
Expand Down
2 changes: 1 addition & 1 deletion redis.gemspec
Expand Up @@ -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
26 changes: 26 additions & 0 deletions test/sentinel_test.rb
Expand Up @@ -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}]
Expand Down

0 comments on commit 5343b53

Please sign in to comment.