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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2018-08-06 19:16:09 +0300
committerStan Hu <stanhu@gmail.com>2018-08-06 19:16:09 +0300
commit964d9f431f64754f171c5c523309417447c2ee71 (patch)
treed9d06c1ba2397ce9717a8ce0b01deb96d2bcce6b /spec
parentb4415c01740430cef58baf9bb0cbda2fb1055edb (diff)
parent1e5192cc8c2ebd3e0d740f3a044b7f5e4c086730 (diff)
Merge branch 'background-migrations-system-load' into 'master'
Respond to DB health in background migrations See merge request gitlab-org/gitlab-ce!20720
Diffstat (limited to 'spec')
-rw-r--r--spec/migrations/normalize_ldap_extern_uids_spec.rb6
-rw-r--r--spec/models/postgresql/replication_slot_spec.rb31
-rw-r--r--spec/workers/background_migration_worker_spec.rb52
3 files changed, 86 insertions, 3 deletions
diff --git a/spec/migrations/normalize_ldap_extern_uids_spec.rb b/spec/migrations/normalize_ldap_extern_uids_spec.rb
index c6ea1e3e49e..a23a5d54e0a 100644
--- a/spec/migrations/normalize_ldap_extern_uids_spec.rb
+++ b/spec/migrations/normalize_ldap_extern_uids_spec.rb
@@ -27,11 +27,11 @@ describe NormalizeLdapExternUids, :migration, :sidekiq do
migrate!
expect(BackgroundMigrationWorker.jobs[0]['args']).to eq([described_class::MIGRATION, [1, 2]])
- expect(BackgroundMigrationWorker.jobs[0]['at']).to eq(5.minutes.from_now.to_f)
+ expect(BackgroundMigrationWorker.jobs[0]['at']).to eq(2.minutes.from_now.to_f)
expect(BackgroundMigrationWorker.jobs[1]['args']).to eq([described_class::MIGRATION, [3, 4]])
- expect(BackgroundMigrationWorker.jobs[1]['at']).to eq(10.minutes.from_now.to_f)
+ expect(BackgroundMigrationWorker.jobs[1]['at']).to eq(4.minutes.from_now.to_f)
expect(BackgroundMigrationWorker.jobs[2]['args']).to eq([described_class::MIGRATION, [5, 5]])
- expect(BackgroundMigrationWorker.jobs[2]['at']).to eq(15.minutes.from_now.to_f)
+ expect(BackgroundMigrationWorker.jobs[2]['at']).to eq(6.minutes.from_now.to_f)
expect(BackgroundMigrationWorker.jobs.size).to eq 3
end
end
diff --git a/spec/models/postgresql/replication_slot_spec.rb b/spec/models/postgresql/replication_slot_spec.rb
new file mode 100644
index 00000000000..919a7526803
--- /dev/null
+++ b/spec/models/postgresql/replication_slot_spec.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Postgresql::ReplicationSlot, :postgresql do
+ describe '.lag_too_great?' do
+ it 'returns true when replication lag is too great' do
+ expect(described_class)
+ .to receive(:pluck)
+ .and_return([125.megabytes])
+
+ expect(described_class.lag_too_great?).to eq(true)
+ end
+
+ it 'returns false when more than one replicas is up to date enough' do
+ expect(described_class)
+ .to receive(:pluck)
+ .and_return([125.megabytes, 0.megabytes, 0.megabytes])
+
+ expect(described_class.lag_too_great?).to eq(false)
+ end
+
+ it 'returns false when replication lag is not too great' do
+ expect(described_class)
+ .to receive(:pluck)
+ .and_return([0.megabytes])
+
+ expect(described_class.lag_too_great?).to eq(false)
+ end
+ end
+end
diff --git a/spec/workers/background_migration_worker_spec.rb b/spec/workers/background_migration_worker_spec.rb
index d67e7698635..3bd072e7125 100644
--- a/spec/workers/background_migration_worker_spec.rb
+++ b/spec/workers/background_migration_worker_spec.rb
@@ -3,6 +3,12 @@ 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)
@@ -28,5 +34,51 @@ describe BackgroundMigrationWorker, :sidekiq, :clean_gitlab_redis_shared_state d
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 'using MySQL', :mysql do
+ it 'returns true' do
+ expect(worker.healthy_database?).to eq(true)
+ end
+ end
+
+ context 'using PostgreSQL', :postgresql 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
+ 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