Module: EventMachine::TestHelper

Defined in:
lib/em-spec/test.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/em-spec/test.rb', line 9

def self.included(cls)
  cls.class_eval(<<-HERE_DOC, __FILE__, __LINE__)
    DefaultTimeout = nil unless const_defined?(:DefaultTimeout)
    
    def self.default_timeout(timeout)
      self.send(:remove_const, :DefaultTimeout)
      self.send(:const_set, :DefaultTimeout, timeout)
    end
    
    def current_default_timeout
      DefaultTimeout
    end
  HERE_DOC
  
end

Instance Method Details

#done(flunk_reason = nil) ⇒ Object



52
53
54
55
56
57
# File 'lib/em-spec/test.rb', line 52

def done(flunk_reason = nil)
  EM.next_tick{
    @flunk_test = flunk_reason
    finish_em_spec_fiber
  }
end

#em(time_to_run = current_default_timeout, &block) ⇒ Object

Raises:

  • (@flunk_test)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/em-spec/test.rb', line 31

def em(time_to_run = current_default_timeout, &block)
  @flunk_test = nil
  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
  raise(@flunk_test) if @flunk_test
end

#timeout(time_to_run) ⇒ Object



26
27
28
29
# File 'lib/em-spec/test.rb', line 26

def timeout(time_to_run)
  EM.cancel_timer(@_em_timer) if @_em_timer
  @_em_timer = EM.add_timer(time_to_run) { done('timeout exceeded') }
end