summaryrefslogtreecommitdiff
path: root/spec/workers/stuck_ci_jobs_worker_spec.rb
blob: e0a5d3c6c1c68f5f1d8d95445090da8165d76712 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

require 'spec_helper'

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 }

  before do
    stub_exclusive_lease(worker_lease_key, worker_lease_uuid)
  end

  describe '#perform' do
    it 'executes an instance of Ci::StuckBuildsDropService' do
      expect_next_instance_of(Ci::StuckBuilds::DropService) do |service|
        expect(service).to receive(:execute).exactly(:once)
      end

      worker.perform
    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

      it 'can execute in sequence' do
        expect(worker).to receive(:remove_lease).at_least(:once)
        expect(worker2).to receive(:remove_lease).at_least(:once)

        worker.perform
        worker2.perform
      end

      it 'cancels exclusive leases after worker perform' do
        expect_to_cancel_exclusive_lease(worker_lease_key, worker_lease_uuid)

        worker.perform
      end
    end
  end
end