Module: RSpec::EM::FakeClock

Defined in:
lib/rspec/eventmachine/fake_clock.rb

Defined Under Namespace

Modules: API Classes: Schedule, Timeout

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_periodic_timer(seconds, proc = nil, &block) ⇒ Object



92
93
94
# File 'lib/rspec/eventmachine/fake_clock.rb', line 92

def self.add_periodic_timer(seconds, proc = nil, &block)
  timer(block || proc, seconds, true)
end

.add_timer(seconds, proc = nil, &block) ⇒ Object



88
89
90
# File 'lib/rspec/eventmachine/fake_clock.rb', line 88

def self.add_timer(seconds, proc = nil, &block)
  timer(block || proc, seconds, false)
end

.cancel_timer(timeout) ⇒ Object



96
97
98
# File 'lib/rspec/eventmachine/fake_clock.rb', line 96

def self.cancel_timer(timeout)
  clear_timeout(timeout)
end

.clear_timeout(timeout) ⇒ Object



100
101
102
# File 'lib/rspec/eventmachine/fake_clock.rb', line 100

def self.clear_timeout(timeout)
  @schedule.delete(timeout)
end

.nowObject



51
52
53
# File 'lib/rspec/eventmachine/fake_clock.rb', line 51

def self.now
  @call_time
end

.resetObject



55
56
57
58
59
# File 'lib/rspec/eventmachine/fake_clock.rb', line 55

def self.reset
  @current_time = Time.now
  @call_time    = @current_time
  @schedule     = Schedule.new
end

.run(timeout) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rspec/eventmachine/fake_clock.rb', line 69

def self.run(timeout)
  @call_time = timeout.time
  timeout.block.call
  
  if timeout.repeat
    timeout.time += timeout.interval
    @schedule = Schedule.new(@schedule)
  else
    clear_timeout(timeout)
  end
end

.tick(seconds) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/rspec/eventmachine/fake_clock.rb', line 61

def self.tick(seconds)
  @current_time += seconds
  while timeout = @schedule.next_scheduled_at(@current_time)
    run(timeout)
  end
  @call_time = @current_time
end

.timer(block, seconds, repeat) ⇒ Object



81
82
83
84
85
86
# File 'lib/rspec/eventmachine/fake_clock.rb', line 81

def self.timer(block, seconds, repeat)
  timeout = Timeout.new(block, seconds, repeat)
  timeout.time = @call_time + seconds
  @schedule.add(timeout)
  timeout
end

Instance Method Details

#clockObject



4
5
6
# File 'lib/rspec/eventmachine/fake_clock.rb', line 4

def clock
  API
end