Commit efaca5de authored by Robert Speicher's avatar Robert Speicher

Merge branch 'da-exclusive-lease-helpers' into 'master'

Add helper methods to stub Gitlab::ExclusiveLease

See merge request gitlab-org/gitlab-ce!20253
parents 70cf3bff 34dbccb2
require 'spec_helper' require 'spec_helper'
describe Mattermost::Session, type: :request do describe Mattermost::Session, type: :request do
include ExclusiveLeaseHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
let(:gitlab_url) { "http://gitlab.com" } let(:gitlab_url) { "http://gitlab.com" }
...@@ -97,26 +99,20 @@ describe Mattermost::Session, type: :request do ...@@ -97,26 +99,20 @@ describe Mattermost::Session, type: :request do
end end
end end
context 'with lease' do context 'exclusive lease' do
before do let(:lease_key) { 'mattermost:session' }
allow(subject).to receive(:lease_try_obtain).and_return('aldkfjsldfk')
end
it 'tries to obtain a lease' do it 'tries to obtain a lease' do
expect(subject).to receive(:lease_try_obtain) expect_to_obtain_exclusive_lease(lease_key, 'uuid')
expect(Gitlab::ExclusiveLease).to receive(:cancel) expect_to_cancel_exclusive_lease(lease_key, 'uuid')
# Cannot setup a session, but we should still cancel the lease # Cannot setup a session, but we should still cancel the lease
expect { subject.with_session }.to raise_error(Mattermost::NoSessionError) expect { subject.with_session }.to raise_error(Mattermost::NoSessionError)
end end
end
context 'without lease' do it 'returns a NoSessionError error without lease' do
before do stub_exclusive_lease_taken(lease_key)
allow(subject).to receive(:lease_try_obtain).and_return(nil)
end
it 'returns a NoSessionError error' do
expect { subject.with_session }.to raise_error(Mattermost::NoSessionError) expect { subject.with_session }.to raise_error(Mattermost::NoSessionError)
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
include ExclusiveLeaseHelpers
set(:build) { create(:ci_build, :running) } set(:build) { create(:ci_build, :running) }
let(:chunk_index) { 0 } let(:chunk_index) { 0 }
let(:data_store) { :redis } let(:data_store) { :redis }
...@@ -322,7 +324,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do ...@@ -322,7 +324,7 @@ describe Ci::BuildTraceChunk, :clean_gitlab_redis_shared_state do
describe 'ExclusiveLock' do describe 'ExclusiveLock' do
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { nil } stub_exclusive_lease_taken
stub_const('Ci::BuildTraceChunk::WRITE_LOCK_RETRY', 1) stub_const('Ci::BuildTraceChunk::WRITE_LOCK_RETRY', 1)
end end
......
require 'spec_helper' require 'spec_helper'
describe ReactiveCaching, :use_clean_rails_memory_store_caching do describe ReactiveCaching, :use_clean_rails_memory_store_caching do
include ExclusiveLeaseHelpers
include ReactiveCachingHelpers include ReactiveCachingHelpers
class CacheTest class CacheTest
...@@ -106,8 +107,8 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do ...@@ -106,8 +107,8 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end end
it 'takes and releases the lease' do it 'takes and releases the lease' do
expect_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return("000000") expect_to_obtain_exclusive_lease(cache_key, 'uuid')
expect(Gitlab::ExclusiveLease).to receive(:cancel).with(cache_key, "000000") expect_to_cancel_exclusive_lease(cache_key, 'uuid')
go! go!
end end
...@@ -153,11 +154,9 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do ...@@ -153,11 +154,9 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end end
context 'when the lease is already taken' do context 'when the lease is already taken' do
before do
expect_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(nil)
end
it 'skips the calculation' do it 'skips the calculation' do
stub_exclusive_lease_taken(cache_key)
expect(instance).to receive(:calculate_reactive_cache).never expect(instance).to receive(:calculate_reactive_cache).never
go! go!
......
require 'spec_helper' require 'spec_helper'
describe Clusters::Applications::CheckIngressIpAddressService do describe Clusters::Applications::CheckIngressIpAddressService do
include ExclusiveLeaseHelpers
let(:application) { create(:clusters_applications_ingress, :installed) } let(:application) { create(:clusters_applications_ingress, :installed) }
let(:service) { described_class.new(application) } let(:service) { described_class.new(application) }
let(:kubeclient) { double(::Kubeclient::Client, get_service: kube_service) } let(:kubeclient) { double(::Kubeclient::Client, get_service: kube_service) }
let(:ingress) { [{ ip: '111.222.111.222' }] } let(:ingress) { [{ ip: '111.222.111.222' }] }
let(:exclusive_lease) { instance_double(Gitlab::ExclusiveLease, try_obtain: true) } let(:lease_key) { "check_ingress_ip_address_service:#{application.id}" }
let(:kube_service) do let(:kube_service) do
::Kubeclient::Resource.new( ::Kubeclient::Resource.new(
...@@ -22,11 +24,8 @@ describe Clusters::Applications::CheckIngressIpAddressService do ...@@ -22,11 +24,8 @@ describe Clusters::Applications::CheckIngressIpAddressService do
subject { service.execute } subject { service.execute }
before do before do
stub_exclusive_lease(lease_key, timeout: 15.seconds.to_i)
allow(application.cluster).to receive(:kubeclient).and_return(kubeclient) allow(application.cluster).to receive(:kubeclient).and_return(kubeclient)
allow(Gitlab::ExclusiveLease)
.to receive(:new)
.with("check_ingress_ip_address_service:#{application.id}", timeout: 15.seconds.to_i)
.and_return(exclusive_lease)
end end
describe '#execute' do describe '#execute' do
...@@ -47,13 +46,9 @@ describe Clusters::Applications::CheckIngressIpAddressService do ...@@ -47,13 +46,9 @@ describe Clusters::Applications::CheckIngressIpAddressService do
end end
context 'when the exclusive lease cannot be obtained' do context 'when the exclusive lease cannot be obtained' do
before do
allow(exclusive_lease)
.to receive(:try_obtain)
.and_return(false)
end
it 'does not call kubeclient' do it 'does not call kubeclient' do
stub_exclusive_lease_taken(lease_key, timeout: 15.seconds.to_i)
subject subject
expect(kubeclient).not_to have_received(:get_service) expect(kubeclient).not_to have_received(:get_service)
......
require 'spec_helper' require 'spec_helper'
describe Users::RefreshAuthorizedProjectsService do describe Users::RefreshAuthorizedProjectsService do
include ExclusiveLeaseHelpers
# We're using let! here so that any expectations for the service class are not # We're using let! here so that any expectations for the service class are not
# triggered twice. # triggered twice.
let!(:project) { create(:project) } let!(:project) { create(:project) }
...@@ -10,12 +12,10 @@ describe Users::RefreshAuthorizedProjectsService do ...@@ -10,12 +12,10 @@ describe Users::RefreshAuthorizedProjectsService do
describe '#execute', :clean_gitlab_redis_shared_state do describe '#execute', :clean_gitlab_redis_shared_state do
it 'refreshes the authorizations using a lease' do it 'refreshes the authorizations using a lease' do
expect_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) lease_key = "refresh_authorized_projects:#{user.id}"
.and_return('foo')
expect(Gitlab::ExclusiveLease).to receive(:cancel)
.with(an_instance_of(String), 'foo')
expect_to_obtain_exclusive_lease(lease_key, 'uuid')
expect_to_cancel_exclusive_lease(lease_key, 'uuid')
expect(service).to receive(:execute_without_lease) expect(service).to receive(:execute_without_lease)
service.execute service.execute
......
module ExclusiveLeaseHelpers
def stub_exclusive_lease(key = nil, uuid = 'uuid', renew: false, timeout: nil)
key ||= instance_of(String)
timeout ||= instance_of(Integer)
lease = instance_double(
Gitlab::ExclusiveLease,
try_obtain: uuid,
exists?: true,
renew: renew
)
allow(Gitlab::ExclusiveLease)
.to receive(:new)
.with(key, timeout: timeout)
.and_return(lease)
lease
end
def stub_exclusive_lease_taken(key = nil, timeout: nil)
stub_exclusive_lease(key, nil, timeout: timeout)
end
def expect_to_obtain_exclusive_lease(key, uuid = 'uuid', timeout: nil)
lease = stub_exclusive_lease(key, uuid, timeout: timeout)
expect(lease).to receive(:try_obtain)
end
def expect_to_cancel_exclusive_lease(key, uuid)
expect(Gitlab::ExclusiveLease)
.to receive(:cancel)
.with(key, uuid)
end
end
...@@ -247,8 +247,10 @@ shared_examples_for 'common trace features' do ...@@ -247,8 +247,10 @@ shared_examples_for 'common trace features' do
end end
context 'when another process has already been archiving', :clean_gitlab_redis_shared_state do context 'when another process has already been archiving', :clean_gitlab_redis_shared_state do
include ExclusiveLeaseHelpers
before do before do
Gitlab::ExclusiveLease.new("trace:archive:#{trace.job.id}", timeout: 1.hour).try_obtain stub_exclusive_lease_taken("trace:archive:#{trace.job.id}", timeout: 1.hour)
end end
it 'blocks concurrent archiving' do it 'blocks concurrent archiving' do
......
...@@ -76,8 +76,10 @@ shared_examples "migrates" do |to_store:, from_store: nil| ...@@ -76,8 +76,10 @@ shared_examples "migrates" do |to_store:, from_store: nil|
end end
context 'when migrate! is occupied by another process' do context 'when migrate! is occupied by another process' do
include ExclusiveLeaseHelpers
before do before do
@uuid = Gitlab::ExclusiveLease.new(subject.exclusive_lease_key, timeout: 1.hour.to_i).try_obtain stub_exclusive_lease_taken(subject.exclusive_lease_key, timeout: 1.hour.to_i)
end end
it 'does not execute migrate!' do it 'does not execute migrate!' do
...@@ -91,10 +93,6 @@ shared_examples "migrates" do |to_store:, from_store: nil| ...@@ -91,10 +93,6 @@ shared_examples "migrates" do |to_store:, from_store: nil|
expect { subject.use_file }.to raise_error(ObjectStorage::ExclusiveLeaseTaken) expect { subject.use_file }.to raise_error(ObjectStorage::ExclusiveLeaseTaken)
end end
after do
Gitlab::ExclusiveLease.cancel(subject.exclusive_lease_key, @uuid)
end
end end
context 'migration is unsuccessful' do context 'migration is unsuccessful' do
......
require 'spec_helper' require 'spec_helper'
describe ProjectCacheWorker do describe ProjectCacheWorker do
include ExclusiveLeaseHelpers
let(:worker) { described_class.new } let(:worker) { described_class.new }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:statistics) { project.statistics } let(:statistics) { project.statistics }
let(:lease_key) { "project_cache_worker:#{project.id}:update_statistics" }
let(:lease_timeout) { ProjectCacheWorker::LEASE_TIMEOUT }
describe '#perform' do describe '#perform' do
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) stub_exclusive_lease(lease_key, timeout: lease_timeout)
.and_return(true)
end end
context 'with a non-existing project' do context 'with a non-existing project' do
...@@ -63,9 +66,7 @@ describe ProjectCacheWorker do ...@@ -63,9 +66,7 @@ describe ProjectCacheWorker do
describe '#update_statistics' do describe '#update_statistics' do
context 'when a lease could not be obtained' do context 'when a lease could not be obtained' do
it 'does not update the repository size' do it 'does not update the repository size' do
allow(worker).to receive(:try_obtain_lease_for) stub_exclusive_lease_taken(lease_key, timeout: lease_timeout)
.with(project.id, :update_statistics)
.and_return(false)
expect(statistics).not_to receive(:refresh!) expect(statistics).not_to receive(:refresh!)
...@@ -75,9 +76,7 @@ describe ProjectCacheWorker do ...@@ -75,9 +76,7 @@ describe ProjectCacheWorker do
context 'when a lease could be obtained' do context 'when a lease could be obtained' do
it 'updates the project statistics' do it 'updates the project statistics' do
allow(worker).to receive(:try_obtain_lease_for) stub_exclusive_lease(lease_key, timeout: lease_timeout)
.with(project.id, :update_statistics)
.and_return(true)
expect(statistics).to receive(:refresh!) expect(statistics).to receive(:refresh!)
.with(only: %i(repository_size)) .with(only: %i(repository_size))
......
require 'spec_helper' require 'spec_helper'
describe ProjectMigrateHashedStorageWorker, :clean_gitlab_redis_shared_state do describe ProjectMigrateHashedStorageWorker, :clean_gitlab_redis_shared_state do
include ExclusiveLeaseHelpers
describe '#perform' do describe '#perform' do
let(:project) { create(:project, :empty_repo) } let(:project) { create(:project, :empty_repo) }
let(:pending_delete_project) { create(:project, :empty_repo, pending_delete: true) } let(:lease_key) { "project_migrate_hashed_storage_worker:#{project.id}" }
let(:lease_timeout) { ProjectMigrateHashedStorageWorker::LEASE_TIMEOUT }
it 'skips when project no longer exists' do
expect(::Projects::HashedStorageMigrationService).not_to receive(:new)
subject.perform(-1)
end
context 'when have exclusive lease' do it 'skips when project is pending delete' do
before do pending_delete_project = create(:project, :empty_repo, pending_delete: true)
lease = subject.lease_for(project.id)
allow(Gitlab::ExclusiveLease).to receive(:new).and_return(lease) expect(::Projects::HashedStorageMigrationService).not_to receive(:new)
allow(lease).to receive(:try_obtain).and_return(true)
end
it 'skips when project no longer exists' do subject.perform(pending_delete_project.id)
nonexistent_id = 999999999999 end
expect(::Projects::HashedStorageMigrationService).not_to receive(:new) it 'delegates removal to service class when have exclusive lease' do
subject.perform(nonexistent_id) stub_exclusive_lease(lease_key, 'uuid', timeout: lease_timeout)
end
it 'skips when project is pending delete' do migration_service = spy
expect(::Projects::HashedStorageMigrationService).not_to receive(:new)
subject.perform(pending_delete_project.id) allow(::Projects::HashedStorageMigrationService)
end .to receive(:new).with(project, subject.logger)
.and_return(migration_service)
it 'delegates removal to service class' do subject.perform(project.id)
service = double('service')
expect(::Projects::HashedStorageMigrationService).to receive(:new).with(project, subject.logger).and_return(service)
expect(service).to receive(:execute)
subject.perform(project.id) expect(migration_service).to have_received(:execute)
end
end end
context 'when dont have exclusive lease' do it 'skips when dont have lease when dont have exclusive lease' do
before do stub_exclusive_lease_taken(lease_key, timeout: lease_timeout)
lease = subject.lease_for(project.id)
allow(Gitlab::ExclusiveLease).to receive(:new).and_return(lease)
allow(lease).to receive(:try_obtain).and_return(false)
end
it 'skips when dont have lease' do expect(::Projects::HashedStorageMigrationService).not_to receive(:new)
expect(::Projects::HashedStorageMigrationService).not_to receive(:new)
subject.perform(project.id) subject.perform(project.id)
end
end end
end end
end end
require 'spec_helper' require 'spec_helper'
describe PropagateServiceTemplateWorker do describe PropagateServiceTemplateWorker do
let!(:service_template) do include ExclusiveLeaseHelpers
PushoverService.create(
template: true,
active: true,
properties: {
device: 'MyDevice',
sound: 'mic',
priority: 4,
user_key: 'asdf',
api_key: '123456789'
})
end
before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain)
.and_return(true)
end
describe '#perform' do describe '#perform' do
it 'calls the propagate service with the template' do it 'calls the propagate service with the template' do
expect(Projects::PropagateServiceTemplate).to receive(:propagate).with(service_template) template = PushoverService.create(
template: true,
active: true,
properties: {
device: 'MyDevice',
sound: 'mic',
priority: 4,
user_key: 'asdf',
api_key: '123456789'
})
stub_exclusive_lease("propagate_service_template_worker:#{template.id}",
timeout: PropagateServiceTemplateWorker::LEASE_TIMEOUT)
expect(Projects::PropagateServiceTemplate)
.to receive(:propagate)
.with(template)
subject.perform(service_template.id) subject.perform(template.id)
end end
end end
end end
require 'rails_helper' require 'rails_helper'
describe RepositoryRemoveRemoteWorker do describe RepositoryRemoveRemoteWorker do
subject(:worker) { described_class.new } include ExclusiveLeaseHelpers
describe '#perform' do describe '#perform' do
let(:remote_name) { 'joe'}
let!(:project) { create(:project, :repository) } let!(:project) { create(:project, :repository) }
let(:remote_name) { 'joe'}
let(:lease_key) { "remove_remote_#{project.id}_#{remote_name}" }
let(:lease_timeout) { RepositoryRemoveRemoteWorker::LEASE_TIMEOUT }
context 'when it cannot obtain lease' do it 'returns nil when project does not exist' do
it 'logs error' do expect(subject.perform(-1, 'remote_name')).to be_nil
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain) { nil }
expect_any_instance_of(Repository).not_to receive(:remove_remote)
expect(worker).to receive(:log_error).with('Cannot obtain an exclusive lease. There must be another instance already in execution.')
worker.perform(project.id, remote_name)
end
end end
context 'when it gets the lease' do context 'when project exists' do
before do before do
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(true) allow(Project)
.to receive(:find_by)
.with(id: project.id)
.and_return(project)
end end
context 'when project does not exist' do it 'does not remove remote when cannot obtain lease' do
it 'returns nil' do stub_exclusive_lease_taken(lease_key, timeout: lease_timeout)
expect(worker.perform(-1, 'remote_name')).to be_nil
end expect(project.repository)
end .not_to receive(:remove_remote)
context 'when project exists' do expect(subject)
it 'removes remote from repository' do .to receive(:log_error)
masterrev = project.repository.find_branch('master').dereferenced_target .with('Cannot obtain an exclusive lease. There must be another instance already in execution.')
create_remote_branch(remote_name, 'remote_branch', masterrev) subject.perform(project.id, remote_name)
end
expect_any_instance_of(Repository).to receive(:remove_remote).with(remote_name).and_call_original it 'removes remote from repository when obtain a lease' do
stub_exclusive_lease(lease_key, timeout: lease_timeout)
masterrev = project.repository.find_branch('master').dereferenced_target
create_remote_branch(remote_name, 'remote_branch', masterrev)
worker.perform(project.id, remote_name) expect(project.repository)
end .to receive(:remove_remote)
.with(remote_name)
.and_call_original
subject.perform(project.id, remote_name)
end end
end end
end end
...@@ -47,6 +53,7 @@ describe RepositoryRemoveRemoteWorker do ...@@ -47,6 +53,7 @@ describe RepositoryRemoveRemoteWorker do
rugged = Gitlab::GitalyClient::StorageSettings.allow_disk_access do rugged = Gitlab::GitalyClient::StorageSettings.allow_disk_access do
project.repository.rugged project.repository.rugged
end end
rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", target.id) rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", target.id)
end end
end end
require 'spec_helper' require 'spec_helper'
describe StuckCiJobsWorker do describe StuckCiJobsWorker do
include ExclusiveLeaseHelpers
let!(:runner) { create :ci_runner } let!(:runner) { create :ci_runner }
let!(:job) { create :ci_build, runner: runner } let!(:job) { create :ci_build, runner: runner }
let(:worker) { described_class.new } let(:trace_lease_key) { "trace:archive:#{job.id}" }
let(:exclusive_lease_uuid) { SecureRandom.uuid } let(:trace_lease_uuid) { SecureRandom.uuid }
let(:worker_lease_key) { StuckCiJobsWorker::EXCLUSIVE_LEASE_KEY }
let(:worker_lease_uuid) { SecureRandom.uuid }
subject(:worker) { described_class.new }
before do before do
stub_exclusive_lease(worker_lease_key, worker_lease_uuid)
stub_exclusive_lease(trace_lease_key, trace_lease_uuid)
job.update!(status: status, updated_at: updated_at) job.update!(status: status, updated_at: updated_at)
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(exclusive_lease_uuid)
end end
shared_examples 'job is dropped' do shared_examples 'job is dropped' do
...@@ -44,16 +51,19 @@ describe StuckCiJobsWorker do ...@@ -44,16 +51,19 @@ describe StuckCiJobsWorker do
context 'when job was not updated for more than 1 day ago' do context 'when job was not updated for more than 1 day ago' do
let(:updated_at) { 2.days.ago } let(:updated_at) { 2.days.ago }
it_behaves_like 'job is dropped' it_behaves_like 'job is dropped'
end end
context 'when job was updated in less than 1 day ago' do context 'when job was updated in less than 1 day ago' do
let(:updated_at) { 6.hours.ago } let(:updated_at) { 6.hours.ago }
it_behaves_like 'job is unchanged' it_behaves_like 'job is unchanged'
end end
context 'when job was not updated for more than 1 hour ago' do context 'when job was not updated for more than 1 hour ago' do
let(:updated_at) { 2.hours.ago } let(:updated_at) { 2.hours.ago }
it_behaves_like 'job is unchanged' it_behaves_like 'job is unchanged'
end end
end end
...@@ -65,11 +75,14 @@ describe StuckCiJobsWorker do ...@@ -65,11 +75,14 @@ describe StuckCiJobsWorker do
context 'when job was not updated for more than 1 hour ago' do context 'when job was not updated for more than 1 hour ago' do
let(:updated_at) { 2.hours.ago } let(:updated_at) { 2.hours.ago }
it_behaves_like 'job is dropped' it_behaves_like 'job is dropped'
end end
context 'when job was updated in less than 1 hour ago' do context 'when job was updated in less than 1
hour ago' do
let(:updated_at) { 30.minutes.ago } let(:updated_at) { 30.minutes.ago }
it_behaves_like 'job is unchanged' it_behaves_like 'job is unchanged'
end end
end end
...@@ -80,11 +93,13 @@ describe StuckCiJobsWorker do ...@@ -80,11 +93,13 @@ describe StuckCiJobsWorker do
context 'when job was not updated for more than 1 hour ago' do context 'when job was not updated for more than 1 hour ago' do
let(:updated_at) { 2.hours.ago } let(:updated_at) { 2.hours.ago }
it_behaves_like 'job is dropped' it_behaves_like 'job is dropped'
end end
context 'when job was updated in less than 1 hour ago' do context 'when job was updated in less than 1 hour ago' do
let(:updated_at) { 30.minutes.ago } let(:updated_at) { 30.minutes.ago }
it_behaves_like 'job is unchanged' it_behaves_like 'job is unchanged'
end end
end end
...@@ -93,6 +108,7 @@ describe StuckCiJobsWorker do ...@@ -93,6 +108,7 @@ describe StuckCiJobsWorker do
context "when job is #{status}" do context "when job is #{status}" do
let(:status) { status } let(:status) { status }
let(:updated_at) { 2.days.ago } let(:updated_at) { 2.days.ago }
it_behaves_like 'job is unchanged' it_behaves_like 'job is unchanged'
end end
end end
...@@ -119,23 +135,27 @@ describe StuckCiJobsWorker do ...@@ -119,23 +135,27 @@ describe StuckCiJobsWorker do
it 'is guard by exclusive lease when executed concurrently' do it 'is guard by exclusive lease when executed concurrently' do
expect(worker).to receive(:drop).at_least(:once).and_call_original expect(worker).to receive(:drop).at_least(:once).and_call_original
expect(worker2).not_to receive(:drop) expect(worker2).not_to receive(:drop)
worker.perform worker.perform
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).and_return(false)
stub_exclusive_lease_taken(worker_lease_key)
worker2.perform worker2.perform
end end
it 'can be executed in sequence' do it 'can be executed in sequence' do
expect(worker).to receive(:drop).at_least(:once).and_call_original expect(worker).to receive(:drop).at_least(:once).and_call_original
expect(worker2).to receive(:drop).at_least(:once).and_call_original expect(worker2).to receive(:drop).at_least(:once).and_call_original
worker.perform worker.perform
worker2.perform worker2.perform
end end
it 'cancels exclusive lease after worker perform' do it 'cancels exclusive leases after worker perform' do
worker.perform expect_to_cancel_exclusive_lease(trace_lease_key, trace_lease_uuid)
expect_to_cancel_exclusive_lease(worker_lease_key, worker_lease_uuid)
expect(Gitlab::ExclusiveLease.new(described_class::EXCLUSIVE_LEASE_KEY, timeout: 1.hour)) worker.perform
.not_to be_exists
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