Class: EventedSpec::SpecHelper::EMExample

Inherits:
EventedExample show all
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

AMQPExample

Constant Summary

Constant Summary

Constants inherited from EventedExample

EventedSpec::SpecHelper::EventedExample::DEFAULT_OPTIONS

Instance Method Summary (collapse)

Methods inherited from EventedExample

#finish_example, #initialize, #run_hooks

Constructor Details

This class inherits a constructor from EventedSpec::SpecHelper::EventedExample

Instance Method Details

- (Object) delayed(delay, &block)



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

- (Object) done(delay = nil)

Breaks the EM event loop and finishes the spec. Done yields to any given block first, then stops EM event loop.

See EventedSpec::SpecHelper::EventedExample#done



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

- (Object) finish_em_loop

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

- (Object) run



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

- (Object) run_em_loop

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

- (Object) timeout(spec_timeout)



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