Skip to content

Commit 310e267

Browse files
committed
Wrap yields in EM.next_tick to fix possible race conditions when calling Sync.reactor.perform after starting reactor. Fix chrismccord#34.
1 parent ccda156 commit 310e267

3 files changed

Lines changed: 4 additions & 2 deletions

File tree

lib/sync/reactor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Reactor
55
# Execute EventMachine bound code block, waiting for reactor to start if
66
# not yet started or reactor thread has gone away
77
def perform
8-
return yield if running?
8+
return EM.next_tick{ yield } if running?
99
cleanly_shutdown_reactor
1010
condition = new_cond
1111
Thread.new do
@@ -19,7 +19,7 @@ def perform
1919
end
2020
synchronize do
2121
condition.wait_until { EM.reactor_running? }
22-
yield
22+
EM.next_tick { yield }
2323
end
2424
end
2525

test/sync/message_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
end
5151

5252
it 'should publish array of messages with single post to faye' do
53+
EM.expects(:next_tick).once.returns true
5354
assert Sync.client.batch_publish(@messages)
5455
end
5556
end

test/sync/reactor_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
ran_block = false
1212
Sync.reactor.perform { ran_block = true}
1313
assert Sync.reactor.running?
14+
sleep 0.1
1415
assert ran_block
1516
end
1617
end

0 commit comments

Comments
 (0)