Use exclusive lease helper methods in the EE spec files

parent d6b982b9
...@@ -2,6 +2,7 @@ require 'spec_helper' ...@@ -2,6 +2,7 @@ require 'spec_helper'
describe Gitlab::Geo::LogCursor::Daemon, :postgresql, :clean_gitlab_redis_shared_state do describe Gitlab::Geo::LogCursor::Daemon, :postgresql, :clean_gitlab_redis_shared_state do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
...@@ -37,9 +38,14 @@ describe Gitlab::Geo::LogCursor::Daemon, :postgresql, :clean_gitlab_redis_shared ...@@ -37,9 +38,14 @@ describe Gitlab::Geo::LogCursor::Daemon, :postgresql, :clean_gitlab_redis_shared
end end
it 'skips execution if cannot achieve a lease' do it 'skips execution if cannot achieve a lease' do
lease = stub_exclusive_lease_taken('geo_log_cursor_processed')
allow(lease).to receive(:try_obtain_with_ttl).and_return({ ttl: 1, uuid: false })
allow(lease).to receive(:same_uuid?).and_return(false)
allow(Gitlab::Geo::LogCursor::Lease).to receive(:exclusive_lease).and_return(lease)
is_expected.to receive(:exit?).and_return(false, true) is_expected.to receive(:exit?).and_return(false, true)
is_expected.not_to receive(:run_once!) is_expected.not_to receive(:run_once!)
expect_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain_with_ttl).and_return({ ttl: 1, uuid: false })
daemon.run! daemon.run!
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Geo::LogCursor::Lease, :clean_gitlab_redis_shared_state do describe Gitlab::Geo::LogCursor::Lease, :clean_gitlab_redis_shared_state do
include ExclusiveLeaseHelpers
describe '.exclusive_lease' do describe '.exclusive_lease' do
it 'returns an exclusive lease instance' do it 'returns an exclusive lease instance' do
expect(described_class.send(:exclusive_lease)).to be_an_instance_of(Gitlab::ExclusiveLease) expect(described_class.send(:exclusive_lease)).to be_an_instance_of(Gitlab::ExclusiveLease)
...@@ -8,15 +10,20 @@ describe Gitlab::Geo::LogCursor::Lease, :clean_gitlab_redis_shared_state do ...@@ -8,15 +10,20 @@ describe Gitlab::Geo::LogCursor::Lease, :clean_gitlab_redis_shared_state do
end end
describe '.renew!' do describe '.renew!' do
let(:lease) { stub_exclusive_lease(described_class::LEASE_KEY, renew: true) }
before do
allow(described_class).to receive(:exclusive_lease).and_return(lease)
end
it 'returns an exclusive lease instance' do it 'returns an exclusive lease instance' do
expect_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew) expect(lease).to receive(:renew)
described_class.renew! described_class.renew!
end end
it 'logs with the correct caller class' do it 'logs with the correct caller class' do
stub_const("Gitlab::Geo::LogCursor::Logger::PID", 111) stub_const("Gitlab::Geo::LogCursor::Logger::PID", 111)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew).and_return(true)
expect(::Gitlab::Logger).to receive(:debug).with(pid: 111, expect(::Gitlab::Logger).to receive(:debug).with(pid: 111,
class: 'Gitlab::Geo::LogCursor::Lease', class: 'Gitlab::Geo::LogCursor::Lease',
...@@ -44,7 +51,12 @@ describe Gitlab::Geo::LogCursor::Lease, :clean_gitlab_redis_shared_state do ...@@ -44,7 +51,12 @@ describe Gitlab::Geo::LogCursor::Lease, :clean_gitlab_redis_shared_state do
end end
it 'returns > 0 if there was an error' do it 'returns > 0 if there was an error' do
expect(Gitlab::ExclusiveLease).to receive(:cancel) lease = stub_exclusive_lease(described_class::LEASE_KEY, 'uuid')
allow(lease).to receive(:try_obtain_with_ttl).and_return({ ttl: 0, uuid: 'uuid' })
allow(described_class).to receive(:exclusive_lease).and_return(lease)
expect_to_cancel_exclusive_lease(described_class::LEASE_KEY, 'uuid')
result = described_class.try_obtain_with_ttl { raise StandardError } result = described_class.try_obtain_with_ttl { raise StandardError }
......
...@@ -2,21 +2,26 @@ require 'spec_helper' ...@@ -2,21 +2,26 @@ require 'spec_helper'
describe Geo::FileDownloadService do describe Geo::FileDownloadService do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
before do before do
stub_current_geo_node(secondary) stub_current_geo_node(secondary)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true)
end end
shared_examples_for 'a service that downloads the file and registers the sync result' do |file_type| shared_examples_for 'a service that downloads the file and registers the sync result' do |file_type|
let(:download_service) { described_class.new(file_type, file.id) } let(:download_service) { described_class.new(file_type, file.id) }
let(:registry) { file_type == 'job_artifact' ? Geo::JobArtifactRegistry : Geo::FileRegistry } let(:registry) { file_type == 'job_artifact' ? Geo::JobArtifactRegistry : Geo::FileRegistry }
subject(:execute!) { download_service.execute } subject(:execute!) { download_service.execute }
before do
stub_exclusive_lease("file_download_service:#{file_type}:#{file.id}",
timeout: Geo::FileDownloadService::LEASE_TIMEOUT)
end
context 'for a new file' do context 'for a new file' do
context 'when the downloader fails before attempting a transfer' do context 'when the downloader fails before attempting a transfer' do
it 'logs that the download failed before attempting a transfer' do it 'logs that the download failed before attempting a transfer' do
......
...@@ -2,18 +2,18 @@ require 'spec_helper' ...@@ -2,18 +2,18 @@ require 'spec_helper'
describe Geo::FileRegistryRemovalService do describe Geo::FileRegistryRemovalService do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
before do before do
stub_current_geo_node(secondary) stub_current_geo_node(secondary)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true)
end end
describe '#execute' do describe '#execute' do
it 'delegates log_error to the Geo logger' do it 'delegates log_error to the Geo logger' do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(false) stub_exclusive_lease_taken("file_registry_removal_service:lfs:99")
expect(Gitlab::Geo::Logger).to receive(:error) expect(Gitlab::Geo::Logger).to receive(:error)
described_class.new(:lfs, 99).execute described_class.new(:lfs, 99).execute
...@@ -22,6 +22,11 @@ describe Geo::FileRegistryRemovalService do ...@@ -22,6 +22,11 @@ describe Geo::FileRegistryRemovalService do
shared_examples 'removes' do shared_examples 'removes' do
subject(:service) { described_class.new(file_registry.file_type, file_registry.file_id) } subject(:service) { described_class.new(file_registry.file_type, file_registry.file_id) }
before do
stub_exclusive_lease("file_registry_removal_service:#{file_registry.file_type}:#{file_registry.file_id}",
timeout: Geo::FileRegistryRemovalService::LEASE_TIMEOUT)
end
it 'file from disk' do it 'file from disk' do
expect do expect do
service.execute service.execute
...@@ -38,6 +43,11 @@ describe Geo::FileRegistryRemovalService do ...@@ -38,6 +43,11 @@ describe Geo::FileRegistryRemovalService do
shared_examples 'removes artifact' do shared_examples 'removes artifact' do
subject(:service) { described_class.new('job_artifact', registry.artifact_id) } subject(:service) { described_class.new('job_artifact', registry.artifact_id) }
before do
stub_exclusive_lease("file_registry_removal_service:job_artifact:#{registry.artifact_id}",
timeout: Geo::FileRegistryRemovalService::LEASE_TIMEOUT)
end
it 'file from disk' do it 'file from disk' do
expect do expect do
service.execute service.execute
......
...@@ -2,13 +2,15 @@ require 'spec_helper' ...@@ -2,13 +2,15 @@ require 'spec_helper'
describe Geo::RepositorySyncService do describe Geo::RepositorySyncService do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
let(:lease) { double(try_obtain: true) }
set(:project) { create(:project_empty_repo) } set(:project) { create(:project_empty_repo) }
let(:repository) { project.repository } let(:repository) { project.repository }
let(:lease_key) { "geo_sync_service:repository:#{project.id}" }
let(:lease_uuid) { 'uuid'}
subject { described_class.new(project) } subject { described_class.new(project) }
...@@ -23,7 +25,8 @@ describe Geo::RepositorySyncService do ...@@ -23,7 +25,8 @@ describe Geo::RepositorySyncService do
let(:url_to_repo) { "#{primary.url}#{project.full_path}.git" } let(:url_to_repo) { "#{primary.url}#{project.full_path}.git" }
before do before do
allow(subject).to receive(:exclusive_lease).and_return(lease) stub_exclusive_lease(lease_key, lease_uuid)
stub_exclusive_lease("geo_project_housekeeping:#{project.id}")
allow_any_instance_of(Repository).to receive(:fetch_as_mirror) allow_any_instance_of(Repository).to receive(:fetch_as_mirror)
.and_return(true) .and_return(true)
...@@ -47,8 +50,7 @@ describe Geo::RepositorySyncService do ...@@ -47,8 +50,7 @@ describe Geo::RepositorySyncService do
end end
it 'returns the lease when succeed' do it 'returns the lease when succeed' do
expect(Gitlab::ExclusiveLease).to receive(:cancel).once.with( expect_to_cancel_exclusive_lease(lease_key, lease_uuid)
subject.__send__(:lease_key), anything).and_call_original
subject.execute subject.execute
end end
...@@ -64,14 +66,13 @@ describe Geo::RepositorySyncService do ...@@ -64,14 +66,13 @@ describe Geo::RepositorySyncService do
.with(url_to_repo, remote_name: 'geo', forced: true) .with(url_to_repo, remote_name: 'geo', forced: true)
.and_raise(Gitlab::Shell::Error) .and_raise(Gitlab::Shell::Error)
expect(Gitlab::ExclusiveLease).to receive(:cancel).once.with( expect_to_cancel_exclusive_lease(lease_key, lease_uuid)
subject.__send__(:lease_key), anything).and_call_original
subject.execute subject.execute
end end
it 'does not fetch project repository if cannot obtain a lease' do it 'does not fetch project repository if cannot obtain a lease' do
allow(lease).to receive(:try_obtain) { false } stub_exclusive_lease_taken(lease_key)
expect(repository).not_to receive(:fetch_as_mirror) expect(repository).not_to receive(:fetch_as_mirror)
......
...@@ -2,13 +2,15 @@ require 'spec_helper' ...@@ -2,13 +2,15 @@ require 'spec_helper'
RSpec.describe Geo::WikiSyncService do RSpec.describe Geo::WikiSyncService do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
set(:project) { create(:project_empty_repo) }
let(:lease) { double(try_obtain: true, lease_key: anything ) }
let(:project) { create(:project_empty_repo) }
let(:repository) { project.wiki.repository } let(:repository) { project.wiki.repository }
let(:lease_key) { "geo_sync_service:wiki:#{project.id}" }
let(:lease_uuid) { 'uuid'}
subject { described_class.new(project) } subject { described_class.new(project) }
...@@ -23,7 +25,7 @@ RSpec.describe Geo::WikiSyncService do ...@@ -23,7 +25,7 @@ RSpec.describe Geo::WikiSyncService do
let(:url_to_repo) { "#{primary.url}#{project.full_path}.wiki.git" } let(:url_to_repo) { "#{primary.url}#{project.full_path}.wiki.git" }
before do before do
allow(subject).to receive(:exclusive_lease).and_return(lease) stub_exclusive_lease(lease_key, lease_uuid)
allow_any_instance_of(Repository).to receive(:fetch_as_mirror) allow_any_instance_of(Repository).to receive(:fetch_as_mirror)
.and_return(true) .and_return(true)
...@@ -39,8 +41,7 @@ RSpec.describe Geo::WikiSyncService do ...@@ -39,8 +41,7 @@ RSpec.describe Geo::WikiSyncService do
end end
it 'releases lease' do it 'releases lease' do
expect(Gitlab::ExclusiveLease).to receive(:cancel).at_least(:once).with( expect_to_cancel_exclusive_lease(lease_key, lease_uuid)
subject.__send__(:lease_key), anything).and_call_original
subject.execute subject.execute
end end
...@@ -52,7 +53,7 @@ RSpec.describe Geo::WikiSyncService do ...@@ -52,7 +53,7 @@ RSpec.describe Geo::WikiSyncService do
end end
it 'does not fetch wiki repository if cannot obtain a lease' do it 'does not fetch wiki repository if cannot obtain a lease' do
allow(lease).to receive(:try_obtain) { false } stub_exclusive_lease_taken(lease_key)
expect(repository).not_to receive(:fetch_as_mirror) expect(repository).not_to receive(:fetch_as_mirror)
......
...@@ -2,13 +2,18 @@ require 'spec_helper' ...@@ -2,13 +2,18 @@ require 'spec_helper'
describe Geo::PruneEventLogWorker, :geo do describe Geo::PruneEventLogWorker, :geo do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
subject(:worker) { described_class.new } subject(:worker) { described_class.new }
set(:primary) { create(:geo_node, :primary) } set(:primary) { create(:geo_node, :primary) }
set(:secondary) { create(:geo_node) } set(:secondary) { create(:geo_node) }
let(:lease_key) { 'geo/prune_event_log_worker' }
let(:lease_timeout) { Geo::PruneEventLogWorker::LEASE_TIMEOUT }
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true) stub_exclusive_lease(lease_key, timeout: lease_timeout)
end end
describe '#perform' do describe '#perform' do
...@@ -30,7 +35,7 @@ describe Geo::PruneEventLogWorker, :geo do ...@@ -30,7 +35,7 @@ describe Geo::PruneEventLogWorker, :geo do
end end
it 'logs error when it cannot obtain lease' do it 'logs error when it cannot obtain lease' do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { nil } stub_exclusive_lease_taken(lease_key, timeout: lease_timeout)
expect(worker).to receive(:log_error).with(/^Cannot obtain an exclusive lease/) expect(worker).to receive(:log_error).with(/^Cannot obtain an exclusive lease/)
......
...@@ -2,10 +2,12 @@ require 'spec_helper' ...@@ -2,10 +2,12 @@ require 'spec_helper'
describe Geo::RepositoriesCleanUpWorker do describe Geo::RepositoriesCleanUpWorker do
describe '#perform' do describe '#perform' do
include ExclusiveLeaseHelpers
let(:geo_node) { create(:geo_node) } let(:geo_node) { create(:geo_node) }
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { true } stub_exclusive_lease
end end
context 'when node has namespace restrictions' do context 'when node has namespace restrictions' do
...@@ -68,7 +70,7 @@ describe Geo::RepositoriesCleanUpWorker do ...@@ -68,7 +70,7 @@ describe Geo::RepositoriesCleanUpWorker do
end end
it 'does not perform GeoRepositoryDestroyWorker when cannnot obtain a lease' do it 'does not perform GeoRepositoryDestroyWorker when cannnot obtain a lease' do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { false } stub_exclusive_lease_taken
expect(GeoRepositoryDestroyWorker).not_to receive(:perform_async) expect(GeoRepositoryDestroyWorker).not_to receive(:perform_async)
......
...@@ -4,6 +4,7 @@ require 'spec_helper' ...@@ -4,6 +4,7 @@ require 'spec_helper'
# can't see changes inside a transaction of a different connection. # can't see changes inside a transaction of a different connection.
describe Geo::RepositoryShardSyncWorker, :geo, :delete, :clean_gitlab_redis_cache do describe Geo::RepositoryShardSyncWorker, :geo, :delete, :clean_gitlab_redis_cache do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
let!(:primary) { create(:geo_node, :primary) } let!(:primary) { create(:geo_node, :primary) }
let!(:secondary) { create(:geo_node) } let!(:secondary) { create(:geo_node) }
...@@ -25,8 +26,7 @@ describe Geo::RepositoryShardSyncWorker, :geo, :delete, :clean_gitlab_redis_cach ...@@ -25,8 +26,7 @@ describe Geo::RepositoryShardSyncWorker, :geo, :delete, :clean_gitlab_redis_cach
end end
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { true } stub_exclusive_lease(renew: true)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew) { true }
Gitlab::ShardHealthCache.update([shard_name]) Gitlab::ShardHealthCache.update([shard_name])
end end
...@@ -48,6 +48,7 @@ describe Geo::RepositoryShardSyncWorker, :geo, :delete, :clean_gitlab_redis_cach ...@@ -48,6 +48,7 @@ describe Geo::RepositoryShardSyncWorker, :geo, :delete, :clean_gitlab_redis_cach
it 'does not perform Geo::ProjectSyncWorker when shard becomes unhealthy' do it 'does not perform Geo::ProjectSyncWorker when shard becomes unhealthy' do
Gitlab::ShardHealthCache.update([]) Gitlab::ShardHealthCache.update([])
expect(Geo::ProjectSyncWorker).not_to receive(:perform_async) expect(Geo::ProjectSyncWorker).not_to receive(:perform_async)
subject.perform(shard_name) subject.perform(shard_name)
......
...@@ -2,6 +2,7 @@ require 'spec_helper' ...@@ -2,6 +2,7 @@ require 'spec_helper'
describe Geo::RepositoryVerification::Primary::ShardWorker, :postgresql, :clean_gitlab_redis_cache do describe Geo::RepositoryVerification::Primary::ShardWorker, :postgresql, :clean_gitlab_redis_cache do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
let!(:primary) { create(:geo_node, :primary) } let!(:primary) { create(:geo_node, :primary) }
let(:shard_name) { Gitlab.config.repositories.storages.keys.first } let(:shard_name) { Gitlab.config.repositories.storages.keys.first }
...@@ -13,8 +14,7 @@ describe Geo::RepositoryVerification::Primary::ShardWorker, :postgresql, :clean_ ...@@ -13,8 +14,7 @@ describe Geo::RepositoryVerification::Primary::ShardWorker, :postgresql, :clean_
describe '#perform' do describe '#perform' do
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { true } stub_exclusive_lease(renew: true)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew) { true }
Gitlab::ShardHealthCache.update([shard_name]) Gitlab::ShardHealthCache.update([shard_name])
end end
......
...@@ -2,6 +2,7 @@ require 'spec_helper' ...@@ -2,6 +2,7 @@ require 'spec_helper'
describe Geo::RepositoryVerification::Primary::SingleWorker, :postgresql, :clean_gitlab_redis_cache do describe Geo::RepositoryVerification::Primary::SingleWorker, :postgresql, :clean_gitlab_redis_cache do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
set(:project) { create(:project) } set(:project) { create(:project) }
...@@ -11,13 +12,10 @@ describe Geo::RepositoryVerification::Primary::SingleWorker, :postgresql, :clean ...@@ -11,13 +12,10 @@ describe Geo::RepositoryVerification::Primary::SingleWorker, :postgresql, :clean
before do before do
stub_current_geo_node(primary) stub_current_geo_node(primary)
stub_exclusive_lease
end end
describe '#perform' do describe '#perform' do
before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { true }
end
it 'does not calculate the checksum when not running on a primary' do it 'does not calculate the checksum when not running on a primary' do
stub_project_repository(project, repository) stub_project_repository(project, repository)
stub_wiki_repository(project.wiki, wiki) stub_wiki_repository(project.wiki, wiki)
......
...@@ -2,6 +2,7 @@ require 'spec_helper' ...@@ -2,6 +2,7 @@ require 'spec_helper'
describe Geo::RepositoryVerification::Secondary::ShardWorker, :postgresql, :clean_gitlab_redis_cache do describe Geo::RepositoryVerification::Secondary::ShardWorker, :postgresql, :clean_gitlab_redis_cache do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
let!(:secondary) { create(:geo_node) } let!(:secondary) { create(:geo_node) }
let(:shard_name) { Gitlab.config.repositories.storages.keys.first } let(:shard_name) { Gitlab.config.repositories.storages.keys.first }
...@@ -16,8 +17,7 @@ describe Geo::RepositoryVerification::Secondary::ShardWorker, :postgresql, :clea ...@@ -16,8 +17,7 @@ describe Geo::RepositoryVerification::Secondary::ShardWorker, :postgresql, :clea
describe '#perform' do describe '#perform' do
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { true } stub_exclusive_lease(renew: true)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:renew) { true }
Gitlab::ShardHealthCache.update([shard_name]) Gitlab::ShardHealthCache.update([shard_name])
end end
......
...@@ -2,6 +2,7 @@ require 'spec_helper' ...@@ -2,6 +2,7 @@ require 'spec_helper'
describe Geo::RepositoryVerification::Secondary::SingleWorker, :postgresql, :clean_gitlab_redis_cache do describe Geo::RepositoryVerification::Secondary::SingleWorker, :postgresql, :clean_gitlab_redis_cache do
include ::EE::GeoHelpers include ::EE::GeoHelpers
include ExclusiveLeaseHelpers
let!(:secondary) { create(:geo_node) } let!(:secondary) { create(:geo_node) }
let(:project) { create(:project, :repository, :wiki_repo) } let(:project) { create(:project, :repository, :wiki_repo) }
...@@ -9,13 +10,10 @@ describe Geo::RepositoryVerification::Secondary::SingleWorker, :postgresql, :cle ...@@ -9,13 +10,10 @@ describe Geo::RepositoryVerification::Secondary::SingleWorker, :postgresql, :cle
before do before do
stub_current_geo_node(secondary) stub_current_geo_node(secondary)
stub_exclusive_lease
end end
describe '#perform' do describe '#perform' do
before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { true }
end
it 'does not calculate the checksum when not running on a secondary' do it 'does not calculate the checksum when not running on a secondary' do
allow(Gitlab::Geo).to receive(:secondary?) { false } allow(Gitlab::Geo).to receive(:secondary?) { false }
......
require 'rails_helper' require 'rails_helper'
describe RepositoryUpdateMirrorWorker do describe RepositoryUpdateMirrorWorker do
include ExclusiveLeaseHelpers
describe '#perform' do describe '#perform' do
let(:jid) { '12345678' } let(:jid) { '12345678' }
let!(:project) { create(:project) } let!(:project) { create(:project) }
let!(:import_state) { create(:import_state, :mirror, :scheduled, project: project) } let!(:import_state) { create(:import_state, :mirror, :scheduled, project: project) }
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true)
allow(subject).to receive(:jid).and_return(jid) allow(subject).to receive(:jid).and_return(jid)
end end
...@@ -56,6 +57,10 @@ describe RepositoryUpdateMirrorWorker do ...@@ -56,6 +57,10 @@ describe RepositoryUpdateMirrorWorker do
end end
context 'when we obtain the lease' do context 'when we obtain the lease' do
before do
allow(stub_exclusive_lease).to receive(:exists?).and_return(false)
end
it 'performs UpdateAllMirrorsWorker when reschedule_immediately? returns true' do it 'performs UpdateAllMirrorsWorker when reschedule_immediately? returns true' do
allow(Gitlab::Mirror).to receive(:reschedule_immediately?).and_return(true) allow(Gitlab::Mirror).to receive(:reschedule_immediately?).and_return(true)
...@@ -74,7 +79,7 @@ describe RepositoryUpdateMirrorWorker do ...@@ -74,7 +79,7 @@ describe RepositoryUpdateMirrorWorker do
end end
it 'does not perform UpdateAllMirrorsWorker when we cannot obtain the lease' do it 'does not perform UpdateAllMirrorsWorker when we cannot obtain the lease' do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(false) stub_exclusive_lease_taken
expect(UpdateAllMirrorsWorker).not_to receive(:perform_async) expect(UpdateAllMirrorsWorker).not_to receive(:perform_async)
...@@ -82,7 +87,7 @@ describe RepositoryUpdateMirrorWorker do ...@@ -82,7 +87,7 @@ describe RepositoryUpdateMirrorWorker do
end end
it 'does not perform UpdateAllMirrorsWorker when the lease already exists' do it 'does not perform UpdateAllMirrorsWorker when the lease already exists' do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:exists?).and_return(true) stub_exclusive_lease_taken
expect(UpdateAllMirrorsWorker).not_to receive(:perform_async) expect(UpdateAllMirrorsWorker).not_to receive(:perform_async)
......
require 'spec_helper' require 'spec_helper'
describe UpdateAllMirrorsWorker do describe UpdateAllMirrorsWorker do
include ExclusiveLeaseHelpers
subject(:worker) { described_class.new } subject(:worker) { described_class.new }
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true) stub_exclusive_lease
end end
describe '#perform' do describe '#perform' do
it 'does not execute if cannot get the lease' do it 'does not execute if cannot get the lease' do
create(:project, :mirror) stub_exclusive_lease_taken
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(false)
expect(worker).not_to receive(:schedule_mirrors!) expect(worker).not_to receive(:schedule_mirrors!)
......
module ExclusiveLeaseHelpers module ExclusiveLeaseHelpers
def stub_exclusive_lease(key = nil, uuid = 'uuid', timeout: nil) def stub_exclusive_lease(key = nil, uuid = 'uuid', renew: false, timeout: nil)
key ||= instance_of(String) key ||= instance_of(String)
timeout ||= instance_of(Integer) timeout ||= instance_of(Integer)
lease = instance_double(Gitlab::ExclusiveLease, try_obtain: uuid)
lease = instance_double(
Gitlab::ExclusiveLease,
try_obtain: uuid,
exists?: true,
renew: renew
)
allow(Gitlab::ExclusiveLease) allow(Gitlab::ExclusiveLease)
.to receive(:new) .to receive(:new)
......
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