2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-28 14:51:36 -04:00
|
|
|
module ExclusiveLeaseHelpers
|
|
|
|
def stub_exclusive_lease(key = nil, uuid = 'uuid', renew: false, timeout: nil)
|
2022-09-28 08:07:50 -04:00
|
|
|
prepare_exclusive_lease_stub
|
|
|
|
|
2018-06-28 14:51:36 -04:00
|
|
|
key ||= instance_of(String)
|
|
|
|
timeout ||= instance_of(Integer)
|
|
|
|
|
|
|
|
lease = instance_double(
|
|
|
|
Gitlab::ExclusiveLease,
|
|
|
|
try_obtain: uuid,
|
|
|
|
exists?: true,
|
2020-05-06 02:09:36 -04:00
|
|
|
renew: renew,
|
2020-05-12 14:07:54 -04:00
|
|
|
cancel: nil,
|
|
|
|
ttl: timeout
|
2018-06-28 14:51:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2022-09-28 08:07:50 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# This prepares the stub to be able to stub specific lease keys
|
|
|
|
# while allowing unstubbed lease keys to behave as original.
|
|
|
|
#
|
|
|
|
# allow(Gitlab::ExclusiveLease).to receive(:new).and_call_original
|
|
|
|
# can only be called once to prevent resetting stubs when
|
|
|
|
# `stub_exclusive_lease` is called multiple times.
|
|
|
|
def prepare_exclusive_lease_stub
|
|
|
|
return if @exclusive_lease_allowed_to_call_original
|
|
|
|
|
|
|
|
allow(Gitlab::ExclusiveLease)
|
|
|
|
.to receive(:new).and_call_original
|
|
|
|
|
|
|
|
@exclusive_lease_allowed_to_call_original = true
|
|
|
|
end
|
2018-06-28 14:51:36 -04:00
|
|
|
end
|