Class: EventMachine::Ventually::Eventually

Inherits:
Object
  • Object
show all
Defined in:
lib/em-ventually/eventually.rb,
lib/em-ventually/eventually/rspec.rb,
lib/em-ventually/eventually/minitest.rb,
lib/em-ventually/eventually/testunit.rb

Direct Known Subclasses

MiniTest, RSpec, TestUnit

Defined Under Namespace

Classes: MiniTest, RSpec, TestUnit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, runner, caller, expectation, opts, block) ⇒ Eventually

Returns a new instance of Eventually.



11
12
13
14
15
16
17
18
# File 'lib/em-ventually/eventually.rb', line 11

def initialize(pool, runner, caller, expectation, opts, block)
  @pool, @runner, @caller, @expectation, @opts, @block = pool, runner, caller, expectation, opts, block
  @count = 0
  @pool.push self
  @total_time = opts && opts[:total] || 1
  @every_time = opts && opts[:every] || 0.1
  run
end

Instance Attribute Details

#expectationObject (readonly)

Returns the value of attribute expectation.



9
10
11
# File 'lib/em-ventually/eventually.rb', line 9

def expectation
  @expectation
end

Instance Method Details

#assert_equal(got) ⇒ Object



20
21
22
# File 'lib/em-ventually/eventually.rb', line 20

def assert_equal(got)
  got == expectation
end

#formatted_message(msg) ⇒ Object



24
25
26
# File 'lib/em-ventually/eventually.rb', line 24

def formatted_message(msg)
  "#{msg} (#{@caller.filename}:#{@caller.line})"
end

#kill_timerObject



28
29
30
# File 'lib/em-ventually/eventually.rb', line 28

def kill_timer
  @kill_timer ||= EM.add_timer(@total_time) { stop(formatted_message("Exceeded time, expected #{expectation.inspect}, last value was #{@last_val.inspect}")) }
end

#process_equality(val) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/em-ventually/eventually.rb', line 48

def process_equality(val)
  if assert_equal(val)
    stop
  else
    @count += 1
    EM.add_timer(@every_time) { run }
  end
end

#report(msg) ⇒ Object



66
67
68
# File 'lib/em-ventually/eventually.rb', line 66

def report(msg)
  STDERR << "Msg: #{msg}\n"
end

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/em-ventually/eventually.rb', line 32

def run
  if @pool.should_run?(self)
    kill_timer
    if @block.arity != 1
      process_equality(@last_val = @block.call)
    else
      @block[proc { |val|
        @last_val = val
        process_equality(val)
      }]
    end
  else
    EM.add_timer(@every_time) { run }
  end
end

#stop(msg = nil) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/em-ventually/eventually.rb', line 57

def stop(msg = nil)
  EM.cancel_timer @kill_timer
  @pool.complete(self)
  report(msg) if msg
  if @pool.empty? && EM.reactor_running?
    EM.stop
  end
end