Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Use UTC instead of system timezone #2

Merged
merged 1 commit into from over 2 years ago

2 participants

Kenshiro Nakagawa Eric Chapweske
Kenshiro Nakagawa

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 @@
1  
-require 'test_helper'
  1
+require File.join(File.dirname(__FILE__), 'test_helper')
3
Eric Chapweske Owner
eac added a note November 17, 2011

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
Eric Chapweske eac merged commit fad0c9e into from November 17, 2011
Eric Chapweske eac closed this November 17, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 17, 2011
Kenshiro Nakagawa Use UTC instead of system timezone f016402
This page is out of date. Refresh to see the latest.
12  lib/resque/durable/queue_audit.rb
@@ -26,7 +26,7 @@ class QueueAudit < ActiveRecord::Base
26 26
       }
27 27
 
28 28
       named_scope :failed, lambda {
29  
-        { :conditions => [ 'completed_at is null AND timeout_at < ?', Time.now ], :order => 'timeout_at asc' }
  29
+        { :conditions => [ 'completed_at is null AND timeout_at < ?', Time.now.utc ], :order => 'timeout_at asc' }
30 30
       }
31 31
 
32 32
       named_scope :complete, lambda {
@@ -80,22 +80,22 @@ def duration
80 80
       end
81 81
 
82 82
       def heartbeat!
83  
-        update_attribute(:timeout_at, Time.now + duration)
  83
+        update_attribute(:timeout_at, Time.now.utc + duration)
84 84
       end
85 85
 
86 86
       def fail!
87  
-        update_attribute(:timeout_at, Time.now)
  87
+        update_attribute(:timeout_at, Time.now.utc)
88 88
       end
89 89
 
90 90
       def enqueued!
91  
-        self.enqueued_at    = Time.now
  91
+        self.enqueued_at    = Time.now.utc
92 92
         self.timeout_at     = enqueued_at + duration
93 93
         self.enqueue_count += 1
94 94
         save!
95 95
       end
96 96
 
97 97
       def complete!
98  
-        self.completed_at = Time.now
  98
+        self.completed_at = Time.now.utc
99 99
         save!
100 100
       end
101 101
 
@@ -104,7 +104,7 @@ def complete?
104 104
       end
105 105
 
106 106
       def retryable?
107  
-        Time.now > (timeout_at + delay)
  107
+        Time.now.utc > (timeout_at + delay)
108 108
       end
109 109
 
110 110
       # 1, 8, 27, 64, 125, 216, etc. minutes.
5  test/queue_audit_test.rb
@@ -65,16 +65,17 @@ class QueueAuditTest < MiniTest::Unit::TestCase
65 65
       describe 'failed' do
66 66
         before do
67 67
           @audit.enqueued!
  68
+          @audit.reload
68 69
         end
69 70
 
70 71
         it 'provides audits enqueued for more than than the expected run duration' do
71  
-          Timecop.freeze(@audit.duration.from_now + 1.second) do
  72
+          Timecop.freeze(@audit.timeout_at + 1.second) do
72 73
             assert_equal [ @audit ], QueueAudit.failed
73 74
           end
74 75
         end
75 76
 
76 77
         it 'does not provides audits enqueued less than the expected run duration' do
77  
-          Timecop.freeze(@audit.duration.from_now - 1.second) do
  78
+          Timecop.freeze(@audit.timeout_at - 1.second) do
78 79
             assert_equal [], QueueAudit.failed
79 80
           end
80 81
         end
4  test/test_helper.rb
@@ -10,6 +10,9 @@
10 10
 
11 11
 database_config = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))
12 12
 ActiveRecord::Base.establish_connection(database_config['test'])
  13
+ActiveRecord::Base.default_timezone = :utc
  14
+Time.zone = Time.__send__(:get_zone, 'UTC')
  15
+Time.zone_default = Time.__send__(:get_zone, 'UTC')
13 16
 require 'schema'
14 17
 
15 18
 module Resque
@@ -54,4 +57,3 @@ def work_queue(name)
54 57
   worker.process
55 58
 end
56 59
 
57  
-
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.