diff --git a/Gemfile b/Gemfile index ba16eab..61000a6 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 066cff1..289d640 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,6 +24,7 @@ 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 @@ -31,3 +32,4 @@ PLATFORMS DEPENDENCIES rspec (~> 2.7) ruby-debug19 (~> 0.11.6) + timecop (~> 0.3) diff --git a/lib/reactorb/reactor.rb b/lib/reactorb/reactor.rb index 70332ad..d04dd57 100644 --- a/lib/reactorb/reactor.rb +++ b/lib/reactorb/reactor.rb @@ -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 diff --git a/spec/reactor_spec.rb b/spec/reactor_spec.rb index 549b170..af81d37 100644 --- a/spec/reactor_spec.rb +++ b/spec/reactor_spec.rb @@ -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 } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f70d752..3a9ffc3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ require 'rspec' +require 'timecop' require 'reactorb' # Requires supporting files with custom matchers and macros, etc,