Skip to content

Commit 5343b53

Browse files
Merge #640 #701
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).
3 parents ea9f1d2 + ef41358 + 8033911 commit 5343b53

File tree

8 files changed

+46
-3
lines changed

8 files changed

+46
-3
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ rvm:
77
- 2.1
88
- 2.2
99
- 2.3.0
10+
- 2.4.1
1011
- jruby-18mode
1112
- jruby-19mode
1213
- jruby-9.0.5.0
@@ -16,6 +17,9 @@ gemfile: ".travis/Gemfile"
1617

1718
sudo: false
1819

20+
before_script:
21+
- if (ruby -e "exit RUBY_VERSION.to_f >= 2.4"); then export RUBYOPT="--enable-frozen-string-literal"; fi; echo $RUBYOPT
22+
1923
env:
2024
global:
2125
- VERBOSE=true

.travis/Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ when "synchrony"
99
gem "hiredis"
1010
gem "em-synchrony"
1111
end
12+
13+
if RUBY_VERSION.to_f < 1.9
14+
gem 'test-unit', '3.1.5'
15+
else
16+
gem 'test-unit', '>= 3.2.5'
17+
end

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22
source 'https://rubygems.org'
33

44
gemspec
5+
6+
if RUBY_VERSION.to_f < 1.9
7+
gem 'test-unit', '3.1.5'
8+
else
9+
gem 'test-unit', '>= 3.2.5'
10+
end

lib/redis.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def self.current=(redis)
4444
# @option options [Boolean] :inherit_socket (false) Whether to use socket in forked process or not
4545
# @option options [Array] :sentinels List of sentinels to contact
4646
# @option options [Symbol] :role (:master) Role to fetch via Sentinel, either `:master` or `:slave`
47+
# @option options [String] :master_name Master group name according to the `monitor` line in Sentinel config
4748
#
4849
# @return [Redis] a new client instance
4950
def initialize(options = {})

lib/redis/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def initialize(options)
509509

510510
@sentinels = @options.delete(:sentinels).dup
511511
@role = @options.fetch(:role, "master").to_s
512-
@master = @options[:host]
512+
@master = @options[:master_name] || @options[:host]
513513
end
514514

515515
def check(client)

lib/redis/connection/ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize(*args)
3939
super(*args)
4040

4141
@timeout = @write_timeout = nil
42-
@buffer = ""
42+
@buffer = "".dup
4343
end
4444

4545
def timeout=(timeout)

redis.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ Gem::Specification.new do |s|
4040
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
4141

4242
s.add_development_dependency("rake", "<11.0.0")
43-
s.add_development_dependency("test-unit", "3.1.5")
43+
s.add_development_dependency("test-unit", ">= 3.1.5")
4444
end

test/sentinel_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,32 @@ def test_sentinel_role_mismatch
196196
assert_match(/Instance role mismatch/, ex.message)
197197
end
198198

199+
def test_sentinel_master_name
200+
sentinels = [{:host => "127.0.0.1", :port => 26381}]
201+
202+
commands = {
203+
:s1 => [],
204+
}
205+
206+
handler = lambda do |id|
207+
{
208+
:sentinel => lambda do |command, *args|
209+
commands[id] << [command, *args]
210+
["127.0.0.1", "6381"]
211+
end
212+
}
213+
end
214+
215+
RedisMock.start(handler.call(:s1)) do |s1_port|
216+
sentinels[0][:port] = s1_port
217+
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master, :master_name => :new_master)
218+
219+
assert redis.ping
220+
end
221+
222+
assert_equal commands[:s1], [%w[get-master-addr-by-name new_master]]
223+
end
224+
199225
def test_sentinel_retries
200226
sentinels = [{:host => "127.0.0.1", :port => 26381},
201227
{:host => "127.0.0.1", :port => 26382}]

0 commit comments

Comments
 (0)