Class: EventedSpec::SpecHelper::EMExample
- Inherits:
-
EventedExample
- Object
- EventedExample
- EventedSpec::SpecHelper::EMExample
- Defined in:
- lib/evented-spec/evented_example/em_example.rb
Overview
Represents spec running inside EM.run loop. See EventedExample for details and method descriptions.
Direct Known Subclasses
Constant Summary
Constants inherited from EventedExample
EventedSpec::SpecHelper::EventedExample::DEFAULT_OPTIONS
Instance Method Summary collapse
- #delayed(delay, &block) ⇒ Object
-
#done(delay = nil) ⇒ Object
Breaks the EM event loop and finishes the spec.
-
#finish_em_loop ⇒ Object
Stops EM event loop.
- #run ⇒ Object
-
#run_em_loop ⇒ Object
Runs given block inside EM event loop.
- #timeout(spec_timeout) ⇒ Object
Methods inherited from EventedExample
#finish_example, #initialize, #run_hooks
Constructor Details
This class inherits a constructor from EventedSpec::SpecHelper::EventedExample
Instance Method Details
#delayed(delay, &block) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/evented-spec/evented_example/em_example.rb', line 71 def delayed(delay, &block) instance = self if delay EM.add_timer delay, Proc.new { instance.instance_eval(&block) } else instance.instance_eval(&block) end end |
#done(delay = nil) ⇒ Object
Breaks the EM event loop and finishes the spec. Done yields to any given block first, then stops EM event loop.
61 62 63 64 65 66 67 68 |
# File 'lib/evented-spec/evented_example/em_example.rb', line 61 def done(delay = nil) delayed(delay) do yield if block_given? EM.next_tick do finish_em_loop end end end |
#finish_em_loop ⇒ Object
Stops EM event loop. It is called from #done
36 37 38 39 |
# File 'lib/evented-spec/evented_example/em_example.rb', line 36 def finish_em_loop run_hooks :em_after EM.stop_event_loop if EM.reactor_running? end |
#run ⇒ Object
51 52 53 54 55 |
# File 'lib/evented-spec/evented_example/em_example.rb', line 51 def run run_em_loop do @example_group_instance.instance_eval(&@block) end end |
#run_em_loop ⇒ Object
Runs given block inside EM event loop. Double-round exception handler needed because some of the exceptions bubble outside of event loop due to asynchronous nature of evented examples
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/evented-spec/evented_example/em_example.rb', line 10 def run_em_loop begin EM.run do run_hooks :em_before @spec_exception = nil timeout(@opts[:spec_timeout]) if @opts[:spec_timeout] begin yield rescue Exception => e @spec_exception ||= e # p "Inside loop, caught #{@spec_exception.class.name}: #{@spec_exception}" done # We need to properly terminate the event loop end end rescue Exception => e @spec_exception ||= e # p "Outside loop, caught #{@spec_exception.class.name}: #{@spec_exception}" run_hooks :em_after # Event loop broken, but we still need to run em_after hooks ensure finish_example end end |
#timeout(spec_timeout) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/evented-spec/evented_example/em_example.rb', line 42 def timeout(spec_timeout) EM.cancel_timer(@spec_timer) if @spec_timer @spec_timer = EM.add_timer(spec_timeout) do @spec_exception = SpecTimeoutExceededError.new "Example timed out" done end end |