Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cleanup_stale_symbols_worker_spec.rb « nuget « packages « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 41afe64a8082774cfaae6e7dddf51d4179bc5661 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Packages::Nuget::CleanupStaleSymbolsWorker, type: :worker, feature_category: :package_registry do
  let(:worker) { described_class.new }

  describe '#perform_work' do
    subject(:perform_work) { worker.perform_work }

    context 'with no work to do' do
      it { is_expected.to be_nil }
    end

    context 'with work to do' do
      let_it_be(:symbol_1) { create(:nuget_symbol) }
      let_it_be(:symbol_2) { create(:nuget_symbol, :stale) }

      it 'deletes the stale symbol', :aggregate_failures do
        expect(worker).to receive(:log_extra_metadata_on_done).with(:nuget_symbol_id, symbol_2.id)
        expect(Packages::Nuget::Symbol).to receive(:next_pending_destruction).with(order_by: nil).and_call_original
        expect { perform_work }.to change { Packages::Nuget::Symbol.count }.by(-1)
        expect { symbol_2.reload }.to raise_error(ActiveRecord::RecordNotFound)
      end
    end

    context 'with a stale symbol' do
      let_it_be(:symbol) { create(:nuget_symbol, :stale) }

      context 'with an error during deletion' do
        before do
          allow_next_found_instance_of(Packages::Nuget::Symbol) do |instance|
            allow(instance).to receive(:destroy!).and_raise(StandardError)
          end
        end

        it 'handles the error' do
          expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
            instance_of(StandardError), class: described_class.name
          )

          expect { perform_work }.to change { Packages::Nuget::Symbol.error.count }.by(1)
          expect(symbol.reload).to be_error
        end
      end

      context 'when trying to destroy a destroyed record' do
        before do
          allow_next_found_instance_of(Packages::Nuget::Symbol) do |instance|
            destroy_method = instance.method(:destroy!)

            allow(instance).to receive(:destroy!) do
              destroy_method.call

              raise StandardError
            end
          end
        end

        it 'handles the error' do
          expect(Gitlab::ErrorTracking).to receive(:log_exception)
            .with(instance_of(StandardError), class: described_class.name)
          expect { perform_work }.not_to change { Packages::Nuget::Symbol.count }
          expect(symbol.reload).to be_error
        end
      end
    end
  end

  describe '#max_running_jobs' do
    let(:capacity) { described_class::MAX_CAPACITY }

    subject { worker.max_running_jobs }

    it { is_expected.to eq(capacity) }
  end
end