Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use UTC instead of system timezone #2

Merged
merged 1 commit into from

2 participants

@kencheeto

The 'failed' named_scope uses Time.now, which looks at the system timezone. Exposed this by changing a spec:

it 'provides audits enqueued for more than than the expected run duration' do
  Timecop.freeze(@audit.duration.from_now + 1.second) do

to

it 'provides audits enqueued for more than than the expected run duration' do
  Timecop.freeze(@audit.timeout_at + 1.second) do

which has the same behavior but will test against the @audit's time, not from_now which uses system time. An example named_scope that uses Time.now is below:

queue_audit.rb

named_scope :failed, lambda {
  { :conditions => [ 'completed_at is null AND timeout_at < ?', Time.now ], :order => 'timeout_at asc' }
}
  1) Failure:
test_0001_provides_audits_enqueued_for_more_than_than_the_expected_run_duration(Queue Audit::failed) [test/queue_audit_test.rb:73]:
--- expected
+++ actual
@@ -1 +1 @@
-[#<Resque::Durable::QueueAudit>]
+[]

After using Time.now.utc instead of Time.now, tests will pass and behavior will be correct in any system timezone.

test/monitor_test.rb
@@ -1,4 +1,4 @@
-require 'test_helper'
+require File.join(File.dirname(__FILE__), 'test_helper')
@eac Owner
eac added a note

Awesome. Can you revert this line? Then I'll merge in. You can run tests via ruby -I "test"

Oops. Will do.

Reverted- ready to merge :^)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@eac eac merged commit fad0c9e into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 17, 2011
  1. @kencheeto
This page is out of date. Refresh to see the latest.
View
12 lib/resque/durable/queue_audit.rb
@@ -26,7 +26,7 @@ class QueueAudit < ActiveRecord::Base
}
named_scope :failed, lambda {
- { :conditions => [ 'completed_at is null AND timeout_at < ?', Time.now ], :order => 'timeout_at asc' }
+ { :conditions => [ 'completed_at is null AND timeout_at < ?', Time.now.utc ], :order => 'timeout_at asc' }
}
named_scope :complete, lambda {
@@ -80,22 +80,22 @@ def duration
end
def heartbeat!
- update_attribute(:timeout_at, Time.now + duration)
+ update_attribute(:timeout_at, Time.now.utc + duration)
end
def fail!
- update_attribute(:timeout_at, Time.now)
+ update_attribute(:timeout_at, Time.now.utc)
end
def enqueued!
- self.enqueued_at = Time.now
+ self.enqueued_at = Time.now.utc
self.timeout_at = enqueued_at + duration
self.enqueue_count += 1
save!
end
def complete!
- self.completed_at = Time.now
+ self.completed_at = Time.now.utc
save!
end
@@ -104,7 +104,7 @@ def complete?
end
def retryable?
- Time.now > (timeout_at + delay)
+ Time.now.utc > (timeout_at + delay)
end
# 1, 8, 27, 64, 125, 216, etc. minutes.
View
5 test/queue_audit_test.rb
@@ -65,16 +65,17 @@ class QueueAuditTest < MiniTest::Unit::TestCase
describe 'failed' do
before do
@audit.enqueued!
+ @audit.reload
end
it 'provides audits enqueued for more than than the expected run duration' do
- Timecop.freeze(@audit.duration.from_now + 1.second) do
+ Timecop.freeze(@audit.timeout_at + 1.second) do
assert_equal [ @audit ], QueueAudit.failed
end
end
it 'does not provides audits enqueued less than the expected run duration' do
- Timecop.freeze(@audit.duration.from_now - 1.second) do
+ Timecop.freeze(@audit.timeout_at - 1.second) do
assert_equal [], QueueAudit.failed
end
end
View
4 test/test_helper.rb
@@ -10,6 +10,9 @@
database_config = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))
ActiveRecord::Base.establish_connection(database_config['test'])
+ActiveRecord::Base.default_timezone = :utc
+Time.zone = Time.__send__(:get_zone, 'UTC')
+Time.zone_default = Time.__send__(:get_zone, 'UTC')
require 'schema'
module Resque
@@ -54,4 +57,3 @@ def work_queue(name)
worker.process
end
-
Something went wrong with that request. Please try again.