Commit 82552153 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve specs for background stage_id ref migration

parent 316d8821
...@@ -12,13 +12,12 @@ class MigrateStageIdReferenceInBackground < ActiveRecord::Migration ...@@ -12,13 +12,12 @@ class MigrateStageIdReferenceInBackground < ActiveRecord::Migration
end end
def up def up
Build.where(stage_id: nil) Build.where(stage_id: nil).in_batches(of: BATCH_SIZE) do |relation, index|
.in_batches(of: BATCH_SIZE) do |relation, index| schedule = index * 5.minutes
schedule = index * 5.minutes jobs = relation.pluck(:id).map { |id| [MIGRATION, [id]] }
jobs = relation.pluck(:id).map { |id| [MIGRATION, [id]] }
BackgroundMigrationWorker.perform_bulk_in(schedule, jobs) BackgroundMigrationWorker.perform_bulk_in(schedule, jobs)
end end
end end
def down def down
......
...@@ -2,7 +2,7 @@ require 'spec_helper' ...@@ -2,7 +2,7 @@ require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20170628080858_migrate_stage_id_reference_in_background') require Rails.root.join('db', 'post_migrate', '20170628080858_migrate_stage_id_reference_in_background')
describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do
matcher :have_scheduled_migration do |delay, *expected| matcher :be_scheduled_migration do |delay, *expected|
match do |migration| match do |migration|
BackgroundMigrationWorker.jobs.any? do |job| BackgroundMigrationWorker.jobs.any? do |job|
job['args'] == [migration, expected] && job['args'] == [migration, expected] &&
...@@ -24,16 +24,22 @@ describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do ...@@ -24,16 +24,22 @@ describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do
stub_const('MigrateStageIdReferenceInBackground::BATCH_SIZE', 2) stub_const('MigrateStageIdReferenceInBackground::BATCH_SIZE', 2)
projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1') projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
projects.create!(id: 345, name: 'gitlab2', path: 'gitlab2')
pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a') pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')
pipelines.create!(id: 2, project_id: 345, ref: 'feature', sha: 'cdf43c3c')
jobs.create!(id: 1, commit_id: 1, project_id: 123, stage_idx: 2, stage: 'build') jobs.create!(id: 1, commit_id: 1, project_id: 123, stage_idx: 2, stage: 'build')
jobs.create!(id: 2, commit_id: 1, project_id: 123, stage_idx: 2, stage: 'build') jobs.create!(id: 2, commit_id: 1, project_id: 123, stage_idx: 2, stage: 'build')
jobs.create!(id: 3, commit_id: 1, project_id: 123, stage_idx: 1, stage: 'test') jobs.create!(id: 3, commit_id: 1, project_id: 123, stage_idx: 1, stage: 'test')
jobs.create!(id: 4, commit_id: 1, project_id: 123, stage_idx: 3, stage: 'deploy') jobs.create!(id: 4, commit_id: 1, project_id: 123, stage_idx: 3, stage: 'deploy')
jobs.create!(id: 5, commit_id: 2, project_id: 345, stage_idx: 1, stage: 'test')
stages.create(id: 101, pipeline_id: 1, project_id: 123, name: 'test') stages.create(id: 101, pipeline_id: 1, project_id: 123, name: 'test')
stages.create(id: 102, pipeline_id: 1, project_id: 123, name: 'build') stages.create(id: 102, pipeline_id: 1, project_id: 123, name: 'build')
stages.create(id: 103, pipeline_id: 1, project_id: 123, name: 'deploy') stages.create(id: 103, pipeline_id: 1, project_id: 123, name: 'deploy')
jobs.create!(id: 6, commit_id: 2, project_id: 345, stage_id: 101, stage_idx: 1, stage: 'test')
end end
it 'correctly schedules background migrations' do it 'correctly schedules background migrations' do
...@@ -41,10 +47,11 @@ describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do ...@@ -41,10 +47,11 @@ describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do
Timecop.freeze do Timecop.freeze do
migrate! migrate!
expect(described_class::MIGRATION).to have_scheduled_migration(5.minutes, 1) expect(described_class::MIGRATION).to be_scheduled_migration(5.minutes, 1)
expect(described_class::MIGRATION).to have_scheduled_migration(5.minutes, 2) expect(described_class::MIGRATION).to be_scheduled_migration(5.minutes, 2)
expect(described_class::MIGRATION).to have_scheduled_migration(10.minutes, 3) expect(described_class::MIGRATION).to be_scheduled_migration(10.minutes, 3)
expect(described_class::MIGRATION).to have_scheduled_migration(10.minutes, 4) expect(described_class::MIGRATION).to be_scheduled_migration(10.minutes, 4)
expect(BackgroundMigrationWorker.jobs.size).to eq 5
end end
end end
end end
...@@ -55,7 +62,7 @@ describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do ...@@ -55,7 +62,7 @@ describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do
migrate! migrate!
expect(jobs.where(stage_id: nil)).to be_empty expect(jobs.where(stage_id: nil)).to be_one
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment