File tree Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ class Client
17
17
:driver => nil ,
18
18
:id => nil ,
19
19
:tcp_keepalive => 0 ,
20
+ :reconnect_attempts => 1 ,
20
21
:inherit_socket => false
21
22
}
22
23
@@ -309,7 +310,7 @@ def establish_connection
309
310
end
310
311
311
312
def ensure_connected
312
- tries = 0
313
+ attempts = 0
313
314
314
315
begin
315
316
tries += 1
@@ -325,11 +326,16 @@ def ensure_connected
325
326
connect
326
327
end
327
328
329
+ <<<<<<< HEAD
330
+ =======
331
+ attempts += 1
332
+
333
+ >>>>>>> make number of retries in ensure_connected configurable
328
334
yield
329
335
rescue ConnectionError , InheritedError
330
336
disconnect
331
337
332
- if tries < 2 && @reconnect
338
+ if attempts <= @options [ :reconnect_attempts ] && @reconnect
333
339
retry
334
340
else
335
341
raise
Original file line number Diff line number Diff line change @@ -157,7 +157,7 @@ def test_connection_timeout
157
157
end
158
158
end
159
159
160
- def close_on_ping ( seq )
160
+ def close_on_ping ( seq , options = { } )
161
161
$request = 0
162
162
163
163
command = lambda do
@@ -169,7 +169,7 @@ def close_on_ping(seq)
169
169
rv
170
170
end
171
171
172
- redis_mock ( :ping => command , :timeout => 0.1 ) do |redis |
172
+ redis_mock ( { :ping => command } , { :timeout => 0.1 } . merge ( options ) ) do |redis |
173
173
yield ( redis )
174
174
end
175
175
end
@@ -218,6 +218,22 @@ def test_retry_only_once_when_read_raises_econnreset
218
218
end
219
219
end
220
220
221
+ def test_retry_with_custom_reconnect_attempts
222
+ close_on_ping ( [ 0 , 1 ] , :reconnect_attempts => 2 ) do |redis |
223
+ assert_equal "2" , redis . ping
224
+ end
225
+ end
226
+
227
+ def test_retry_with_custom_reconnect_attempts_can_still_fail
228
+ close_on_ping ( [ 0 , 1 , 2 ] , :reconnect_attempts => 2 ) do |redis |
229
+ assert_raise Redis ::ConnectionError do
230
+ redis . ping
231
+ end
232
+
233
+ assert !redis . client . connected?
234
+ end
235
+ end
236
+
221
237
def test_don_t_retry_when_second_read_in_pipeline_raises_econnreset
222
238
close_on_ping ( [ 1 ] ) do |redis |
223
239
assert_raise Redis ::ConnectionError do
You can’t perform that action at this time.
0 commit comments