Sha256: 947bbaeced8da5a558e573ebe61d2dd1f970d0b078b286dbc7676a168b7f73b3

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require_relative 'test_helper'

# Unit Test for RocketJob::Job
class JobTest < Minitest::Test
  class SimpleJob < RocketJob::Job
    def perform
    end
  end

  describe RocketJob::Job do
    before do
      @description = 'Hello World'
      @job         = SimpleJob.new(description: @description)
      @job2        = SimpleJob.new(description: @description, priority: 52)
    end

    after do
      @job.destroy if @job && !@job.new_record?
      @job2.destroy if @job2 && !@job2.new_record?
    end

    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

      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
    end

    describe '.requeue_dead_server' do
      it 'requeue jobs from dead workers' do
        assert_equal 52, @job2.priority
        worker_name      = 'server:12345'
        @job.worker_name = worker_name
        @job.start!
        assert @job.running?, @job.state

        worker_name2      = 'server:76467'
        @job2.worker_name = worker_name2
        @job2.start!
        assert_equal true, @job2.valid?
        assert @job2.running?, @job2.state
        @job2.save!

        RocketJob::Job.requeue_dead_server(worker_name)
        @job.reload

        assert @job.queued?
        assert_nil @job.worker_name

        assert_equal worker_name2, @job2.worker_name
        @job2.reload
        assert_equal worker_name2, @job2.worker_name
        assert @job2.running?, @job2.state
        assert_equal worker_name2, @job2.worker_name
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rocketjob-3.5.2 test/job_test.rb
rocketjob-3.5.1 test/job_test.rb
rocketjob-3.5.0 test/job_test.rb