# I''m interested in what you all think of this. Some units/examples
(particularly those interested with timers in a more complex app)
require eventmachine to be running.
# My solution to date has been to completely extract the call to
eventmachine run to the outtermost call I can.
# Several areas are of interest to me in this regard:
# * devs on win32 need a replacement for reactor_running? when on the
0.8.1 gem
# * It''s easier to spin off a thread than to dig into rspec and
''fix''
the runtime orders of things
# * The started thread is always sleeping to ruby(?)
# * This also allows multiple calls to run:
# - We have some integration suite tests which need to start up
several eventmachine apps inside each other.
# - Whilst we have control of our code, a couple of complex
setups end up using say, an EM connection lib inside an
evented_mongrel. Here one must avoid run being called twice.
# - It is possible to do all of the above in logic, but the anti-
pattern results in hard segfaults
#
# * Is it time to make run not clobber itself (and the rest of the
process) when already running?
# - Under what conditions is this specifically not desirable?
#
# I hope someone else might find this useful too.
#!/usr/bin/env ruby
require ''rubygems''
require ''spec''
require ''eventmachine''
require ''thread''
Thread.abort_on_exception = true
class <<EventMachine
def reactor_running?
@reactor_running
end unless defined?(reactor_running?)
alias run! run
def start
unless reactor_running?
job = proc { run! }
@start_run_thread = Thread.new &job
end
@reactor_running = true
reactor_running?
end
def stop!
stop
@reactor_running = false
raise ''EventMachine thread did not stop correctly!''
unless
@start_run_thread.join
end
def run &blk
if start
yield if block_given?
else
raise ''Failed to start EventMachine''
end
end
end
Spec::Runner.configure do |config|
config.before(:all) {
::EventMachine::run
}
config.after(:all) {
::EventMachine::stop!
}
end
describe ''eventmachine monkey patch helper'' do
it ''should have started the reactor'' do
::EventMachine.should be_reactor_running
end
end