Commit 9b7239c4 authored by Alex Kalderimis's avatar Alex Kalderimis

Hide SleepingLock#attempts and expose #retried?

parent fd82c357
......@@ -35,7 +35,7 @@ module Gitlab
lease.obtain(1 + retries)
yield(lease.attempts > 1)
yield(lease.retried?)
ensure
lease&.cancel
end
......
......@@ -4,8 +4,6 @@ module Gitlab
module ExclusiveLeaseHelpers
# Wrapper around ExclusiveLease that adds retry logic
class SleepingLock
attr_reader :attempts
delegate :cancel, to: :@lease
def initialize(key, timeout:, delay:)
......@@ -23,9 +21,13 @@ module Gitlab
end
end
def retried?
attempts > 1
end
private
attr_reader :delay
attr_reader :delay, :attempts
def held?
@uuid.present?
......
......@@ -11,6 +11,32 @@ describe Gitlab::ExclusiveLeaseHelpers::SleepingLock, :clean_gitlab_redis_shared
subject { described_class.new(key, timeout: timeout, delay: delay) }
describe '#retried?' do
before do
stub_exclusive_lease(key, 'uuid')
end
context 'we have not made any attempts' do
it { is_expected.not_to be_retried }
end
context 'we just made a single (initial) attempt' do
it 'is not considered a retry' do
subject.send(:try_obtain)
is_expected.not_to be_retried
end
end
context 'made multiple attempts' do
it 'is considered a retry' do
2.times { subject.send(:try_obtain) }
is_expected.to be_retried
end
end
end
describe '#obtain' do
context 'when the lease is not held' do
before do
......@@ -22,7 +48,7 @@ describe Gitlab::ExclusiveLeaseHelpers::SleepingLock, :clean_gitlab_redis_shared
subject.obtain(10)
expect(subject.attempts).to eq(1)
expect(subject).not_to be_retried
end
end
......@@ -51,14 +77,14 @@ describe Gitlab::ExclusiveLeaseHelpers::SleepingLock, :clean_gitlab_redis_shared
end
context 'when lease is granted after retry' do
it 'records the successful attempt number' do
it 'knows that it retried' do
expect(subject).to receive(:sleep).with(delay).exactly(3).times
expect(lease).to receive(:try_obtain).exactly(3).times { nil }
expect(lease).to receive(:try_obtain).once { 'obtained' }
subject.obtain(max_attempts)
expect(subject.attempts).to eq(4)
expect(subject).to be_retried
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