Module: EventMachine::SpecHelper

Included in:
Spec
Defined in:
lib/em-spec/rspec.rb

Constant Summary collapse

SpecTimeoutExceededError =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/em-spec/rspec.rb', line 9

def self.included(cls)
  ::RSpec::Core::ExampleGroup.instance_eval "
  @@_em_default_time_to_finish = nil
  def self.default_timeout(timeout)
    @@_em_default_time_to_finish = timeout
  end
  "
end

Instance Method Details

#doneObject



42
43
44
45
46
# File 'lib/em-spec/rspec.rb', line 42

def done
  EM.next_tick{
    finish_em_spec_fiber
  }
end

#em(time_to_run = @@_em_default_time_to_finish, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/em-spec/rspec.rb', line 23

def em(time_to_run = @@_em_default_time_to_finish, &block)
  EM.run do
    timeout(time_to_run) if time_to_run
    em_spec_exception = nil
    @_em_spec_fiber = Fiber.new do
      begin
        block.call
      rescue Exception => em_spec_exception
        done
      end
      Fiber.yield
    end  

    @_em_spec_fiber.resume

    raise em_spec_exception if em_spec_exception
  end
end

#timeout(time_to_run) ⇒ Object



18
19
20
21
# File 'lib/em-spec/rspec.rb', line 18

def timeout(time_to_run)
  EM.cancel_timer(@_em_timer) if @_em_timer
  @_em_timer = EM.add_timer(time_to_run) { done; raise SpecTimeoutExceededError.new }
end