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

redis_migration_worker_spec.rb « workers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f29c84a94895f4902f22dd356ac544f64d63db9 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RedisMigrationWorker, :clean_gitlab_redis_shared_state, feature_category: :redis do
  describe '.fetch_migrator!' do
    it 'raise error if class does not exist' do
      expect { described_class.fetch_migrator!('UnknownClass') }.to raise_error(NotImplementedError)
    end

    context 'when class exists' do
      it 'returns an instance' do
        expect(
          described_class.fetch_migrator!('BackfillProjectPipelineStatusTtl')
        ).to be_a Gitlab::BackgroundMigration::Redis::BackfillProjectPipelineStatusTtl
      end
    end
  end

  describe '#perform' do
    let(:job_class_name) { 'SampleJob' }
    let(:migrator_class) do
      Class.new do
        def perform(keys)
          keys.each { |key| redis.set(key, "adjusted", ex: 10) }
        end

        def scan_match_pattern
          'sample:*:pattern'
        end

        def redis
          ::Redis.new(::Gitlab::Redis::Cache.params)
        end
      end
    end

    let(:migrator) { migrator_class.new }

    before do
      allow(described_class).to receive(:fetch_migrator!).with(job_class_name).and_return(migrator)

      100.times do |i|
        migrator.redis.set("sample:#{i}:pattern", i)
      end
    end

    it 'runs migration logic on scanned keys' do
      expect(migrator).to receive(:perform).at_least(:once)

      subject.perform(job_class_name, '0')
    end

    context 'when job exceeds deadline' do
      before do
        # stub Time.now to force the 3rd invocation to timeout
        now = Time.now # rubocop:disable Rails/TimeZone
        allow(Time).to receive(:now).and_return(now, now, now + 5.minutes)
      end

      it 'enqueues another job and returns' do
        expect(described_class).to receive(:perform_async)

        # use smaller scan_size to ensure multiple scans are required
        subject.perform(job_class_name, '0', { scan_size: 10 })
      end
    end

    it_behaves_like 'an idempotent worker' do
      let(:job_args) { [job_class_name, '0'] }
    end
  end
end