gitlab-org--gitlab-foss/spec/workers/stuck_ci_jobs_worker_spec.rb

75 lines
2.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'spec_helper'
2015-10-21 09:29:47 +00:00
RSpec.describe StuckCiJobsWorker do
include ExclusiveLeaseHelpers
let(:worker_lease_key) { StuckCiJobsWorker::EXCLUSIVE_LEASE_KEY }
let(:worker_lease_uuid) { SecureRandom.uuid }
let(:worker2) { described_class.new }
subject(:worker) { described_class.new }
2015-10-21 09:29:47 +00:00
before do
stub_exclusive_lease(worker_lease_key, worker_lease_uuid)
end
describe '#perform' do
it 'enqueues a Ci::StuckBuilds::DropRunningWorker job' do
expect(Ci::StuckBuilds::DropRunningWorker).to receive(:perform_in).with(20.minutes).exactly(:once)
worker.perform
end
it 'executes an instance of Ci::StuckBuilds::DropService' do
expect_next_instance_of(Ci::StuckBuilds::DropService) do |service|
expect(service).to receive(:execute).exactly(:once)
end
worker.perform
2017-09-02 08:06:25 +00:00
end
context 'with an exclusive lease' do
it 'does not execute concurrently' do
expect(worker).to receive(:remove_lease).exactly(:once)
expect(worker2).not_to receive(:remove_lease)
worker.perform
stub_exclusive_lease_taken(worker_lease_key)
worker2.perform
end
2018-09-27 09:17:43 +00:00
it 'can execute in sequence' do
expect(worker).to receive(:remove_lease).at_least(:once)
expect(worker2).to receive(:remove_lease).at_least(:once)
2018-09-27 09:17:43 +00:00
worker.perform
worker2.perform
2018-09-27 09:17:43 +00:00
end
it 'cancels exclusive leases after worker perform' do
expect_to_cancel_exclusive_lease(worker_lease_key, worker_lease_uuid)
2018-09-27 09:17:43 +00:00
worker.perform
end
context 'when the DropService fails' do
it 'ensures cancellation of the exclusive lease' do
expect_to_cancel_exclusive_lease(worker_lease_key, worker_lease_uuid)
allow_next_instance_of(Ci::StuckBuilds::DropService) do |service|
expect(service).to receive(:execute) do
raise 'The query timed out'
end
end
expect { worker.perform }.to raise_error(/The query timed out/)
end
end
2018-09-27 09:17:43 +00:00
end
end
2017-03-01 12:56:54 +00:00
end