test/plugins/cron_test.rb in rocketjob-3.4.1 vs test/plugins/cron_test.rb in rocketjob-3.4.2

- old
+ new

@@ -9,14 +9,23 @@ def perform 'DONE' end end + class FailOnceCronJob < RocketJob::Job + include RocketJob::Plugins::Cron + + def perform + raise 'oh no' if failure_count == 0 + end + end + describe RocketJob::Plugins::Cron do before do # destroy_all could create new instances CronJob.delete_all + FailOnceCronJob.delete_all assert_equal 0, CronJob.count end after do @job.destroy if @job && !@job.new_record? @@ -89,13 +98,16 @@ end describe '#fail' do describe 'with cron_schedule' do let :job do - job = CronJob.create!(cron_schedule: '* 1 * * *') + job = FailOnceCronJob.create!(cron_schedule: '* 1 * * *') job.start - job.fail + assert_raises RuntimeError do + job.perform_now + end + job.reload job end it 'allows current cron job instance to fail' do assert job.failed? @@ -108,15 +120,23 @@ it 'retains run_at' do assert job.run_at end it 'schedules a new instance' do - assert_equal 0, CronJob.count + assert_equal 0, FailOnceCronJob.count job - assert_equal 2, CronJob.count - assert scheduled_job = CronJob.last + assert_equal 2, FailOnceCronJob.count + assert scheduled_job = FailOnceCronJob.last assert scheduled_job.queued? assert_equal '* 1 * * *', scheduled_job.cron_schedule + end + + it 'restarts on retry' do + job.retry! + job.perform_now + assert job.completed? + assert_equal 1, FailOnceCronJob.count, -> { FailOnceCronJob.all.to_a.collect(&:state).to_s } + assert_equal 1, FailOnceCronJob.queued.count end end describe 'without cron_schedule' do let :job do