summaryrefslogtreecommitdiff
path: root/spec/workers/background_migration_worker_spec.rb
blob: e5be8ce042332af33de825b2ea1aab4a0605b7f0 (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'

describe BackgroundMigrationWorker, :sidekiq, :clean_gitlab_redis_shared_state do
  let(:worker) { described_class.new }

  describe '.minimum_interval' do
    it 'returns 2 minutes' do
      expect(described_class.minimum_interval).to eq(2.minutes.to_i)
    end
  end

  describe '.perform' do
    it 'performs a background migration' do
      expect(Gitlab::BackgroundMigration)
        .to receive(:perform)
        .with('Foo', [10, 20])

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

    it 'reschedules a migration if it was performed recently' do
      expect(worker)
        .to receive(:always_perform?)
        .and_return(false)

      worker.lease_for('Foo').try_obtain

      expect(Gitlab::BackgroundMigration)
        .not_to receive(:perform)

      expect(described_class)
        .to receive(:perform_in)
        .with(a_kind_of(Numeric), 'Foo', [10, 20])

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

    it 'reschedules a migration if the database is not healthy' do
      allow(worker)
        .to receive(:always_perform?)
        .and_return(false)

      allow(worker)
        .to receive(:healthy_database?)
        .and_return(false)

      expect(described_class)
        .to receive(:perform_in)
        .with(a_kind_of(Numeric), 'Foo', [10, 20])

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

  describe '#healthy_database?' do
    context 'when replication lag is too great' do
      it 'returns false' do
        allow(Postgresql::ReplicationSlot)
          .to receive(:lag_too_great?)
          .and_return(true)

        expect(worker.healthy_database?).to eq(false)
      end

      context 'when replication lag is small enough' do
        it 'returns true' do
          allow(Postgresql::ReplicationSlot)
            .to receive(:lag_too_great?)
            .and_return(false)

          expect(worker.healthy_database?).to eq(true)
        end
      end
    end
  end
end