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

background_migration_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8dd7f6892a6d639db64a24df90f0779e73b59f0a (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration do
  let(:default_tracking_database) { described_class::DEFAULT_TRACKING_DATABASE }
  let(:coordinator) { described_class::JobCoordinator.for_tracking_database(default_tracking_database) }

  before do
    allow(described_class).to receive(:coordinator_for_database)
      .with(default_tracking_database)
      .and_return(coordinator)
  end

  describe '.queue' do
    it 'returns background migration worker queue' do
      expect(described_class.queue)
        .to eq BackgroundMigrationWorker.sidekiq_options['queue']
    end
  end

  describe '.steal' do
    context 'when the queue contains unprocessed jobs' do
      let(:queue) do
        [
          double(args: ['Foo', [10, 20]], klass: 'BackgroundMigrationWorker'),
          double(args: ['Bar', [20, 30]], klass: 'BackgroundMigrationWorker'),
          double(args: ['Foo', [20, 30]], klass: 'MergeWorker')
        ]
      end

      before do
        allow(Sidekiq::Queue).to receive(:new)
          .with(coordinator.queue)
          .and_return(queue)
      end

      it 'uses the coordinator to steal jobs' do
        expect(queue[0]).to receive(:delete).and_return(true)

        expect(coordinator).to receive(:steal).with('Foo', retry_dead_jobs: false).and_call_original
        expect(coordinator).to receive(:perform).with('Foo', [10, 20])

        described_class.steal('Foo')
      end

      context 'when a custom predicate is given' do
        it 'steals jobs that match the predicate' do
          expect(queue[0]).to receive(:delete).and_return(true)

          expect(coordinator).to receive(:perform).with('Foo', [10, 20])

          described_class.steal('Foo') { |job| job.args.second.first == 10 && job.args.second.second == 20 }
        end

        it 'does not steal jobs that do not match the predicate' do
          expect(coordinator).not_to receive(:perform)

          expect(queue[0]).not_to receive(:delete)

          described_class.steal('Foo') { |(arg1, _)| arg1 == 5 }
        end
      end
    end

    context 'when retry_dead_jobs is true', :redis do
      let(:retry_queue) do
        [double(args: ['Object', [3]], klass: 'BackgroundMigrationWorker', delete: true)]
      end

      let(:dead_queue) do
        [double(args: ['Object', [4]], klass: 'BackgroundMigrationWorker', delete: true)]
      end

      before do
        allow(Sidekiq::RetrySet).to receive(:new).and_return(retry_queue)
        allow(Sidekiq::DeadSet).to receive(:new).and_return(dead_queue)
      end

      it 'steals from the dead and retry queue' do
        Sidekiq::Testing.disable! do
          expect(coordinator).to receive(:perform).with('Object', [1]).ordered
          expect(coordinator).to receive(:perform).with('Object', [2]).ordered
          expect(coordinator).to receive(:perform).with('Object', [3]).ordered
          expect(coordinator).to receive(:perform).with('Object', [4]).ordered

          BackgroundMigrationWorker.perform_async('Object', [2])
          BackgroundMigrationWorker.perform_in(10.minutes, 'Object', [1])

          described_class.steal('Object', retry_dead_jobs: true)
        end
      end
    end
  end

  describe '.perform' do
    let(:migration) { spy(:migration) }

    before do
      stub_const("#{described_class.name}::Foo", migration)
    end

    it 'uses the coordinator to perform a background migration' do
      expect(coordinator).to receive(:perform).with('Foo', [10, 20]).and_call_original
      expect(migration).to receive(:perform).with(10, 20).once

      described_class.perform('Foo', [10, 20])
    end
  end

  describe '.exists?', :redis do
    before do
      Sidekiq::Testing.disable! do
        MergeWorker.perform_async('Bar')
        BackgroundMigrationWorker.perform_async('Foo')
      end
    end

    it 'uses the coordinator to find if a job exists' do
      expect(coordinator).to receive(:exists?).with('Foo', []).and_call_original

      expect(described_class.exists?('Foo')).to eq(true)
    end

    it 'uses the coordinator to find a job does not exist' do
      expect(coordinator).to receive(:exists?).with('Bar', []).and_call_original

      expect(described_class.exists?('Bar')).to eq(false)
    end
  end

  describe '.remaining', :redis do
    before do
      Sidekiq::Testing.disable! do
        MergeWorker.perform_async('Foo')
        MergeWorker.perform_in(10.minutes, 'Foo')

        5.times do
          BackgroundMigrationWorker.perform_async('Foo')
        end
        3.times do
          BackgroundMigrationWorker.perform_in(10.minutes, 'Foo')
        end
      end
    end

    it 'uses the coordinator to find the number of remaining jobs' do
      expect(coordinator).to receive(:remaining).and_call_original

      expect(described_class.remaining).to eq(8)
    end
  end
end