summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration_spec.rb
blob: c12040cba1472e31d85ac7afafe11b9774692691 (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
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(:job) { double(:job, args: ['Foo', [10, 20]]) }
      let(:queue) { [job] }

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

        allow(job).to receive(:queue)
          .and_return(described_class.queue)
      end

      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])

        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).with('Foo', [10, 20])

        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 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]).ordered
          expect(described_class).to receive(:perform)
            .with('Object', [2]).ordered

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

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

  describe '.perform' do
    it 'performs a background migration' do
      instance = double(:instance)
      klass = double(:klass, new: instance)

      expect(described_class).to receive(:const_get)
        .with('Foo')
        .and_return(klass)

      expect(instance).to receive(:perform).with(10, 20)

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