diff --git a/.gitignore b/.gitignore index 9b49d33a..34ac13d6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ doc/ pkg nbproject +Gemfile.lock diff --git a/Gemfile b/Gemfile index e45e65f8..4e457c72 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,8 @@ source :rubygems gemspec + +group :test do + gem "rake" + gem "rack-test" + gem "mocha" +end diff --git a/Gemfile.lock b/Gemfile.lock index 981a8ee4..510e2846 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -41,4 +45,5 @@ DEPENDENCIES bundler (>= 1.0.0) mocha rack-test + rake resque-scheduler! diff --git a/lib/resque/scheduler.rb b/lib/resque/scheduler.rb index 5808f789..e9277a51 100644 --- a/lib/resque/scheduler.rb +++ b/lib/resque/scheduler.rb @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/resque-scheduler.gemspec b/resque-scheduler.gemspec index 68e91deb..0d78bd05 100644 --- a/resque-scheduler.gemspec +++ b/resque-scheduler.gemspec @@ -21,8 +21,6 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.add_runtime_dependency(%q, [">= 2.0.1"]) - s.add_runtime_dependency(%q, [">= 1.15.0"]) + s.add_runtime_dependency(%q, [">= 1.19.0"]) s.add_runtime_dependency(%q, [">= 0"]) - s.add_development_dependency(%q, [">= 0"]) - s.add_development_dependency(%q, [">= 0"]) end diff --git a/test/delayed_queue_test.rb b/test/delayed_queue_test.rb index 637d00a2..1a61c991 100644 --- a/test/delayed_queue_test.rb +++ b/test/delayed_queue_test.rb @@ -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") @@ -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") @@ -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") @@ -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") @@ -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") @@ -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 @@ -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) @@ -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) @@ -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) @@ -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') @@ -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) } @@ -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") @@ -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") @@ -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") @@ -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 diff --git a/test/scheduler_args_test.rb b/test/scheduler_args_test.rb index bd8f1426..7439f333 100644 --- a/test/scheduler_args_test.rb +++ b/test/scheduler_args_test.rb @@ -1,7 +1,70 @@ require File.dirname(__FILE__) + '/test_helper' context "scheduling jobs with arguments" do - setup { Resque::Scheduler.clear_schedule! } + + setup do + Resque::Scheduler.clear_schedule! + Resque::Scheduler.dynamic = false + Resque::Scheduler.mute = true + end + + test "enqueue_from_config puts stuff in resque without class loaded" do + Resque::Job.stubs(:create).once.returns(true).with('joes_queue', 'UndefinedJob', '/tmp') + Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'UndefinedJob', 'args' => "/tmp", 'queue' => 'joes_queue') + end + + test "enqueue_from_config with_every_syntax" do + Resque::Job.stubs(:create).once.returns(true).with('james_queue', 'JamesJob', '/tmp') + Resque::Scheduler.enqueue_from_config('every' => '1m', 'class' => 'JamesJob', 'args' => '/tmp', 'queue' => 'james_queue') + end + + test "enqueue_from_config puts jobs in the resque queue" do + Resque::Job.stubs(:create).once.returns(true).with(:ivar, SomeIvarJob, '/tmp') + Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp") + end + + test "enqueue_from_config with custom_class_job in resque" do + FakeCustomJobClass.stubs(:scheduled).once.returns(true).with(:ivar, 'SomeIvarJob', '/tmp') + Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'custom_job_class' => 'FakeCustomJobClass', 'args' => "/tmp") + end + + test "enqueue_from_config puts stuff in resquewhen rails_env matches" do + # The job should be loaded : its rails_env config matches the RAILS_ENV variable: + ENV['RAILS_ENV'] = 'production' + assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) + + Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'production'}} + Resque::Scheduler.load_schedule! + assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size) + + # we allow multiple rails_env definition, it should work also: + Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging, production'}} + Resque::Scheduler.load_schedule! + assert_equal(2, Resque::Scheduler.rufus_scheduler.all_jobs.size) + end + + test "enqueue_from_config doesnt put stuff in resque when rails_env doesnt match" do + # RAILS_ENV is not set: + assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) + Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}} + Resque::Scheduler.load_schedule! + assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) + + # SET RAILS_ENV to a common value: + ENV['RAILS_ENV'] = 'production' + Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}} + Resque::Scheduler.load_schedule! + assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) + end + + test "enqueue_from_config when rails env arg is not set" do + # The job should be loaded, since a missing rails_env means ALL envs. + ENV['RAILS_ENV'] = 'production' + assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) + Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}} + Resque::Scheduler.load_schedule! + assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size) + end test "calls the worker without arguments when 'args' is missing from the config" do Resque::Scheduler.enqueue_from_config(YAML.load(<<-YAML)) diff --git a/test/scheduler_test.rb b/test/scheduler_test.rb index f7a25d53..aa6d0036 100644 --- a/test/scheduler_test.rb +++ b/test/scheduler_test.rb @@ -1,12 +1,8 @@ require File.dirname(__FILE__) + '/test_helper' -class Resque::SchedulerTest < Test::Unit::TestCase +context "Resque::Scheduler" do - class FakeJob - def self.scheduled(queue, klass, *args); end - end - - def setup + setup do Resque::Scheduler.dynamic = false Resque.redis.del(:schedules) Resque.redis.del(:schedules_changed) @@ -15,70 +11,7 @@ def setup Resque::Scheduler.send(:class_variable_set, :@@scheduled_jobs, {}) end - def test_enqueue_from_config_puts_stuff_in_the_resque_queue_without_class_loaded - Resque::Job.stubs(:create).once.returns(true).with('joes_queue', 'BigJoesJob', '/tmp') - Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'BigJoesJob', 'args' => "/tmp", 'queue' => 'joes_queue') - end - - def test_enqueue_from_config_with_every_syntax - Resque::Job.stubs(:create).once.returns(true).with('james_queue', 'JamesJob', '/tmp') - Resque::Scheduler.enqueue_from_config('every' => '1m', 'class' => 'JamesJob', 'args' => '/tmp', 'queue' => 'james_queue') - end - - def test_enqueue_from_config_doesnt_crash_on_exception_when_enqueueing - Resque::Job.stubs(:create).raises(Resque::NoQueueError, 'test exception').with(:ivar, 'SomeIvarJob', '/tmp') - Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp") - end - - def test_enqueue_from_config_puts_stuff_in_the_resque_queue - Resque::Job.stubs(:create).once.returns(true).with(:ivar, SomeIvarJob, '/tmp') - Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp") - end - - def test_enqueue_from_config_with_custom_class_job_in_the_resque_queue - FakeJob.stubs(:scheduled).once.returns(true).with(:ivar, 'SomeIvarJob', '/tmp') - Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'custom_job_class' => 'Resque::SchedulerTest::FakeJob', 'args' => "/tmp") - end - - def test_enqueue_from_config_puts_stuff_in_the_resque_queue_when_env_match - # The job should be loaded : its rails_env config matches the RAILS_ENV variable: - ENV['RAILS_ENV'] = 'production' - assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) - - Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'production'}} - Resque::Scheduler.load_schedule! - assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size) - - # we allow multiple rails_env definition, it should work also: - Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging, production'}} - Resque::Scheduler.load_schedule! - assert_equal(2, Resque::Scheduler.rufus_scheduler.all_jobs.size) - end - - def test_enqueue_from_config_dont_puts_stuff_in_the_resque_queue_when_env_doesnt_match - # RAILS_ENV is not set: - assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) - Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}} - Resque::Scheduler.load_schedule! - assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) - - # SET RAILS_ENV to a common value: - ENV['RAILS_ENV'] = 'production' - Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp", 'rails_env' => 'staging'}} - Resque::Scheduler.load_schedule! - assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) - end - - def test_enqueue_from_config_when_rails_env_arg_is_not_set - # The job should be loaded, since a missing rails_env means ALL envs. - ENV['RAILS_ENV'] = 'production' - assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) - Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}} - Resque::Scheduler.load_schedule! - assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size) - end - - def test_enqueue_constantizes + test "enqueue constantizes" do # The job should be loaded, since a missing rails_env means ALL envs. ENV['RAILS_ENV'] = 'production' config = {'cron' => "* * * * *", 'class' => 'SomeRealClass', 'args' => "/tmp"} @@ -86,7 +19,7 @@ def test_enqueue_constantizes Resque::Scheduler.enqueue_from_config(config) end - def test_enqueue_runs_hooks + test "enqueue runs hooks" do # The job should be loaded, since a missing rails_env means ALL envs. ENV['RAILS_ENV'] = 'production' config = {'cron' => "* * * * *", 'class' => 'SomeRealClass', 'args' => "/tmp"} @@ -97,7 +30,7 @@ def test_enqueue_runs_hooks Resque::Scheduler.enqueue_from_config(config) end - def test_config_makes_it_into_the_rufus_scheduler + test "config makes it into the rufus_scheduler" do assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}} @@ -107,7 +40,7 @@ def test_config_makes_it_into_the_rufus_scheduler assert Resque::Scheduler.scheduled_jobs.include?(:some_ivar_job) end - def test_can_reload_schedule + test "can reload schedule" do Resque::Scheduler.dynamic = true Resque.schedule = {"some_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}} @@ -129,7 +62,7 @@ def test_can_reload_schedule assert Resque::Scheduler.scheduled_jobs.include?("some_ivar_job2") end - def test_load_schedule_job + test "load_schedule_job loads a schedule" do Resque::Scheduler.load_schedule_job("some_ivar_job", {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}) assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size) @@ -137,7 +70,7 @@ def test_load_schedule_job assert Resque::Scheduler.scheduled_jobs.keys.include?("some_ivar_job") end - def test_load_schedule_job_with_no_cron + test "load_schedule_job without cron" do Resque::Scheduler.load_schedule_job("some_ivar_job", {'class' => 'SomeIvarJob', 'args' => "/tmp"}) assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) @@ -145,7 +78,7 @@ def test_load_schedule_job_with_no_cron assert !Resque::Scheduler.scheduled_jobs.keys.include?("some_ivar_job") end - def test_load_schedule_job_with_blank_cron + test "load_schedule_job with an empty cron" do Resque::Scheduler.load_schedule_job("some_ivar_job", {'cron' => '', 'class' => 'SomeIvarJob', 'args' => "/tmp"}) assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size) @@ -153,7 +86,7 @@ def test_load_schedule_job_with_blank_cron assert !Resque::Scheduler.scheduled_jobs.keys.include?("some_ivar_job") end - def test_update_schedule + test "update_schedule" do Resque::Scheduler.dynamic = true Resque.schedule = { "some_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}, @@ -187,7 +120,7 @@ def test_update_schedule assert_equal 0, Resque.redis.scard(:schedules_changed) end - def test_update_schedule_with_mocks + test "update_schedule with mocks" do Resque::Scheduler.dynamic = true Resque.schedule = { "some_ivar_job" => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}, @@ -223,7 +156,7 @@ def test_update_schedule_with_mocks assert_equal 0, Resque.redis.scard(:schedules_changed) end - def test_set_schedules + test "schedule= sets the schedule" do Resque::Scheduler.dynamic = true Resque.schedule = {"my_ivar_job" => { 'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/75" @@ -232,7 +165,7 @@ def test_set_schedules Resque.decode(Resque.redis.hget(:schedules, "my_ivar_job"))) end - def test_set_schedule + test "set_schedule can set an individual schedule" do Resque.set_schedule("some_ivar_job", { 'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/22" }) @@ -241,7 +174,7 @@ def test_set_schedule assert Resque.redis.sismember(:schedules_changed, "some_ivar_job") end - def test_get_schedule + test "get_schedule returns a schedule" do Resque.redis.hset(:schedules, "some_ivar_job2", Resque.encode( {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/33"} )) @@ -249,7 +182,7 @@ def test_get_schedule Resque.get_schedule("some_ivar_job2")) end - def test_remove_schedule + test "remove_schedule removes a schedule" do Resque.redis.hset(:schedules, "some_ivar_job3", Resque.encode( {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp/44"} )) @@ -258,7 +191,7 @@ def test_remove_schedule assert Resque.redis.sismember(:schedules_changed, "some_ivar_job3") end - def test_adheres_to_lint + test "adheres to lint" do assert_nothing_raised do Resque::Plugin.lint(Resque::Scheduler) Resque::Plugin.lint(ResqueScheduler) diff --git a/test/test_helper.rb b/test/test_helper.rb index d6e92afe..9766c0cb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -68,6 +68,10 @@ def self.teardown(&block) define_method(:teardown, &block) end klass.class_eval &block end +class FakeCustomJobClass + def self.scheduled(queue, klass, *args); end +end + class SomeJob def self.perform(repo_id, path) end