Skip to content
Browse files

Added Timecop to specs; Quick specs refactor..

  • Loading branch information...
1 parent f1582b3 commit fa8c98ea88f0e1ec0d9eb893e1655881546296f7 @jeremyvdw committed Mar 3, 2012
Showing with 39 additions and 25 deletions.
  1. +38 −25 spec/simple_reactor/timer_map_spec.rb
  2. +1 −0 spec/spec_helper.rb
View
63 spec/simple_reactor/timer_map_spec.rb
@@ -1,56 +1,69 @@
require 'spec_helper'
describe SimpleReactor::TimerMap do
- let!(:time_now) { Time.now }
let(:proc) { Proc.new { |arg| "Hi #{arg} from the block!" } }
let(:args) { '5' }
before do
- subject[(time_now + 15).to_i] = [proc, 15]
- subject[(time_now + 10).to_i] = [proc, 10]
- subject[(time_now + 5).to_i] = [proc, 5]
+ Timecop.freeze
+
+ [5, 10, 15].sort.each do |time|
+ subject[(Time.now + time).to_i] = [proc, time]
+ end
end
- it "#[]= keeps a @sorted_keys array up to date" do
- subject.sorted_keys.first.should eq (time_now + 5).to_i
+ describe "#[]=" do
+ it "keeps a @sorted_keys array up to date" do
+ subject.sorted_keys.should eq subject.keys.sort
+ end
end
- it "#delete keeps the @sorted_keys up to date" do
- subject.delete (time_now + 5).to_i
- subject.sorted_keys.first.should eq (time_now + 10).to_i
+ describe "#delete" do
+ it "keeps the @sorted_keys up to date" do
+ subject.delete (Time.now + 5).to_i
+ subject.sorted_keys.should eq subject.keys.sort
+ end
end
- it "#next_time returns the @sorted_keys first value" do
- subject.next_time.should eq (time_now + 5).to_i
+ describe "#next_time" do
+ it "returns the @sorted_keys first value" do
+ next_scheduled_at = subject.keys.first
+ subject.next_time.should eq next_scheduled_at
+ end
end
- it "#shift returns the next event (as array)" do
- next_event = [subject.next_time, subject[subject.next_time]]
- subject.shift.should eq next_event
- subject.keys.should_not include next_event
+ describe "#shift" do
+ it "returns the next event (as array)" do
+ next_event = subject.first
+ subject.shift.should eq next_event
+ subject.keys.should_not include next_event
+ end
end
describe "#add_timer" do
+ let(:two_minutes_from_now) { (Time.now + 2).to_i }
+
it "stores a new timer for given seconds later from now" do
- subject.add_timer(2, '5', &proc).should eq [proc, args]
+ subject.add_timer(2, args, &proc).should eq [proc, args]
- subject.next_time.to_i.should eq (time_now + 2).to_i
+ subject.next_time.should eq two_minutes_from_now
subject[subject.next_time].should eq [proc, args]
end
it "stores a new timer for a given Time" do
- subject.add_timer(time_now + 2, '5', &proc).should eq [proc, args]
-
- subject.next_time.to_i.should eq (time_now + 2).to_i
+ subject.add_timer(2, args, &proc).should eq [proc, args]
+ subject.next_time.should eq two_minutes_from_now
subject[subject.next_time].should eq [proc, args]
end
end
- it "#call_next_timer call next-to-come block code with given args" do
- subject.add_timer(1, 'John') do |arg|
- "What are you looking at #{arg} ?"
+ describe "#call_next_timer" do
+ it "calls next-to-come block code with given args" do
+ subject.add_timer(1, 'John') do |arg|
+ "What are you looking at #{arg} ?"
+ end
+ subject.call_next_timer.should eq "What are you looking at John ?"
end
- subject.call_next_timer.should eq "What are you looking at John ?"
end
-end
+end
View
1 spec/spec_helper.rb
@@ -1 +1,2 @@
+require 'timecop'
require 'simple_reactor'

0 comments on commit fa8c98e

Please sign in to comment.
Something went wrong with that request. Please try again.