diff options
author | Shinya Maeda <shinya@gitlab.com> | 2018-07-03 16:20:27 +0900 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2018-07-03 16:20:27 +0900 |
commit | 93a964d449ad29e94e785209d7ecde217a8e9b25 (patch) | |
tree | ba4abf6750e33ae6c0b64cc3ddf7109cb5214480 /spec | |
parent | 02e3a624d29df1736a170a1484b5686f6dfe24d5 (diff) | |
download | gitlab-ce-93a964d449ad29e94e785209d7ecde217a8e9b25.tar.gz |
Add spec for ExclusiveLeaseHelpers
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/exclusive_lease_helpers_spec.rb | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/spec/lib/gitlab/exclusive_lease_helpers_spec.rb b/spec/lib/gitlab/exclusive_lease_helpers_spec.rb new file mode 100644 index 00000000000..eb2143f9d1d --- /dev/null +++ b/spec/lib/gitlab/exclusive_lease_helpers_spec.rb @@ -0,0 +1,76 @@ +require 'spec_helper' + +describe Gitlab::ExclusiveLeaseHelpers, :clean_gitlab_redis_shared_state do + include ::ExclusiveLeaseHelpers + + let(:class_instance) { (Class.new { include ::Gitlab::ExclusiveLeaseHelpers }).new } + let(:unique_key) { SecureRandom.hex(10) } + + describe '#in_lock' do + subject { class_instance.in_lock(unique_key, **options) { } } + + let(:options) { { } } + + context 'when the lease is not obtained yet' do + before do + stub_exclusive_lease(unique_key, 'uuid') + end + + it 'calls the given block' do + expect { |b| class_instance.in_lock(unique_key, &b) }.to yield_control.once + end + + it 'calls the given block continuously' do + expect { |b| class_instance.in_lock(unique_key, &b) }.to yield_control.once + expect { |b| class_instance.in_lock(unique_key, &b) }.to yield_control.once + expect { |b| class_instance.in_lock(unique_key, &b) }.to yield_control.once + end + + it 'cancels the exclusive lease after the block' do + expect_to_cancel_exclusive_lease(unique_key, 'uuid') + + 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 + let(:options) { { ttl: 10.minute } } + + it 'receives the specified argument' do + expect(Gitlab::ExclusiveLease).to receive(:new).with(unique_key, { timeout: 10.minute } ) + + 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 + + expect { subject }.to raise_error('Failed to obtain a lock') + end + end + + context 'when sleep second is specified' do + let(:options) { { retries: 0, sleep_sec: 0.05.second } } + + it 'receives the specified argument' do + expect(class_instance).to receive(:sleep).with(0.05.second).once + + expect { subject }.to raise_error('Failed to obtain a lock') + end + end + end + end +end |