2019-11-08 03:06:48 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-03 07:20:27 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::ExclusiveLeaseHelpers, :clean_gitlab_redis_shared_state do
|
2018-07-03 07:20:27 +00:00
|
|
|
include ::ExclusiveLeaseHelpers
|
|
|
|
|
|
|
|
let(:class_instance) { (Class.new { include ::Gitlab::ExclusiveLeaseHelpers }).new }
|
|
|
|
let(:unique_key) { SecureRandom.hex(10) }
|
|
|
|
|
|
|
|
describe '#in_lock' do
|
2022-08-02 03:09:33 +00:00
|
|
|
subject { class_instance.in_lock(unique_key, **options) {} }
|
2018-07-03 07:20:27 +00:00
|
|
|
|
2018-07-04 04:28:18 +00:00
|
|
|
let(:options) { {} }
|
2018-07-03 07:20:27 +00:00
|
|
|
|
2018-11-23 16:25:11 +00:00
|
|
|
context 'when unique key is not set' do
|
2022-08-02 03:09:33 +00:00
|
|
|
let(:unique_key) {}
|
2018-11-23 16:25:11 +00:00
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject }.to raise_error ArgumentError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-03 07:20:27 +00:00
|
|
|
context 'when the lease is not obtained yet' do
|
2020-05-06 06:09:36 +00:00
|
|
|
let!(:lease) { stub_exclusive_lease(unique_key, 'uuid') }
|
2018-07-03 07:20:27 +00:00
|
|
|
|
|
|
|
it 'calls the given block' do
|
2020-09-28 15:09:44 +00:00
|
|
|
expect { |b| class_instance.in_lock(unique_key, &b) }
|
|
|
|
.to yield_with_args(false, an_instance_of(described_class::SleepingLock))
|
2018-07-03 07:20:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls the given block continuously' do
|
2020-09-28 15:09:44 +00:00
|
|
|
expect { |b| class_instance.in_lock(unique_key, &b) }
|
|
|
|
.to yield_with_args(false, an_instance_of(described_class::SleepingLock))
|
|
|
|
expect { |b| class_instance.in_lock(unique_key, &b) }
|
|
|
|
.to yield_with_args(false, an_instance_of(described_class::SleepingLock))
|
|
|
|
expect { |b| class_instance.in_lock(unique_key, &b) }
|
|
|
|
.to yield_with_args(false, an_instance_of(described_class::SleepingLock))
|
2018-07-03 07:20:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'cancels the exclusive lease after the block' do
|
2020-05-06 06:09:36 +00:00
|
|
|
expect(lease).to receive(:cancel).once
|
2018-07-03 07:20:27 +00:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the lease is obtained already' do
|
|
|
|
let!(:lease) { stub_exclusive_lease_taken(unique_key) }
|
|
|
|
|
|
|
|
it 'retries to obtain a lease and raises an error' do
|
|
|
|
expect(lease).to receive(:try_obtain).exactly(11).times
|
|
|
|
|
|
|
|
expect { subject }.to raise_error('Failed to obtain a lock')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ttl is specified' do
|
2018-07-04 04:28:18 +00:00
|
|
|
let(:options) { { ttl: 10.minutes } }
|
2018-07-03 07:20:27 +00:00
|
|
|
|
|
|
|
it 'receives the specified argument' do
|
2018-07-04 04:28:18 +00:00
|
|
|
expect(Gitlab::ExclusiveLease).to receive(:new).with(unique_key, { timeout: 10.minutes } )
|
|
|
|
|
2018-07-03 07:20:27 +00:00
|
|
|
expect { subject }.to raise_error('Failed to obtain a lock')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when retry count is specified' do
|
|
|
|
let(:options) { { retries: 3 } }
|
|
|
|
|
|
|
|
it 'retries for the specified times' do
|
|
|
|
expect(lease).to receive(:try_obtain).exactly(4).times
|
2018-07-04 04:28:18 +00:00
|
|
|
|
2018-07-03 07:20:27 +00:00
|
|
|
expect { subject }.to raise_error('Failed to obtain a lock')
|
|
|
|
end
|
2019-07-24 00:51:23 +00:00
|
|
|
|
|
|
|
context 'when lease is granted after retry' do
|
|
|
|
it 'yields block with true' do
|
|
|
|
expect(lease).to receive(:try_obtain).exactly(3).times { nil }
|
|
|
|
expect(lease).to receive(:try_obtain).once { unique_key }
|
|
|
|
|
2020-09-28 15:09:44 +00:00
|
|
|
expect { |b| class_instance.in_lock(unique_key, &b) }
|
|
|
|
.to yield_with_args(true, an_instance_of(described_class::SleepingLock))
|
2019-07-24 00:51:23 +00:00
|
|
|
end
|
|
|
|
end
|
2018-07-03 07:20:27 +00:00
|
|
|
end
|
|
|
|
|
2020-05-06 06:09:36 +00:00
|
|
|
context 'when we specify no retries' do
|
|
|
|
let(:options) { { retries: 0 } }
|
|
|
|
|
|
|
|
it 'never sleeps' do
|
2020-11-02 21:09:10 +00:00
|
|
|
expect_any_instance_of(Gitlab::ExclusiveLeaseHelpers::SleepingLock).not_to receive(:sleep)
|
2020-05-06 06:09:36 +00:00
|
|
|
|
|
|
|
expect { subject }.to raise_error('Failed to obtain a lock')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-03 07:20:27 +00:00
|
|
|
context 'when sleep second is specified' do
|
2020-04-30 00:09:37 +00:00
|
|
|
let(:options) { { retries: 1, sleep_sec: 0.05.seconds } }
|
2018-07-03 07:20:27 +00:00
|
|
|
|
|
|
|
it 'receives the specified argument' do
|
2020-11-02 21:09:10 +00:00
|
|
|
expect_any_instance_of(Gitlab::ExclusiveLeaseHelpers::SleepingLock).to receive(:sleep).with(0.05.seconds).once
|
2020-04-30 00:09:37 +00:00
|
|
|
|
|
|
|
expect { subject }.to raise_error('Failed to obtain a lock')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sleep second is specified as a lambda' do
|
|
|
|
let(:options) { { retries: 2, sleep_sec: ->(num) { 0.1 + num } } }
|
|
|
|
|
|
|
|
it 'receives the specified argument' do
|
2020-11-02 21:09:10 +00:00
|
|
|
expect_any_instance_of(Gitlab::ExclusiveLeaseHelpers::SleepingLock).to receive(:sleep).with(1.1.seconds).once
|
|
|
|
expect_any_instance_of(Gitlab::ExclusiveLeaseHelpers::SleepingLock).to receive(:sleep).with(2.1.seconds).once
|
2018-07-04 04:28:18 +00:00
|
|
|
|
2018-07-03 07:20:27 +00:00
|
|
|
expect { subject }.to raise_error('Failed to obtain a lock')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|