Skip to content

Commit

Permalink
Cleaning up tests and some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bvandenbos committed Nov 2, 2011
1 parent 54075b0 commit 4822308
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 147 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
doc/
pkg
nbproject
Gemfile.lock
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
source :rubygems
gemspec

group :test do
gem "rake"
gem "rack-test"
gem "mocha"
end
39 changes: 22 additions & 17 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,38 @@ PATH
specs:
resque-scheduler (2.0.0.e)
redis (>= 2.0.1)
resque (>= 1.15.0)
resque (>= 1.19.0)
rufus-scheduler

GEM
remote: http://rubygems.org/
specs:
json (1.4.6)
mocha (0.9.9)
rake
rack (1.2.1)
rack-test (0.5.6)
metaclass (0.0.1)
mocha (0.10.0)
metaclass (~> 0.0.1)
multi_json (1.0.3)
rack (1.3.5)
rack-protection (1.1.4)
rack
rack-test (0.6.1)
rack (>= 1.0)
rake (0.8.7)
rake (0.9.2)
redis (2.2.2)
redis-namespace (1.1.0)
redis-namespace (1.0.3)
redis (< 3.0.0)
resque (1.15.0)
json (~> 1.4.6)
redis-namespace (>= 0.10.0)
resque (1.19.0)
multi_json (~> 1.0)
redis-namespace (~> 1.0.2)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
rufus-scheduler (2.0.10)
rufus-scheduler (2.0.12)
tzinfo (>= 0.3.23)
sinatra (1.2.6)
rack (~> 1.1)
tilt (>= 1.2.2, < 2.0)
tilt (1.3.2)
tzinfo (0.3.29)
sinatra (1.3.1)
rack (~> 1.3, >= 1.3.4)
rack-protection (~> 1.1, >= 1.1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
tzinfo (0.3.30)
vegas (0.1.8)
rack (>= 1.0.0)

Expand All @@ -41,4 +45,5 @@ DEPENDENCIES
bundler (>= 1.0.0)
mocha
rack-test
rake
resque-scheduler!
24 changes: 14 additions & 10 deletions lib/resque/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,9 @@ def load_schedule_job(name, config)
interval_types = %w{cron every}
interval_types.each do |interval_type|
if !config[interval_type].nil? && config[interval_type].length > 0
begin
@@scheduled_jobs[name] = rufus_scheduler.send(interval_type, config[interval_type]) do
log! "queueing #{config['class']} (#{name})"
enqueue_from_config(config)
end
rescue Exception => e
log! "#{e.class.name}: #{e.message}"
@@scheduled_jobs[name] = rufus_scheduler.send(interval_type, config[interval_type]) do
log! "queueing #{config['class']} (#{name})"
handle_errors { enqueue_from_config(config) }
end
interval_defined = true
break
Expand Down Expand Up @@ -160,7 +156,7 @@ def enqueue_delayed_items_for_timestamp(timestamp)
handle_shutdown do
if item = Resque.next_item_for_timestamp(timestamp)
log "queuing #{item['class']} [delayed]"
enqueue_from_config(item)
handle_errors { enqueue_from_config(item) }
end
end
# continue processing until there are no more ready items in this timestamp
Expand All @@ -172,6 +168,14 @@ def handle_shutdown
yield
exit if @shutdown
end

def handle_errors
begin
yield
rescue Exception => e
log! "#{e.class.name}: #{e.message}"
end
end

# Enqueues a job based on a config hash
def enqueue_from_config(job_config)
Expand Down Expand Up @@ -200,11 +204,11 @@ def enqueue_from_config(job_config)
if Class === klass
Resque.enqueue(klass, *params)
else
# This will not run the before_hooks in rescue, but will at least
# queue the job.
Resque::Job.create(queue, klass, *params)
end
end
rescue
log! "Failed to enqueue #{klass_name}:\n #{$!}"
end

def rufus_scheduler
Expand Down
4 changes: 1 addition & 3 deletions resque-scheduler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ Gem::Specification.new do |s|
s.require_path = 'lib'

s.add_runtime_dependency(%q<redis>, [">= 2.0.1"])
s.add_runtime_dependency(%q<resque>, [">= 1.15.0"])
s.add_runtime_dependency(%q<resque>, [">= 1.19.0"])
s.add_runtime_dependency(%q<rufus-scheduler>, [">= 0"])
s.add_development_dependency(%q<mocha>, [">= 0"])
s.add_development_dependency(%q<rack-test>, [">= 0"])
end
82 changes: 49 additions & 33 deletions test/delayed_queue_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require File.dirname(__FILE__) + '/test_helper'

class Resque::DelayedQueueTest < Test::Unit::TestCase
context "DelayedQueue" do

def setup
setup do
Resque::Scheduler.mute = true
Resque.redis.flushall
end

def test_enqueue_at_adds_correct_list_and_zset

test "enqueue_at adds correct list and zset" do
timestamp = Time.now - 1 # 1 second ago (in the past, should come out right away)

assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should be empty to start")
Expand All @@ -34,8 +33,7 @@ def test_enqueue_at_adds_correct_list_and_zset
assert_equal(0, Resque.redis.zcard(:delayed_queue_schedule), "delayed queue should be empty")
end

def test_enqueue_at_with_queue_adds_correct_list_and_zset_and_queue

test "enqueue_at with queue adds correct list and zset and queue" do
timestamp = Time.now - 1 # 1 second ago (in the past, should come out right away)

assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should be empty to start")
Expand All @@ -62,7 +60,7 @@ def test_enqueue_at_with_queue_adds_correct_list_and_zset_and_queue
assert_equal(0, Resque.redis.zcard(:delayed_queue_schedule), "delayed queue should be empty")
end

def test_something_in_the_future_doesnt_come_out
test "a job in the future doesn't come out" do
timestamp = Time.now + 600 # 10 minutes from now (in the future, shouldn't come out)

assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should be empty to start")
Expand All @@ -78,7 +76,7 @@ def test_something_in_the_future_doesnt_come_out
assert_nil(read_timestamp, "No timestamps should be ready for queueing")
end

def test_something_in_the_future_comes_out_if_you_want_it_to
test "a job in the future comes out if you want it to" do
timestamp = Time.now + 600 # 10 minutes from now

Resque.enqueue_at(timestamp, SomeIvarJob, "path")
Expand All @@ -88,7 +86,7 @@ def test_something_in_the_future_comes_out_if_you_want_it_to
assert_equal(timestamp.to_i, read_timestamp, "The timestamp we pull out of redis should match the one we put in")
end

def test_enqueue_at_and_enqueue_in_are_equivelent
test "enqueue_at and enqueue_in are equivelent" do
timestamp = Time.now + 60

Resque.enqueue_at(timestamp, SomeIvarJob, "path")
Expand All @@ -98,11 +96,11 @@ def test_enqueue_at_and_enqueue_in_are_equivelent
assert_equal(2, Resque.redis.llen("delayed:#{timestamp.to_i}"), "should have 2 items in the timestamp queue")
end

def test_empty_delayed_queue_peek
test "empty delayed_queue_peek returns empty array" do
assert_equal([], Resque.delayed_queue_peek(0,20))
end

def test_delayed_queue_peek
test "delqyed_queue_peek returns stuff" do
t = Time.now
expected_timestamps = (1..5).to_a.map do |i|
(t + 60 + i).to_i
Expand All @@ -117,38 +115,56 @@ def test_delayed_queue_peek
assert_equal(expected_timestamps[2,3], timestamps)
end

def test_delayed_queue_schedule_size
test "delayed_queue_schedule_size returns correct size" do
assert_equal(0, Resque.delayed_queue_schedule_size)
Resque.enqueue_at(Time.now+60, SomeIvarJob)
assert_equal(1, Resque.delayed_queue_schedule_size)
end

def test_delayed_timestamp_size
test "delayed_timestamp_size returns 0 when nothing is queue" do
t = Time.now + 60
assert_equal(0, Resque.delayed_timestamp_size(t))
end

test "delayed_timestamp_size returns 1 when one thing is queued" do
t = Time.now + 60
assert_equal(0, Resque.delayed_timestamp_size(t))
Resque.enqueue_at(t, SomeIvarJob)
assert_equal(1, Resque.delayed_timestamp_size(t))
assert_equal(0, Resque.delayed_timestamp_size(t.to_i+1))
assert_equal(1, Resque.delayed_timestamp_size(t))
end

def test_delayed_timestamp_peek
test "delayed_timestamp_peek returns empty array when nothings in it" do
t = Time.now + 60
assert_equal([], Resque.delayed_timestamp_peek(t, 0, 1), "make sure it's an empty array, not nil")
end

test "delayed_timestamp_peek returns an array containing one job when one thing is queued" do
t = Time.now + 60
Resque.enqueue_at(t, SomeIvarJob)
assert_equal [{'args' => [], 'class' => 'SomeIvarJob', 'queue' => 'ivar'}], Resque.delayed_timestamp_peek(t, 0, 1)
end

test "delayed_timestamp_peek returns an array of multiple jobs when more than one job is queued" do
t = Time.now + 60
Resque.enqueue_at(t, SomeIvarJob)
assert_equal(1, Resque.delayed_timestamp_peek(t, 0, 1).length)
Resque.enqueue_at(t, SomeIvarJob)
assert_equal(1, Resque.delayed_timestamp_peek(t, 0, 1).length)
assert_equal(2, Resque.delayed_timestamp_peek(t, 0, 3).length)

assert_equal({'args' => [], 'class' => 'SomeIvarJob', 'queue' => 'ivar'}, Resque.delayed_timestamp_peek(t, 0, 1).first)
job = {'args' => [], 'class' => 'SomeIvarJob', 'queue' => 'ivar'}
assert_equal([job, job], Resque.delayed_timestamp_peek(t, 0, 2))
end

test "delayed_timestamp_peek only returns an array of one job if only asked for 1" do
t = Time.now + 60
Resque.enqueue_at(t, SomeIvarJob)
Resque.enqueue_at(t, SomeIvarJob)
job = {'args' => [], 'class' => 'SomeIvarJob', 'queue' => 'ivar'}
assert_equal([job], Resque.delayed_timestamp_peek(t, 0, 1))
end

def test_handle_delayed_items_with_no_items
test "handle_delayed_items with no items" do
Resque::Scheduler.expects(:enqueue).never
Resque::Scheduler.handle_delayed_items
end

def test_handle_delayed_items_with_items
test "handle_delayed_item with items" do
t = Time.now - 60 # in the past
Resque.enqueue_at(t, SomeIvarJob)
Resque.enqueue_at(t, SomeIvarJob)
Expand All @@ -158,7 +174,7 @@ def test_handle_delayed_items_with_items
Resque::Scheduler.handle_delayed_items
end

def test_handle_delayed_items_with_items_in_the_future
test "handle_delayed_items with items in the future" do
t = Time.now + 60 # in the future
Resque.enqueue_at(t, SomeIvarJob)
Resque.enqueue_at(t, SomeIvarJob)
Expand All @@ -168,7 +184,7 @@ def test_handle_delayed_items_with_items_in_the_future
Resque::Scheduler.handle_delayed_items(t)
end

def test_enqueue_delayed_items_for_timestamp
test "enqueue_delayed_items_for_timestamp creates jobs and empties the delayed queue" do
t = Time.now + 60

Resque.enqueue_at(t, SomeIvarJob)
Expand All @@ -183,7 +199,7 @@ def test_enqueue_delayed_items_for_timestamp
assert_equal(0, Resque.delayed_timestamp_peek(t, 0, 3).length)
end

def test_works_with_out_specifying_queue__upgrade_case
test "handle_delayed_items works with out specifying queue (upgrade case)" do
t = Time.now - 60
Resque.delayed_push(t, :class => 'SomeIvarJob')

Expand All @@ -195,7 +211,7 @@ def test_works_with_out_specifying_queue__upgrade_case
Resque::Scheduler.handle_delayed_items
end

def test_clearing_delayed_queue
test "reset_delayed_queue clears the queue" do
t = Time.now + 120
4.times { Resque.enqueue_at(t, SomeIvarJob) }
4.times { Resque.enqueue_at(Time.now + rand(100), SomeIvarJob) }
Expand All @@ -204,14 +220,14 @@ def test_clearing_delayed_queue
assert_equal(0, Resque.delayed_queue_schedule_size)
end

def test_remove_specific_item
test "remove_delayed removes job and returns the count" do
t = Time.now + 120
Resque.enqueue_at(t, SomeIvarJob)

assert_equal(1, Resque.remove_delayed(SomeIvarJob))
end

def test_remove_bogus_item_leaves_the_rest_alone
test "remove_delayed doesn't remove things it shouldn't" do
t = Time.now + 120
Resque.enqueue_at(t, SomeIvarJob, "foo")
Resque.enqueue_at(t, SomeIvarJob, "bar")
Expand All @@ -221,7 +237,7 @@ def test_remove_bogus_item_leaves_the_rest_alone
assert_equal(0, Resque.remove_delayed(SomeIvarJob))
end

def test_remove_specific_item_in_group_of_other_items_at_same_timestamp
test "remove_delayed respected param" do
t = Time.now + 120
Resque.enqueue_at(t, SomeIvarJob, "foo")
Resque.enqueue_at(t, SomeIvarJob, "bar")
Expand All @@ -232,7 +248,7 @@ def test_remove_specific_item_in_group_of_other_items_at_same_timestamp
assert_equal(1, Resque.delayed_queue_schedule_size)
end

def test_remove_specific_item_in_group_of_other_items_at_different_timestamps
test "remove_delayed removes items in different timestamps" do
t = Time.now + 120
Resque.enqueue_at(t, SomeIvarJob, "foo")
Resque.enqueue_at(t + 1, SomeIvarJob, "bar")
Expand All @@ -243,7 +259,7 @@ def test_remove_specific_item_in_group_of_other_items_at_different_timestamps
assert_equal(2, Resque.count_all_scheduled_jobs)
end

def test_invalid_job_class
test "invalid job class" do
assert_raise Resque::NoClassError do
Resque.enqueue_in(10, nil)
end
Expand Down
Loading

0 comments on commit 4822308

Please sign in to comment.