summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/lib/gitlab/database/background_migration_job_shared_examples.rb
blob: 7888ade56ebc0debb2fa802acb9b8cba47d95083 (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
# frozen_string_literal: true

RSpec.shared_examples 'marks background migration job records' do
  it 'marks each job record as succeeded after processing' do
    create(:background_migration_job, class_name: "::#{described_class.name}",
           arguments: arguments)

    expect(::Gitlab::Database::BackgroundMigrationJob).to receive(:mark_all_as_succeeded).and_call_original

    expect do
      subject.perform(*arguments)
    end.to change { ::Gitlab::Database::BackgroundMigrationJob.succeeded.count }.from(0).to(1)
  end

  it 'returns the number of job records marked as succeeded' do
    create(:background_migration_job, class_name: "::#{described_class.name}",
           arguments: arguments)

    jobs_updated = subject.perform(*arguments)

    expect(jobs_updated).to eq(1)
  end
end

RSpec.shared_examples 'finalized background migration' do
  it 'processed the scheduled sidekiq queue' do
    queued = Sidekiq::ScheduledSet
      .new
      .select do |scheduled|
        scheduled.klass == 'BackgroundMigrationWorker' &&
        scheduled.args.first == job_class_name
      end
    expect(queued.size).to eq(0)
  end

  it 'processed the async sidekiq queue' do
    queued = Sidekiq::Queue.new('BackgroundMigrationWorker')
      .select { |scheduled| scheduled.klass == job_class_name }
    expect(queued.size).to eq(0)
  end

  include_examples 'removed tracked jobs', 'pending'
end

RSpec.shared_examples 'finalized tracked background migration' do
  include_examples 'finalized background migration'
  include_examples 'removed tracked jobs', 'succeeded'
end

RSpec.shared_examples 'removed tracked jobs' do |status|
  it "removes '#{status}' tracked jobs" do
    jobs = Gitlab::Database::BackgroundMigrationJob
      .where(status: Gitlab::Database::BackgroundMigrationJob.statuses[status])
      .for_migration_class(job_class_name)
    expect(jobs).to be_empty
  end
end

RSpec.shared_examples 'retained tracked jobs' do |status|
  it "retains '#{status}' tracked jobs" do
    jobs = Gitlab::Database::BackgroundMigrationJob
      .where(status: Gitlab::Database::BackgroundMigrationJob.statuses[status])
      .for_migration_class(job_class_name)
    expect(jobs).to be_present
  end
end