Skip to content

Commit 3adddbf

Browse files
committed
Redis mock always uses a fresh port.
1 parent b2707a6 commit 3adddbf

File tree

3 files changed

+37
-52
lines changed

3 files changed

+37
-52
lines changed

test/sentinel_command_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def test_sentinel_command_master
1616
}
1717
end
1818

19-
RedisMock.start(handler.call(:s1), {}, 26381) do
20-
redis = Redis.new(:host => "127.0.0.1", :port => 26381)
19+
RedisMock.start(handler.call(:s1)) do |port|
20+
redis = Redis.new(:host => "127.0.0.1", :port => port)
2121

2222
result = redis.sentinel('master', 'master1')
2323
assert_equal result, { "name" => "master1", "ip" => "127.0.0.1" }
@@ -34,8 +34,8 @@ def test_sentinel_command_masters
3434
}
3535
end
3636

37-
RedisMock.start(handler.call(:s1), {}, 26381) do
38-
redis = Redis.new(:host => "127.0.0.1", :port => 26381)
37+
RedisMock.start(handler.call(:s1)) do |port|
38+
redis = Redis.new(:host => "127.0.0.1", :port => port)
3939

4040
result = redis.sentinel('masters')
4141
assert_equal result[0], { "name" => "master1", "ip" => "127.0.0.1", "port" => "6381" }
@@ -53,8 +53,8 @@ def test_sentinel_command_get_master_by_name
5353
}
5454
end
5555

56-
RedisMock.start(handler.call(:s1), {}, 26381) do
57-
redis = Redis.new(:host => "127.0.0.1", :port => 26381)
56+
RedisMock.start(handler.call(:s1)) do |port|
57+
redis = Redis.new(:host => "127.0.0.1", :port => port)
5858

5959
result = redis.sentinel('get-master-addr-by-name', 'master1')
6060
assert_equal result, ["127.0.0.1", "6381"]
@@ -70,8 +70,8 @@ def test_sentinel_command_ckquorum
7070
}
7171
end
7272

73-
RedisMock.start(handler.call(:s1), {}, 26381) do
74-
redis = Redis.new(:host => "127.0.0.1", :port => 26381)
73+
RedisMock.start(handler.call(:s1)) do |port|
74+
redis = Redis.new(:host => "127.0.0.1", :port => port)
7575

7676
result = redis.sentinel('ckquorum', 'master1')
7777
assert_equal result, "OK 2 usable Sentinels. Quorum and failover authorization can be reached"

test/sentinel_test.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def test_sentinel_connection
2424
}
2525
end
2626

27-
RedisMock.start(handler.call(:s1), {}, 0) do |s1_port|
28-
RedisMock.start(handler.call(:s2), {}, 0) do |s2_port|
27+
RedisMock.start(handler.call(:s1)) do |s1_port|
28+
RedisMock.start(handler.call(:s2)) do |s2_port|
2929
sentinels[0][:port] = s1_port
3030
sentinels[1][:port] = s2_port
3131
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
@@ -61,8 +61,8 @@ def test_sentinel_failover
6161
end
6262
}
6363

64-
RedisMock.start(s1, {}, 0) do |s1_port|
65-
RedisMock.start(s2, {}, 0) do |s2_port|
64+
RedisMock.start(s1) do |s1_port|
65+
RedisMock.start(s2) do |s2_port|
6666
sentinels[0][:port] = s1_port
6767
sentinels[1][:port] = s2_port
6868
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
@@ -98,8 +98,8 @@ def test_sentinel_failover_prioritize_healthy_sentinel
9898
end
9999
}
100100

101-
RedisMock.start(s1, {}, 0) do |s1_port|
102-
RedisMock.start(s2, {}, 0) do |s2_port|
101+
RedisMock.start(s1) do |s1_port|
102+
RedisMock.start(s2) do |s2_port|
103103
sentinels[0][:port] = s1_port
104104
sentinels[1][:port] = s2_port
105105
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
@@ -152,8 +152,8 @@ def test_sentinel_with_non_sentinel_options
152152
end
153153
}
154154

155-
RedisMock.start(master, {}, 0) do |master_port|
156-
RedisMock.start(sentinel.call(master_port), {}, 0) do |sen_port|
155+
RedisMock.start(master) do |master_port|
156+
RedisMock.start(sentinel.call(master_port)) do |sen_port|
157157
sentinels[0][:port] = sen_port
158158
redis = Redis.new(:url => "redis://:foo@master1/15", :sentinels => sentinels, :role => :master)
159159

@@ -183,8 +183,8 @@ def test_sentinel_role_mismatch
183183
}
184184

185185
ex = assert_raise(Redis::ConnectionError) do
186-
RedisMock.start(master, {}, 0) do |master_port|
187-
RedisMock.start(sentinel.call(master_port), {}, 0) do |sen_port|
186+
RedisMock.start(master) do |master_port|
187+
RedisMock.start(sentinel.call(master_port)) do |sen_port|
188188
sentinels[0][:port] = sen_port
189189
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
190190

@@ -222,9 +222,9 @@ def test_sentinel_retries
222222
end
223223
}
224224

225-
RedisMock.start(master, {}, 0) do |master_port|
226-
RedisMock.start(handler.call(:s1, master_port), {}, 0) do |s1_port|
227-
RedisMock.start(handler.call(:s2, master_port), {}, 0) do |s2_port|
225+
RedisMock.start(master) do |master_port|
226+
RedisMock.start(handler.call(:s1, master_port)) do |s1_port|
227+
RedisMock.start(handler.call(:s2, master_port)) do |s2_port|
228228
sentinels[0][:port] = s1_port
229229
sentinels[1][:port] = s2_port
230230
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master, :reconnect_attempts => 1)
@@ -239,9 +239,9 @@ def test_sentinel_retries
239239
connections.clear
240240

241241
ex = assert_raise(Redis::CannotConnectError) do
242-
RedisMock.start(master, {}, 0) do |master_port|
243-
RedisMock.start(handler.call(:s1, master_port), {}, 0) do |s1_port|
244-
RedisMock.start(handler.call(:s2, master_port), {}, 0) do |s2_port|
242+
RedisMock.start(master) do |master_port|
243+
RedisMock.start(handler.call(:s1, master_port)) do |s1_port|
244+
RedisMock.start(handler.call(:s2, master_port)) do |s2_port|
245245
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master, :reconnect_attempts => 0)
246246

247247
assert redis.ping

test/support/redis_mock.rb

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
module RedisMock
44
class Server
5-
VERBOSE = false
6-
7-
def initialize(port, options = {}, &block)
8-
@server = TCPServer.new(options[:host] || "127.0.0.1", port)
9-
@server.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
5+
def initialize(options = {}, &block)
6+
@server = TCPServer.new(options[:host] || "127.0.0.1", 0)
7+
@server.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
108
end
119

1210
def port
@@ -17,14 +15,8 @@ def start(&block)
1715
@thread = Thread.new { run(&block) }
1816
end
1917

20-
# Bail out of @server.accept before closing the socket. This is required
21-
# to avoid EADDRINUSE after a couple of iterations.
2218
def shutdown
23-
@thread.terminate if @thread
24-
@server.close if @server
25-
rescue => ex
26-
$stderr.puts "Error closing mock server: #{ex.message}" if VERBOSE
27-
$stderr.puts ex.backtrace if VERBOSE
19+
@thread.kill
2820
end
2921

3022
def run
@@ -39,20 +31,15 @@ def run
3931
end
4032
end
4133
rescue => ex
42-
$stderr.puts "Error running mock server: #{ex.message}" if VERBOSE
43-
$stderr.puts ex.backtrace if VERBOSE
34+
$stderr.puts "Error running mock server: #{ex.message}"
35+
$stderr.puts ex.backtrace
4436
retry
4537
ensure
46-
begin
47-
@server.close
48-
rescue IOError
49-
end
38+
@server.close
5039
end
5140
end
5241
end
5342

54-
MOCK_PORT = 6382
55-
5643
# Starts a mock Redis server in a thread.
5744
#
5845
# The server will use the lambda handler passed as argument to handle
@@ -63,15 +50,13 @@ def run
6350
# # Every connection will be closed immediately
6451
# end
6552
#
66-
def self.start_with_handler(blk, options = {}, port = MOCK_PORT)
67-
server = Server.new(port, options)
53+
def self.start_with_handler(blk, options = {})
54+
server = Server.new(options)
6855
port = server.port
6956

7057
begin
7158
server.start(&blk)
72-
7359
yield(port)
74-
7560
ensure
7661
server.shutdown
7762
end
@@ -82,11 +67,11 @@ def self.start_with_handler(blk, options = {}, port = MOCK_PORT)
8267
# The server will reply with a `+OK` to all commands, but you can
8368
# customize it by providing a hash. For example:
8469
#
85-
# RedisMock.start(:ping => lambda { "+PONG" }) do
86-
# assert_equal "PONG", Redis.new(:port => MOCK_PORT).ping
70+
# RedisMock.start(:ping => lambda { "+PONG" }) do |port|
71+
# assert_equal "PONG", Redis.new(:port => port).ping
8772
# end
8873
#
89-
def self.start(commands, options = {}, port = MOCK_PORT, &blk)
74+
def self.start(commands, options = {}, &blk)
9075
handler = lambda do |session|
9176
while line = session.gets
9277
argv = Array.new(line[1..-3].to_i) do
@@ -129,6 +114,6 @@ def self.start(commands, options = {}, port = MOCK_PORT, &blk)
129114
end
130115
end
131116

132-
start_with_handler(handler, options, port, &blk)
117+
start_with_handler(handler, options, &blk)
133118
end
134119
end

0 commit comments

Comments
 (0)