File: test.rb

package info (click to toggle)
ruby-em-spec 0.2.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: ruby: 500; makefile: 3
file content (99 lines) | stat: -rw-r--r-- 2,345 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'eventmachine'
require File.dirname(__FILE__) + '/../ext/fiber18'

module EventMachine
  TestTimeoutExceededError = Class.new(RuntimeError)

  module TestHelper

    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

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

    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

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

    private

    def finish_em_spec_fiber
      EM.stop_event_loop if EM.reactor_running?
      @_em_spec_fiber.resume if @_em_spec_fiber.alive?
    end

  end

  module Test

    def self.included(cls)
      cls.class_eval(<<-HERE_DOC, __FILE__, __LINE__)
        include TestHelper

        alias_method :run_without_em, :run
        def run(result, &block)
          em(DefaultTimeout) { run_without_em(result, &block) }
        rescue Exception => e
          if RUBY_VERSION >= "1.9.1"
            result.puke(self.class, @name, e)
          else
            add_error($!)
          end
        end

      HERE_DOC
    end

  end

end