Permalink
Browse files

using Timecop in the job specs now.

  • Loading branch information...
1 parent 04e4509 commit 6bde639b13ff11f1756eca39e5cde93cdd83a853 @jeffkreeftmeijer committed Jul 10, 2010
Showing with 21 additions and 21 deletions.
  1. +1 −0 Gemfile
  2. +19 −21 spec/job_spec.rb
  3. +1 −0 spec/spec_helper.rb
View
@@ -24,6 +24,7 @@ end
group :test do
gem 'rspec', '1.3.0'
+ gem 'timecop', '0.3.5'
end
gem 'yard', '>= 0.5.2'
View
@@ -1,6 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe 'Navvy::Job' do
+ before do
+ Timecop.freeze(Time.local(2010, 1, 1))
+ end
+
describe '.keep?' do
after(:each) do
Navvy::Job.keep = false
@@ -70,17 +74,14 @@
it 'should set the created_at date' do
Navvy::Job.enqueue(Cow, :speak, true, false)
job = first_job
- job.created_at.should be_instance_of Time
- job.created_at.should <= Time.now
- job.created_at.should > Time.now - 10
+ job.created_at.should == Time.now
end
it 'should set the run_at date' do
Navvy::Job.enqueue(Cow, :speak, true, false)
job = first_job
- job.run_at.should be_instance_of Time
- job.run_at.should <= Time.now
- job.created_at.should > Time.now - 10
+ job.run_at.should == Time.now
+ job.created_at.should == Time.now
end
it 'should return the enqueued job' do
@@ -211,17 +212,17 @@
jobs = Navvy::Job.next
jobs.first.run
job_count.should == 1
- jobs.first.started_at.should be_instance_of Time
- jobs.first.completed_at.should be_instance_of Time
+ jobs.first.started_at.should == Time.now
+ jobs.first.completed_at.should == Time.now
end
- it 'should mark the job as complete when keep has not passed yer' do
+ it 'should mark the job as complete when keep has not passed yet' do
Navvy::Job.keep = (60 * 60)
jobs = Navvy::Job.next
jobs.first.run
job_count.should == 1
- jobs.first.started_at.should be_instance_of Time
- jobs.first.completed_at.should be_instance_of Time
+ jobs.first.started_at.should == Time.now
+ jobs.first.completed_at.should == Time.now
end
it 'should delete the job when the "keep" flag has passed' do
@@ -243,8 +244,8 @@
jobs = Navvy::Job.next
jobs.first.run
jobs.first.exception.should == 'this method is broken'
- jobs.first.started_at.should be_instance_of Time
- jobs.first.failed_at.should be_instance_of Time
+ jobs.first.started_at.should == Time.now
+ jobs.first.failed_at.should == Time.now
end
end
end
@@ -358,25 +359,22 @@
it 'should set the run_at date to about 16 seconds from now' do
failed_job = Navvy::Job.enqueue(Cow, :speak, 'name' => 'Betsy')
failed_job.stub!(:times_failed).and_return 2
- now = Time.now
job = failed_job.retry
- job.run_at.to_i.should == (now + 16).to_i
+ job.run_at.to_i.should == (Time.now + 16).to_i
end
it 'should set the run_at date to about 256 seconds from now' do
failed_job = Navvy::Job.enqueue(Cow, :speak, 'name' => 'Betsy')
failed_job.stub!(:times_failed).and_return 4
- now = Time.now
job = failed_job.retry
- job.run_at.to_i.should == (now + 256).to_i
+ job.run_at.to_i.should == (Time.now + 256).to_i
end
it 'should set the run_at date to about 4096 seconds from now' do
failed_job = Navvy::Job.enqueue(Cow, :speak, 'name' => 'Betsy')
failed_job.stub!(:times_failed).and_return 8
- now = Time.now
job = failed_job.retry
- job.run_at.to_i.should == (now + 4096).to_i
+ job.run_at.to_i.should == (Time.now + 4096).to_i
end
it 'should set the parent_id to the master job id' do
@@ -458,7 +456,7 @@
:completed_at => Time.now
)
- job.duration.should >= 2
+ job.duration.should == 2
end
it 'should return a duration if started_at and failed_at are set' do
@@ -467,7 +465,7 @@
:failed_at => Time.now
)
- job.duration.should >= 3
+ job.duration.should == 3
end
it 'should return 0 if only started_at is set' do
View
@@ -2,6 +2,7 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'navvy'
require 'spec'
+require 'timecop'
require 'spec/autorun'
Spec::Runner.configure do |config|

2 comments on commit 6bde639

You'll likely want to include an after block that restores true time. Otherwise subsequently run specs will still have time frozen.

after do
  Timecop.return
end

Thanks! I've added it -> c5a5b5d :)

Please sign in to comment.