summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration_spec.rb
blob: 9c6b07f02cc0d09b1e9da7caf3b9f2eb75c9e73f (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
154
155
156
157
158
159
160
161
162
163
164
165
166
require 'spec_helper'

describe Gitlab::BackgroundMigration do
  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 there are enqueued jobs present' do
      let(:queue) do
        [double(args: ['Foo', [10, 20]], queue: described_class.queue)]
      end

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

      context 'when queue contains unprocessed jobs' do
        it 'steals jobs from a queue' do
          expect(queue[0]).to receive(:delete).and_return(true)

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

          described_class.steal('Foo')
        end

        it 'does not steal job that has already been taken' do
          expect(queue[0]).to receive(:delete).and_return(false)

          expect(described_class).not_to receive(:perform)

          described_class.steal('Foo')
        end

        it 'does not steal jobs for a different migration' do
          expect(described_class).not_to receive(:perform)

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

          described_class.steal('Bar')
        end
      end

      context 'when one of the jobs raises an error' do
        let(:migration) { spy(:migration) }

        let(:queue) do
          [double(args: ['Foo', [10, 20]], queue: described_class.queue),
           double(args: ['Foo', [20, 30]], queue: described_class.queue)]
        end

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

          allow(queue[0]).to receive(:delete).and_return(true)
          allow(queue[1]).to receive(:delete).and_return(true)
        end

        context 'when standard error is being raised' do
          before do
            allow(migration).to receive(:perform).with(10, 20)
              .and_raise(StandardError, 'Migration error')
          end

          it 'recovers from an exception and retries the migration' do
            expect(migration).to receive(:perform).with(10, 20)
              .exactly(3).times.ordered
            expect(migration).to receive(:perform).with(20, 30)
              .once.ordered
            expect(Rails.logger).to receive(:warn)
              .with(/Retrying background migration/).exactly(3).times

            described_class.steal('Foo')
          end
        end

        context 'when top level exception is being raised' do
          it 'enqueues the migration again and reraises the error' do
            allow(migration).to receive(:perform).with(10, 20)
              .and_raise(Exception, 'Migration error').once

            expect(BackgroundMigrationWorker).to receive(:perform_async)
              .with('Foo', [10, 20]).once

            expect(Rails.logger).not_to receive(:warn)
            expect { described_class.steal('Foo') }
              .to raise_error(Exception)
          end
        end
      end
    end

    context 'when there are scheduled jobs present', :sidekiq, :redis do
      it 'steals all jobs from the scheduled sets' do
        Sidekiq::Testing.disable! do
          BackgroundMigrationWorker.perform_in(10.minutes, 'Object')

          expect(Sidekiq::ScheduledSet.new).to be_one
          expect(described_class).to receive(:perform).with('Object', any_args)

          described_class.steal('Object')

          expect(Sidekiq::ScheduledSet.new).to be_none
        end
      end
    end

    context 'when there are enqueued and scheduled jobs present', :sidekiq, :redis do
      it 'steals from the scheduled sets queue first' do
        Sidekiq::Testing.disable! do
          expect(described_class).to receive(:perform)
            .with('Object', [1], anything).ordered
          expect(described_class).to receive(:perform)
            .with('Object', [2], anything).ordered

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

          described_class.steal('Object')
        end
      end
    end
  end

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

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

    context 'when retries count is not specified' do
      it 'performs a background migration' do
        expect(migration).to receive(:perform).with(10, 20).once

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

    context 'when retries count is zero' do
      it 'perform a background migration only once' do
        expect(migration).to receive(:perform).with(10, 20)
          .and_raise(StandardError).once

        expect {  described_class.perform('Foo', [10, 20], retries: 0) }
          .to raise_error(StandardError)
      end
    end

    context 'when retries count is larger than zero' do
      it 'retries a background migration when needed' do
        expect(migration).to receive(:perform).with(10, 20)
          .and_raise(StandardError).exactly(3).times

        expect {  described_class.perform('Foo', [10, 20], retries: 3) }
          .to raise_error(StandardError)
      end
    end
  end
end