Last active
December 17, 2015 06:19
-
-
Save nolman/5564693 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EM_TIMEOUT = 5 | |
def run_reactor_until_finished(timeout = EM_TIMEOUT, &block) | |
run_test_reactor(true, timeout, &block) | |
end | |
def run_reactor(&block) | |
run_test_reactor(false, EM_TIMEOUT, &block) | |
end | |
private | |
def run_test_reactor(wait, timeout, &block) | |
EM.synchrony do | |
begin | |
block.call | |
rescue => ex | |
# stop the running the reactor if something happened | |
EM.stop | |
raise ex | |
ensure | |
if wait | |
# Prevent the test from running forever | |
EM.add_timer(timeout) { EM.stop } | |
else | |
EM.stop | |
end | |
end | |
end | |
end | |
#sample test - This is testing some code that is called via a EM.next_tick {} | |
it "sends a message from the cache" do | |
run_reactor_until_finished do | |
subscriber = StubSubscriber.new | |
subscriber.after_data do | |
subscriber.stream.should include "event: HostHealth\n" | |
EM.stop | |
end | |
event_subscriber.subscribe(subscriber) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment