test/plugins/job/worker_test.rb in rocketjob-2.1.3 vs test/plugins/job/worker_test.rb in rocketjob-3.0.0.alpha

- old
+ new

@@ -17,34 +17,36 @@ logger.info 'some very noisy logging' end end class SumJob < RocketJob::Job - rocket_job do |job| - job.destroy_on_complete = false - job.collect_output = true - job.priority = 51 - end + self.destroy_on_complete = false + self.collect_output = true + self.priority = 51 - def perform(first, second) + field :first, type: Integer + field :second, type: Integer + + def perform first + second end end describe RocketJob::Plugins::Job::Worker do before do - RocketJob::Job.destroy_all + RocketJob::Job.delete_all + RocketJob::Server.delete_all end after do @job.destroy if @job && !@job.new_record? end describe '.rocket_job_next_job' do before do @job = QuietJob.new - @worker = RocketJob::Worker.new(name: 'worker:123') + @worker = RocketJob::Server.new(name: 'worker:123') end it 'return nil when no jobs available' do assert_equal nil, RocketJob::Job.rocket_job_next_job(@worker.name) end @@ -77,29 +79,21 @@ end end describe '#perform_now' do it 'calls perform method' do - @job = SumJob.new(arguments: [10, 5]) + @job = SumJob.new(first: 10, second: 5) assert_equal 15, @job.perform_now['result'] assert @job.completed?, @job.attributes.ai assert_equal 15, @job.result['result'] end - it 'saves exception' do - @job = SumJob.new(arguments: ['10', 5]) - assert_raises TypeError do - @job.perform_now - end - assert @job.exception.backtrace - assert_equal 'TypeError', @job.exception.class_name - if RUBY_VERSION.to_f < 2.0 - assert_equal "can't convert Fixnum into String", @job.exception.message - else - assert_equal 'no implicit conversion of Fixnum into String', @job.exception.message - end - assert_equal 'inline', @job.exception.worker_name + it 'converts type' do + @job = SumJob.new(first: '10', second: 5) + assert_equal 15, @job.perform_now['result'] + assert @job.completed?, @job.attributes.ai + assert_equal 15, @job.result['result'] end it 'silence logging when log_level is set' do @job = NoisyJob.new @job.log_level = :warn @@ -125,18 +119,18 @@ end describe '.perform_later' do it 'queues the job for processing' do RocketJob::Config.stub(:inline_mode, false) do - @job = SumJob.perform_later(1, 23) + @job = SumJob.perform_later(first: 1, second: 23) end assert @job.queued? # Manually run the job @job.perform_now assert @job.completed?, @job.attributes.ai - assert_equal 24, @job.result['result'] + assert_equal 24, @job.result['result'], -> { @job.result.ai } assert_nil @job.worker_name assert @job.completed_at assert @job.created_at assert_equal false, @job.destroy_on_complete @@ -148,11 +142,11 @@ assert @job.started_at end it 'runs the job immediately when inline_mode = true' do RocketJob::Config.stub(:inline_mode, true) do - @job = SumJob.perform_later(1, 23) + @job = SumJob.perform_later(first: 1, second: 23) end assert @job.completed?, @job.attributes.ai assert_equal 24, @job.result['result'] @@ -169,12 +163,37 @@ end end describe '.perform_now' do it 'run the job immediately' do - @job = SumJob.perform_now(1, 5) + @job = SumJob.perform_now(first: 1, second: 5) assert_equal true, @job.completed? assert_equal 6, @job.result['result'] + end + end + + describe '#rocket_job_active_servers' do + before do + @job = QuietJob.create! + @worker = RocketJob::Server.create!(name: 'worker:123') + end + + it 'should return empty hash for no active jobs' do + assert_equal({}, @job.rocket_job_active_servers) + end + + it 'should return active servers' do + assert job = RocketJob::Job.rocket_job_next_job(@worker.name) + assert active = job.rocket_job_active_servers + assert_equal 1, active.size + assert active_servers = active[@worker.name] + assert_equal 1, active_servers.size + assert active_server = active_servers.first + assert_equal @job.id, active_server.job.id + assert_equal @worker.name, active_server.name + assert_equal job.started_at, active_server.started_at + assert active_server.duration_s + assert active_server.duration end end end end