test/job_test.rb in rocketjob-1.1.2 vs test/job_test.rb in rocketjob-1.1.3

- old
+ new

@@ -1,12 +1,12 @@ require_relative 'test_helper' require_relative 'jobs/test_job' # Unit Test for RocketJob::Job class JobTest < Minitest::Test - context RocketJob::Job do - setup do + describe RocketJob::Job do + before do @worker = RocketJob::Worker.new @worker.started @description = 'Hello World' @arguments = [1] @job = Jobs::TestJob.new( @@ -19,23 +19,23 @@ arguments: @arguments, destroy_on_complete: false ) end - teardown do + after do @job.destroy if @job && !@job.new_record? @job2.destroy if @job2 && !@job2.new_record? end - context '.config' do - should 'support multiple databases' do + describe '.config' do + it 'support multiple databases' do assert_equal 'test_rocketjob', RocketJob::Job.collection.db.name end end - context '#reload' do - should 'handle hash' do + describe '#reload' do + it 'handle hash' do @job = Jobs::TestJob.new( description: @description, arguments: [{key: 'value'}], destroy_on_complete: false, worker_name: 'worker:123' @@ -51,12 +51,12 @@ assert_equal 'value', @job.arguments.first[:key] assert_equal nil, @job.worker_name end end - context '#save!' do - should 'save a blank job' do + describe '#save!' do + it 'save a blank job' do @job.save! assert_nil @job.worker_name assert_nil @job.completed_at assert @job.created_at assert_equal @description, @job.description @@ -70,53 +70,53 @@ assert_nil @job.started_at assert_equal :queued, @job.state end end - context '#status' do - should 'return status for a queued job' do + describe '#status' do + it 'return status for a queued job' do assert_equal true, @job.queued? h = @job.status assert_equal :queued, h['state'] assert_equal @description, h['description'] end - should 'return status for a failed job' do + it 'return status for a failed job' do @job.start! @job.fail!('worker:1234', 'oh no') assert_equal true, @job.failed? h = @job.status assert_equal :failed, h['state'] assert_equal @description, h['description'] assert_equal 'RocketJob::JobException', h['exception']['class_name'], h assert_equal 'oh no', h['exception']['message'], h end - should 'mark user as reason for failure when not supplied' do + it 'mark user as reason for failure when not supplied' do @job.start! @job.fail! assert_equal true, @job.failed? assert_equal @description, @job.description assert_equal 'RocketJob::JobException', @job.exception.class_name assert_equal 'Job failed through user action', @job.exception.message assert_equal 'user', @job.exception.worker_name end end - context '#fail_with_exception!' do - should 'fail with message' do + describe '#fail_with_exception!' do + it 'fail with message' do @job.start! @job.fail!('myworker:2323', 'oh no') assert_equal true, @job.failed? h = @job.status assert_equal :failed, h['state'] assert_equal @description, h['description'] assert_equal 'RocketJob::JobException', h['exception']['class_name'], h assert_equal 'oh no', h['exception']['message'], h end - should 'fail with exception' do + it 'fail with exception' do @job.start! exception = nil begin blah rescue Exception => exc @@ -130,35 +130,35 @@ assert_equal exception.class.name.to_s, h['exception']['class_name'], h assert h['exception']['message'].include?('undefined local variable or method'), h end end - context '#work' do - should 'call default perform method' do + describe '#work' do + it 'call default perform method' do @job.start! assert_equal false, @job.work(@worker) assert_equal true, @job.completed?, @job.state assert_equal 2, Jobs::TestJob.result end - should 'call specific method' do + it 'call specific method' do @job.perform_method = :sum @job.arguments = [23, 45] @job.start! assert_equal false, @job.work(@worker) assert_equal true, @job.completed? assert_equal 68, Jobs::TestJob.result end - should 'destroy on complete' do + it 'destroy on complete' do @job.destroy_on_complete = true @job.start! assert_equal false, @job.work(@worker) assert_equal nil, RocketJob::Job.find_by_id(@job.id) end - should 'silence logging when log_level is set' do + it 'silence logging when log_level is set' do @job.destroy_on_complete = true @job.log_level = :warn @job.perform_method = :noisy_logger @job.arguments = [] @job.start! @@ -167,11 +167,11 @@ assert_equal false, @job.work(@worker), @job.inspect end assert_equal false, logged end - should 'raise logging when log_level is set' do + it 'raise logging when log_level is set' do @job.destroy_on_complete = true @job.log_level = :trace @job.perform_method = :debug_logging @job.arguments = [] @job.start! @@ -183,11 +183,11 @@ end end assert_equal false, logged end - should 'call before and after' do + it 'call before and after' do named_parameters = {'counter' => 23} @job.perform_method = :event @job.arguments = [named_parameters] @job.start! assert_equal false, @job.work(@worker), @job.inspect @@ -195,49 +195,49 @@ assert_equal named_parameters.merge('before_event' => true, 'after_event' => true), @job.arguments.first end end - context '.next_job' do - setup do + describe '.next_job' do + before do RocketJob::Job.destroy_all end - should 'return nil when no jobs available' do + it 'return nil when no jobs available' do assert_equal nil, RocketJob::Job.next_job(@worker.name) end - should 'return the first job' do + it 'return the first job' do @job.save! assert job = RocketJob::Job.next_job(@worker.name), 'Failed to find job' assert_equal @job.id, job.id end - should 'Ignore future dated jobs' do + it 'Ignore future dated jobs' do @job.run_at = Time.now + 1.hour @job.save! assert_equal nil, RocketJob::Job.next_job(@worker.name) end - should 'Process future dated jobs when time is now' do + it 'Process future dated jobs when time is now' do @job.run_at = Time.now @job.save! assert job = RocketJob::Job.next_job(@worker.name), 'Failed to find future job' assert_equal @job.id, job.id end - should 'Skip expired jobs' do + it 'Skip expired jobs' do count = RocketJob::Job.count @job.expires_at = Time.now - 100 @job.save! assert_equal nil, RocketJob::Job.next_job(@worker.name) assert_equal count, RocketJob::Job.count end end - context '#requeue!' do - should 'requeue jobs from dead workers' do + describe '#requeue!' do + it 'requeue jobs from dead workers' do worker_name = 'server:12345' @job.worker_name = worker_name @job.start! assert @job.running? @@ -247,12 +247,12 @@ assert @job.queued? assert_equal nil, @job.worker_name end end - context '#requeue' do - should 'requeue jobs from dead workers' do + describe '#requeue' do + it 'requeue jobs from dead workers' do worker_name = 'server:12345' @job.worker_name = worker_name @job.start! assert @job.running? @@ -264,12 +264,12 @@ assert @job.running? assert_equal worker_name, @job.worker_name end end - context '.requeue_dead_worker' do - should 'requeue jobs from dead workers' do + describe '.requeue_dead_worker' do + it 'requeue jobs from dead workers' do worker_name = 'server:12345' @job.worker_name = worker_name @job.start! assert @job.running? @@ -287,23 +287,25 @@ assert @job2.running? assert_equal worker_name2, @job2.worker_name end end - context '#retry!' do - should 'retry failed jobs' do + describe '#retry!' do + it 'retry failed jobs' do worker_name = 'server:12345' @job.worker_name = worker_name @job.start! assert @job.running? assert_equal worker_name, @job.worker_name @job.fail!(worker_name, 'oh no') assert @job.failed? + assert_equal 'oh no', @job.exception.message @job.retry! assert @job.queued? assert_equal nil, @job.worker_name + assert_equal nil, @job.exception end end end end