Skip to content

Commit 5af8c80

Browse files
committed
Merge pull request #562 from redis/addrinuse-fix
Addrinuse fix
2 parents c9312e0 + bd258e1 commit 5af8c80

File tree

2 files changed

+56
-37
lines changed

2 files changed

+56
-37
lines changed

test/sentinel_test.rb

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

27-
RedisMock.start(handler.call(:s1), {}, 26381) do
28-
RedisMock.start(handler.call(:s2), {}, 26382) do
27+
RedisMock.start(handler.call(:s1), {}, 0) do |s1_port|
28+
RedisMock.start(handler.call(:s2), {}, 0) do |s2_port|
29+
sentinels[0][:port] = s1_port
30+
sentinels[1][:port] = s2_port
2931
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
3032

3133
assert redis.ping
@@ -59,8 +61,10 @@ def test_sentinel_failover
5961
end
6062
}
6163

62-
RedisMock.start(s1, {}, 26381) do
63-
RedisMock.start(s2, {}, 26382) do
64+
RedisMock.start(s1, {}, 0) do |s1_port|
65+
RedisMock.start(s2, {}, 0) do |s2_port|
66+
sentinels[0][:port] = s1_port
67+
sentinels[1][:port] = s2_port
6468
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
6569

6670
assert redis.ping
@@ -94,8 +98,10 @@ def test_sentinel_failover_prioritize_healthy_sentinel
9498
end
9599
}
96100

97-
RedisMock.start(s1, {}, 26381) do
98-
RedisMock.start(s2, {}, 26382) do
101+
RedisMock.start(s1, {}, 0) do |s1_port|
102+
RedisMock.start(s2, {}, 0) do |s2_port|
103+
sentinels[0][:port] = s1_port
104+
sentinels[1][:port] = s2_port
99105
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
100106

101107
assert redis.ping
@@ -118,20 +124,22 @@ def test_sentinel_with_non_sentinel_options
118124
:m1 => []
119125
}
120126

121-
sentinel = {
122-
:auth => lambda do |pass|
123-
commands[:s1] << ["auth", pass]
124-
"-ERR unknown command 'auth'"
125-
end,
126-
:select => lambda do |db|
127-
commands[:s1] << ["select", db]
128-
"-ERR unknown command 'select'"
129-
end,
130-
:sentinel => lambda do |command, *args|
131-
commands[:s1] << [command, *args]
132-
["127.0.0.1", "6382"]
133-
end
134-
}
127+
sentinel = lambda do |port|
128+
{
129+
:auth => lambda do |pass|
130+
commands[:s1] << ["auth", pass]
131+
"-ERR unknown command 'auth'"
132+
end,
133+
:select => lambda do |db|
134+
commands[:s1] << ["select", db]
135+
"-ERR unknown command 'select'"
136+
end,
137+
:sentinel => lambda do |command, *args|
138+
commands[:s1] << [command, *args]
139+
["127.0.0.1", port.to_s]
140+
end
141+
}
142+
end
135143

136144
master = {
137145
:auth => lambda do |pass|
@@ -144,8 +152,9 @@ def test_sentinel_with_non_sentinel_options
144152
end
145153
}
146154

147-
RedisMock.start(master, {}, 6382) do
148-
RedisMock.start(sentinel, {}, 26381) do
155+
RedisMock.start(master, {}, 0) do |master_port|
156+
RedisMock.start(sentinel.call(master_port), {}, 0) do |sen_port|
157+
sentinels[0][:port] = sen_port
149158
redis = Redis.new(:url => "redis://:foo@master1/15", :sentinels => sentinels, :role => :master)
150159

151160
assert redis.ping
@@ -159,11 +168,13 @@ def test_sentinel_with_non_sentinel_options
159168
def test_sentinel_role_mismatch
160169
sentinels = [{:host => "127.0.0.1", :port => 26381}]
161170

162-
sentinel = {
163-
:sentinel => lambda do |command, *args|
164-
["127.0.0.1", "6382"]
165-
end
166-
}
171+
sentinel = lambda do |port|
172+
{
173+
:sentinel => lambda do |command, *args|
174+
["127.0.0.1", port.to_s]
175+
end
176+
}
177+
end
167178

168179
master = {
169180
:role => lambda do
@@ -172,8 +183,9 @@ def test_sentinel_role_mismatch
172183
}
173184

174185
ex = assert_raise(Redis::ConnectionError) do
175-
RedisMock.start(master, {}, 6382) do
176-
RedisMock.start(sentinel, {}, 26381) do
186+
RedisMock.start(master, {}, 0) do |master_port|
187+
RedisMock.start(sentinel.call(master_port), {}, 0) do |sen_port|
188+
sentinels[0][:port] = sen_port
177189
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master)
178190

179191
assert redis.ping
@@ -190,15 +202,15 @@ def test_sentinel_retries
190202

191203
connections = []
192204

193-
handler = lambda do |id|
205+
handler = lambda do |id, port|
194206
{
195207
:sentinel => lambda do |command, *args|
196208
connections << id
197209

198210
if connections.count(id) < 2
199211
:close
200212
else
201-
["127.0.0.1", "6382"]
213+
["127.0.0.1", port.to_s]
202214
end
203215
end
204216
}
@@ -210,9 +222,11 @@ def test_sentinel_retries
210222
end
211223
}
212224

213-
RedisMock.start(master, {}, 6382) do
214-
RedisMock.start(handler.call(:s1), {}, 26381) do
215-
RedisMock.start(handler.call(:s2), {}, 26382) do
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|
228+
sentinels[0][:port] = s1_port
229+
sentinels[1][:port] = s2_port
216230
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master, :reconnect_attempts => 1)
217231

218232
assert redis.ping
@@ -225,9 +239,9 @@ def test_sentinel_retries
225239
connections.clear
226240

227241
ex = assert_raise(Redis::CannotConnectError) do
228-
RedisMock.start(master, {}, 6382) do
229-
RedisMock.start(handler.call(:s1), {}, 26381) do
230-
RedisMock.start(handler.call(:s2), {}, 26382) 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|
231245
redis = Redis.new(:url => "redis://master1", :sentinels => sentinels, :role => :master, :reconnect_attempts => 0)
232246

233247
assert redis.ping

test/support/redis_mock.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def initialize(port, options = {}, &block)
99
@server.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
1010
end
1111

12+
def port
13+
@server.addr[1]
14+
end
15+
1216
def start(&block)
1317
@thread = Thread.new { run(&block) }
1418
end
@@ -61,6 +65,7 @@ def run
6165
#
6266
def self.start_with_handler(blk, options = {}, port = MOCK_PORT)
6367
server = Server.new(port, options)
68+
port = server.port
6469

6570
begin
6671
server.start(&blk)

0 commit comments

Comments
 (0)