File tree Expand file tree Collapse file tree 3 files changed +45
-13
lines changed Expand file tree Collapse file tree 3 files changed +45
-13
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,8 @@ def initialize(options = {})
77
77
@connection = nil
78
78
@command_map = { }
79
79
80
+ @pending_reads = 0
81
+
80
82
if options . include? ( :sentinels )
81
83
@connector = Connector ::Sentinel . new ( @options )
82
84
else
@@ -243,12 +245,15 @@ def io
243
245
244
246
def read
245
247
io do
246
- connection . read
248
+ value = connection . read
249
+ @pending_reads -= 1
250
+ value
247
251
end
248
252
end
249
253
250
254
def write ( command )
251
255
io do
256
+ @pending_reads += 1
252
257
connection . write ( command )
253
258
end
254
259
end
@@ -315,6 +320,7 @@ def establish_connection
315
320
@options [ :port ] = server [ :port ]
316
321
317
322
@connection = @options [ :driver ] . connect ( server )
323
+ @pending_reads = 0
318
324
rescue TimeoutError ,
319
325
Errno ::ECONNREFUSED ,
320
326
Errno ::EHOSTDOWN ,
@@ -326,6 +332,8 @@ def establish_connection
326
332
end
327
333
328
334
def ensure_connected
335
+ disconnect if @pending_reads > 0
336
+
329
337
attempts = 0
330
338
331
339
begin
Original file line number Diff line number Diff line change @@ -186,4 +186,25 @@ def test_config_set
186
186
r . config :set , "timeout" , 300
187
187
end
188
188
end
189
+
190
+ driver ( :ruby , :hiredis ) do
191
+ def test_consistency_on_multithreaded_env
192
+ t = nil
193
+
194
+ commands = {
195
+ :set => lambda { |key , value | t . kill ; "+OK\r \n " } ,
196
+ :incr => lambda { |key | ":1\r \n " } ,
197
+ }
198
+
199
+ redis_mock ( commands ) do |redis |
200
+ t = Thread . new do
201
+ redis . set ( "foo" , "bar" )
202
+ end
203
+
204
+ t . join
205
+
206
+ assert_equal 1 , redis . incr ( "baz" )
207
+ end
208
+ end
209
+ end
189
210
end
Original file line number Diff line number Diff line change @@ -24,20 +24,23 @@ def shutdown
24
24
end
25
25
26
26
def run
27
- loop do
28
- session = @server . accept
29
-
30
- begin
31
- return if yield ( session ) == :exit
32
- ensure
33
- session . close
27
+ begin
28
+ loop do
29
+ session = @server . accept
30
+
31
+ begin
32
+ return if yield ( session ) == :exit
33
+ ensure
34
+ session . close
35
+ end
34
36
end
37
+ rescue => ex
38
+ $stderr. puts "Error running mock server: #{ ex . message } " if VERBOSE
39
+ $stderr. puts ex . backtrace if VERBOSE
40
+ retry
41
+ ensure
42
+ @server . close
35
43
end
36
- rescue => ex
37
- $stderr. puts "Error running mock server: #{ ex . message } " if VERBOSE
38
- $stderr. puts ex . backtrace if VERBOSE
39
- ensure
40
- @server . close
41
44
end
42
45
end
43
46
You can’t perform that action at this time.
0 commit comments