Permalink
Browse files

Use timecop to freeze time and stub Reactor.now to make specs indepen…

…dent of actual time
  • Loading branch information...
1 parent d57d94d commit 258a4a499f4f8ae785feb8502e6b634d26785566 @gsterndale committed Dec 19, 2011
Showing with 15 additions and 4 deletions.
  1. +1 −0 Gemfile
  2. +2 −0 Gemfile.lock
  3. +5 −1 lib/reactorb/reactor.rb
  4. +6 −3 spec/reactor_spec.rb
  5. +1 −0 spec/spec_helper.rb
View
@@ -2,3 +2,4 @@ source 'http://rubygems.org'
gem 'ruby-debug19', '~> 0.11.6', :require => 'ruby-debug'
gem 'rspec', '~> 2.7'
+gem 'timecop', '~> 0.3'
View
@@ -24,10 +24,12 @@ GEM
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
+ timecop (0.3.5)
PLATFORMS
ruby
DEPENDENCIES
rspec (~> 2.7)
ruby-debug19 (~> 0.11.6)
+ timecop (~> 0.3)
@@ -36,7 +36,7 @@ def at(time_or_number, *args, &block)
end
def call_timers
- now = Time.now.to_i
+ now = self.class.now
while @timers.any? && @timers.first_key <= now
block, args = @timers.shift
block.call(*args)
@@ -47,6 +47,10 @@ def empty?
@timers.empty?
end
+ def self.now
+ Time.now.to_i
+ end
+
end
class ShiftableHash < Hash
@@ -16,6 +16,12 @@
end
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
expect { subject.run{|r| raise "foo" } }.to raise_error "foo"
end
@@ -45,19 +51,16 @@
end
context "with time based events added in the future" do
it "should fire events" do
- now = Time.now.to_i + 1
tally = 0
subject.run do |r|
r.at(now+1){ tally += 1}
r.at(now+2){ tally += 1}
- r.at(now+3){ r.stop }
end
tally.should == 2
end
end
context "stopped with time based events added in the future" do
it "should not fire events" do
- now = Time.now.to_i + 1
tally = 0
subject.run do |r|
r.at(now+1){ r.stop }
View
@@ -1,4 +1,5 @@
require 'rspec'
+require 'timecop'
require 'reactorb'
# Requires supporting files with custom matchers and macros, etc,

0 comments on commit 258a4a4

Please sign in to comment.