summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/workers/reactive_cacheable_shared_examples.rb
blob: 0bbd0e2a90db89beddef8a4d5088d686e3342dd8 (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
55
56
57
58
59
# frozen_string_literal: true

RSpec.shared_examples 'reactive cacheable worker' do
  describe '#perform' do
    context 'when reactive cache worker class is found' do
      let!(:cluster) { create(:cluster, :project, :provided_by_gcp) }
      let(:project) { cluster.project }
      let!(:environment) { create(:environment, project: project) }

      it 'calls #exclusively_update_reactive_cache!' do
        expect_any_instance_of(Environment).to receive(:exclusively_update_reactive_cache!)

        described_class.new.perform("Environment", environment.id)
      end

      context 'when ReactiveCaching::ExceededReactiveCacheLimit is raised' do
        it 'avoids failing the job and tracks via Gitlab::ErrorTracking' do
          allow_any_instance_of(Environment).to receive(:exclusively_update_reactive_cache!)
            .and_raise(ReactiveCaching::ExceededReactiveCacheLimit)

          expect(Gitlab::ErrorTracking).to receive(:track_exception)
            .with(kind_of(ReactiveCaching::ExceededReactiveCacheLimit))

          described_class.new.perform("Environment", environment.id)
        end
      end
    end

    context 'when reactive cache worker class is not found' do
      it 'raises no error' do
        expect { described_class.new.perform("Environment", -1) }.not_to raise_error
      end
    end

    context 'when reactive cache worker class is invalid' do
      it 'raises no error' do
        expect { described_class.new.perform("FooBarKux", -1) }.not_to raise_error
      end
    end
  end

  describe 'worker context' do
    it 'sets the related class on the job' do
      described_class.perform_async('Environment', 1, 'other', 'argument')

      scheduled_job = described_class.jobs.first

      expect(scheduled_job).to include('meta.related_class' => 'Environment')
    end

    it 'sets the related class on the job when it was passed as a class' do
      described_class.perform_async(Project, 1, 'other', 'argument')

      scheduled_job = described_class.jobs.first

      expect(scheduled_job).to include('meta.related_class' => 'Project')
    end
  end
end