Module: EventMachine::TestHelper

Defined in:
lib/em-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
24
25
26
27
28
29
30
31
32
33
# File 'lib/em-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)

      if self.const_defined? :DefaultTimeout
        self.ancestors.each do |cls|
          begin
            cls.send(:remove_const, :DefaultTimeout)
            cls.send(:const_set, :DefaultTimeout, timeout)
            break
          rescue
          end
        end
      else
        self.send(:const_set, :DefaultTimeout, timeout)
      end
    end

    def current_default_timeout
      DefaultTimeout
    end
  HERE_DOC
end

Instance Method Details

#done(flunk_reason = nil) ⇒ Object



61
62
63
64
65
66
# File 'lib/em-test.rb', line 61

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)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/em-test.rb', line 40

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
      @_output = 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
  @_output
end

#timeout(time_to_run) ⇒ Object



35
36
37
38
# File 'lib/em-test.rb', line 35

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