Skip to content

Commit 0616961

Browse files
committed
Add basic test for Sentinel support.
1 parent 964ccf7 commit 0616961

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

test/sentinel_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# encoding: UTF-8
2+
3+
require File.expand_path("helper", File.dirname(__FILE__))
4+
5+
class SentinalTest < Test::Unit::TestCase
6+
7+
include Helper::Client
8+
9+
def test_sentinel_connection
10+
sentinels = [{:host => "127.0.0.1", :port => 26381},
11+
{:host => "127.0.0.1", :port => 26382}]
12+
13+
commands = {
14+
:s1 => [],
15+
:s2 => [],
16+
}
17+
18+
handler = lambda do |id|
19+
{
20+
:sentinel => lambda do |command, *args|
21+
commands[id] << [command, *args]
22+
["127.0.0.1", "6381"]
23+
end
24+
}
25+
end
26+
27+
RedisMock.start(handler.call(:s1), {}, 26381) do
28+
RedisMock.start(handler.call(:s2), {}, 26382) do
29+
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
30+
31+
assert redis.ping
32+
end
33+
end
34+
35+
assert_equal commands[:s1], [%w[get-master-addr-by-name master1]]
36+
assert_equal commands[:s2], []
37+
end
38+
end

test/support/redis_mock.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ def run
5959
# # Every connection will be closed immediately
6060
# end
6161
#
62-
def self.start_with_handler(blk, options = {})
63-
server = Server.new(MOCK_PORT, options)
62+
def self.start_with_handler(blk, options = {}, port = MOCK_PORT)
63+
server = Server.new(port, options)
6464

6565
begin
6666
server.start(&blk)
6767

68-
yield(MOCK_PORT)
68+
yield(port)
6969

7070
ensure
7171
server.shutdown
@@ -81,7 +81,7 @@ def self.start_with_handler(blk, options = {})
8181
# assert_equal "PONG", Redis.new(:port => MOCK_PORT).ping
8282
# end
8383
#
84-
def self.start(commands, options = {}, &blk)
84+
def self.start(commands, options = {}, port = MOCK_PORT, &blk)
8585
handler = lambda do |session|
8686
while line = session.gets
8787
argv = Array.new(line[1..-3].to_i) do
@@ -116,6 +116,6 @@ def self.start(commands, options = {}, &blk)
116116
end
117117
end
118118

119-
start_with_handler(handler, options, &blk)
119+
start_with_handler(handler, options, port, &blk)
120120
end
121121
end

0 commit comments

Comments
 (0)