Skip to content

Commit

Permalink
Use timecop to freeze time and stub Reactor.now to make specs indepen…
Browse files Browse the repository at this point in the history
…dent of actual time
  • Loading branch information
gsterndale committed Dec 19, 2011
1 parent d57d94d commit 258a4a4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -2,3 +2,4 @@ source 'http://rubygems.org'


gem 'ruby-debug19', '~> 0.11.6', :require => 'ruby-debug' gem 'ruby-debug19', '~> 0.11.6', :require => 'ruby-debug'
gem 'rspec', '~> 2.7' gem 'rspec', '~> 2.7'
gem 'timecop', '~> 0.3'
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -24,10 +24,12 @@ GEM
ruby-debug-base19 (>= 0.11.19) ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5) ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2) archive-tar-minitar (>= 0.5.2)
timecop (0.3.5)


PLATFORMS PLATFORMS
ruby ruby


DEPENDENCIES DEPENDENCIES
rspec (~> 2.7) rspec (~> 2.7)
ruby-debug19 (~> 0.11.6) ruby-debug19 (~> 0.11.6)
timecop (~> 0.3)
6 changes: 5 additions & 1 deletion lib/reactorb/reactor.rb
Expand Up @@ -36,7 +36,7 @@ def at(time_or_number, *args, &block)
end end


def call_timers def call_timers
now = Time.now.to_i now = self.class.now
while @timers.any? && @timers.first_key <= now while @timers.any? && @timers.first_key <= now
block, args = @timers.shift block, args = @timers.shift
block.call(*args) block.call(*args)
Expand All @@ -47,6 +47,10 @@ def empty?
@timers.empty? @timers.empty?
end end


def self.now
Time.now.to_i
end

end end


class ShiftableHash < Hash class ShiftableHash < Hash
Expand Down
9 changes: 6 additions & 3 deletions spec/reactor_spec.rb
Expand Up @@ -16,6 +16,12 @@
end end


describe Reactor, "#run" do describe Reactor, "#run" do
let(:now) { 1324311324 }
before do
start = now
Timecop.freeze(start)
Reactor.stub(:now) { start += 1 }
end
it "should execute block passed" do it "should execute block passed" do
expect { subject.run{|r| raise "foo" } }.to raise_error "foo" expect { subject.run{|r| raise "foo" } }.to raise_error "foo"
end end
Expand Down Expand Up @@ -45,19 +51,16 @@
end end
context "with time based events added in the future" do context "with time based events added in the future" do
it "should fire events" do it "should fire events" do
now = Time.now.to_i + 1
tally = 0 tally = 0
subject.run do |r| subject.run do |r|
r.at(now+1){ tally += 1} r.at(now+1){ tally += 1}
r.at(now+2){ tally += 1} r.at(now+2){ tally += 1}
r.at(now+3){ r.stop }
end end
tally.should == 2 tally.should == 2
end end
end end
context "stopped with time based events added in the future" do context "stopped with time based events added in the future" do
it "should not fire events" do it "should not fire events" do
now = Time.now.to_i + 1
tally = 0 tally = 0
subject.run do |r| subject.run do |r|
r.at(now+1){ r.stop } r.at(now+1){ r.stop }
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -1,4 +1,5 @@
require 'rspec' require 'rspec'
require 'timecop'
require 'reactorb' require 'reactorb'


# Requires supporting files with custom matchers and macros, etc, # Requires supporting files with custom matchers and macros, etc,
Expand Down

0 comments on commit 258a4a4

Please sign in to comment.