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

schedule_aggregation_worker_spec.rb « namespaces « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62f9be501ccbe9fdf512fa1ff3570e8c94ea133a (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
78
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Namespaces::ScheduleAggregationWorker, '#perform', :clean_gitlab_redis_shared_state do
  let(:group) { create(:group) }

  subject(:worker) { described_class.new }

  context 'when group is the root ancestor' do
    context 'when aggregation schedule exists' do
      it 'does not create a new one' do
        stub_aggregation_schedule_statistics

        Namespace::AggregationSchedule.safe_find_or_create_by!(namespace_id: group.id)

        expect do
          worker.perform(group.id)
        end.not_to change { Namespace::AggregationSchedule.count }
      end
    end

    context 'when aggregation schedule does not exist' do
      it 'creates one' do
        stub_aggregation_schedule_statistics

        expect do
          worker.perform(group.id)
        end.to change { Namespace::AggregationSchedule.count }.by(1)

        expect(group.aggregation_schedule).to be_present
      end
    end
  end

  context 'when group is not the root ancestor' do
    let(:parent_group) { create(:group) }
    let(:group) { create(:group, parent: parent_group) }

    it 'creates an aggregation schedule for the root' do
      stub_aggregation_schedule_statistics

      worker.perform(group.id)

      expect(parent_group.aggregation_schedule).to be_present
    end
  end

  context 'when namespace does not exist' do
    it 'logs the error' do
      expect(Gitlab::ErrorTracking).to receive(:track_exception).once

      worker.perform(non_existing_record_id)
    end
  end

  it_behaves_like 'an idempotent worker' do
    let(:job_args) { [group.id] }

    it 'creates a single aggregation schedule' do
      expect { worker.perform(*job_args) }
        .to change { Namespace::AggregationSchedule.count }.by(1)
      expect { worker.perform(*job_args) }
        .not_to change { Namespace::AggregationSchedule.count }
    end
  end

  def stub_aggregation_schedule_statistics
    # Namespace::Aggregations are deleted by
    # Namespace::AggregationSchedule::schedule_root_storage_statistics,
    # which is executed async. Stubing the service so instances are not deleted
    # while still running the specs.
    expect_next_instance_of(Namespace::AggregationSchedule) do |aggregation_schedule|
      expect(aggregation_schedule)
        .to receive(:schedule_root_storage_statistics)
    end
  end
end