Skip to content

Commit 5fd389b

Browse files
committed
use latches in the consumer test to avoid live lock
1 parent d510574 commit 5fd389b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/resque/consumer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def resume
6363
@paused = false
6464
@latch.release
6565
end
66-
66+
6767
def shutdown
6868
@should_shutdown = true
6969
end

test/consumer_test.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ def run
1414
end
1515
end
1616

17+
class Resumer
18+
LATCHES = {}
19+
20+
def initialize(latch)
21+
@latch_id = latch.object_id
22+
LATCHES[@latch_id] = latch
23+
end
24+
25+
def run
26+
LATCHES[@latch_id].release
27+
end
28+
end
29+
1730
before do
1831
Actionable.ran.clear
1932
end
@@ -59,17 +72,19 @@ def run
5972
q.pop until q.empty?
6073

6174
c = Consumer.new(q, 1)
75+
consumed = Consumer::Latch.new
6276

6377
c.pause
6478
t = Thread.new { c.consume }
65-
# wait until queue blocks
66-
sleep 1
79+
Thread.pass until c.paused?
6780

68-
q << Actionable.new
81+
# A job that unblocks the main thread
82+
q << Resumer.new(consumed)
6983
c.resume
70-
# wait until queue blocks
71-
Thread.pass until t.status != "sleep"
72-
sleep 1
84+
85+
# wait until consumed
86+
consumed.await
87+
7388
assert_equal 0, q.length, 'all jobs should be consumed'
7489
t.kill
7590
end

0 commit comments

Comments
 (0)