BigW Consortium Gitlab

background_migration_spec.rb 3.6 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::BackgroundMigration do
4 5 6 7 8 9
  describe '.queue' do
    it 'returns background migration worker queue' do
      expect(described_class.queue)
        .to eq BackgroundMigrationWorker.sidekiq_options['queue']
    end
  end
10

11 12
  describe '.steal' do
    context 'when there are enqueued jobs present' do
13 14 15
      let(:queue) do
        [double(args: ['Foo', [10, 20]], queue: described_class.queue)]
      end
16

17 18 19 20 21
      before do
        allow(Sidekiq::Queue).to receive(:new)
          .with(described_class.queue)
          .and_return(queue)
      end
22

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

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

30 31 32 33 34 35
          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)

36
          expect(described_class).not_to receive(:perform)
37

38 39
          described_class.steal('Foo')
        end
40

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

44 45 46 47
          expect(queue[0]).not_to receive(:delete)

          described_class.steal('Bar')
        end
48 49
      end

50 51 52 53 54 55 56 57 58 59 60 61 62 63
      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
64

65 66 67
        it 'enqueues the migration again and re-raises the error' do
          allow(migration).to receive(:perform).with(10, 20)
            .and_raise(Exception, 'Migration error').once
68

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

72
          expect { described_class.steal('Foo') }.to raise_error(Exception)
73
        end
74
      end
75
    end
76 77

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

82 83 84 85 86 87 88 89 90
          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
91 92 93 94 95

    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)
96
            .with('Object', [1]).ordered
97
          expect(described_class).to receive(:perform)
98
            .with('Object', [2]).ordered
99 100 101 102 103 104 105 106

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

          described_class.steal('Object')
        end
      end
    end
107 108 109
  end

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

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

116 117
    it 'performs a background migration' do
      expect(migration).to receive(:perform).with(10, 20).once
118

119
      described_class.perform('Foo', [10, 20])
120 121 122
    end
  end
end