Skip to content

Commit

Permalink
Fix a bug in the offset calculation of .enqueue_at.
Browse files Browse the repository at this point in the history
The old offset calculation was always one second off:

```ruby
irb(main):049:0> offset = ((Time.now + 2) - Time.now)
=> 1.999996
irb(main):004:0> offset.to_i
=> 1
```

Report in rails/rails@40ff508#commitcomment-10860316
  • Loading branch information
senny committed Apr 23, 2015
1 parent 45ee42e commit e66a305
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Unreleased
- Fixed a bug in the offset calculation of `.enqueue_at`.

Version 3.0.0rc
- Improved signal handling

Expand Down
2 changes: 1 addition & 1 deletion lib/queue_classic/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def enqueue(method, *args)
# and args argument must be in the form described in the documentation for
# the #enqueue method.
def enqueue_at(timestamp, method, *args)
offset = Time.at(timestamp) - Time.now
offset = Time.at(timestamp).to_i - Time.now.to_i
enqueue_in(offset, method, *args)
end

Expand Down
3 changes: 3 additions & 0 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ def test_lock_when_empty
end

def test_lock_with_future_job_with_enqueue_in
now = Time.now
QC.enqueue_in(2, "Klass.method")
assert_nil QC.lock
sleep 2
job = QC.lock
assert_equal("Klass.method", job[:method])
assert_equal([], job[:args])
assert_equal((now + 2).to_i, job[:scheduled_at].to_i)
end

def test_lock_with_future_job_with_enqueue_at_with_a_time_object
Expand All @@ -44,6 +46,7 @@ def test_lock_with_future_job_with_enqueue_at_with_a_time_object
job = QC.lock
assert_equal("Klass.method", job[:method])
assert_equal([], job[:args])
assert_equal(future.to_i, job[:scheduled_at].to_i)
end

def test_lock_with_future_job_with_enqueue_at_with_a_float_timestamp
Expand Down

3 comments on commit e66a305

@senny
Copy link
Contributor Author

@senny senny commented on e66a305 Apr 23, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should cover the issue found by @matthewd.

/cc @severin @jipiboily

@severin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for identifying and fixing this @matthewd and @senny!

@jipiboily
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch guys! Thanks!

Please sign in to comment.