-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
169 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,93 @@ | ||
# frozen_string_literal: true | ||
RSpec.describe Hyrax::LockManager do | ||
let(:ttl) { Hyrax.config.lock_time_to_live } | ||
let(:retry_count) { Hyrax.config.lock_retry_count } | ||
let(:retry_delay) { Hyrax.config.lock_retry_delay } | ||
|
||
subject do | ||
described_class.new(Hyrax.config.lock_time_to_live, | ||
Hyrax.config.lock_retry_count, | ||
Hyrax.config.lock_retry_delay) | ||
described_class.new(ttl, retry_count, retry_delay) | ||
end | ||
|
||
describe "lock", unless: ENV['TRAVIS'] do | ||
it "calls the block" do | ||
expect { |probe| subject.lock('foobar', &probe) }.to yield_with_no_args | ||
end | ||
|
||
describe 'ttl' do | ||
it "uses the configured ttl" do | ||
client = instance_double(Redlock::Client) | ||
allow(Redlock::Client).to receive(:new).and_return(client) | ||
|
||
key = 'the key' | ||
expect(client).to receive(:lock).with(key, ttl).and_yield(true) | ||
subject.lock(key) { |_| } | ||
end | ||
|
||
it "accepts an optional ttl argument" do | ||
new_ttl = 1000 | ||
expect(new_ttl).not_to eq(ttl) # just to be sure | ||
|
||
client = instance_double(Redlock::Client) | ||
allow(Redlock::Client).to receive(:new).and_return(client) | ||
|
||
key = 'the key' | ||
expect(client).to receive(:lock).with(key, new_ttl).and_yield(true) | ||
subject.lock(key, ttl: new_ttl) { |_| } | ||
end | ||
end | ||
|
||
describe 'retry_count' do | ||
it "uses the configured retry_count" do | ||
client = instance_double(Redlock::Client) | ||
expect(Redlock::Client) | ||
.to receive(:new) | ||
.with(kind_of(Array), retry_count: retry_count, retry_delay: kind_of(Integer)) | ||
.and_return(client) | ||
|
||
allow(client).to receive(:lock).and_yield(true) | ||
subject.lock("a key") { |_| } | ||
end | ||
|
||
it "accepts an optional retry_count argument" do | ||
new_retry_count = 11 | ||
expect(new_retry_count).not_to eq(retry_count) # just to be sure | ||
|
||
client = instance_double(Redlock::Client) | ||
expect(Redlock::Client) | ||
.to receive(:new) | ||
.with(kind_of(Array), retry_count: new_retry_count, retry_delay: retry_delay) | ||
.and_return(client) | ||
|
||
allow(client).to receive(:lock).and_yield(true) | ||
subject.lock("a key", retry_count: new_retry_count) { |_| } | ||
end | ||
end | ||
|
||
describe 'retry_delay' do | ||
it "uses the configured retry_delay" do | ||
client = instance_double(Redlock::Client) | ||
expect(Redlock::Client) | ||
.to receive(:new) | ||
.with(kind_of(Array), retry_count: kind_of(Integer), retry_delay: retry_delay) | ||
.and_return(client) | ||
|
||
allow(client).to receive(:lock).and_yield(true) | ||
subject.lock("a key") { |_| } | ||
end | ||
|
||
it "accepts an optional retry_delay argument" do | ||
new_retry_delay = 11 | ||
expect(new_retry_delay).not_to eq(retry_delay) # just to be sure | ||
|
||
client = instance_double(Redlock::Client) | ||
expect(Redlock::Client) | ||
.to receive(:new) | ||
.with(kind_of(Array), retry_count: retry_count, retry_delay: new_retry_delay) | ||
.and_return(client) | ||
|
||
allow(client).to receive(:lock).and_yield(true) | ||
subject.lock("a key", retry_delay: new_retry_delay) { |_| } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
RSpec.describe Hyrax::Lockable do | ||
let(:lockable_class) { Class.new.include(described_class) } | ||
let(:lock_manager) { instance_double(Hyrax::LockManager) } | ||
|
||
subject { lockable_class.new } | ||
|
||
describe "lock_manager" do | ||
it "lazily creates a new lock manager with the default configuration" do | ||
expect(Hyrax::LockManager).to receive(:new).once.with( | ||
Hyrax.config.lock_time_to_live, | ||
Hyrax.config.lock_retry_count, | ||
Hyrax.config.lock_retry_delay | ||
).and_return(lock_manager) | ||
|
||
expect(subject.lock_manager).to be(lock_manager) | ||
expect(subject.lock_manager).to be(lock_manager) | ||
end | ||
end | ||
|
||
describe "acquire_lock_for" do | ||
before do | ||
subject.instance_variable_set(:@lock_manager, lock_manager) | ||
end | ||
|
||
it 'acquires the lock' do | ||
key = 'a key' | ||
block = proc {} | ||
expect(lock_manager).to receive(:lock).with(key, &block) | ||
subject.acquire_lock_for(key, &block) | ||
end | ||
|
||
it 'accepts an optional ttl argument' do | ||
key = 'a key' | ||
new_ttl = 1000 | ||
block = proc {} | ||
expect(lock_manager).to receive(:lock).with(key, ttl: new_ttl, &block) | ||
subject.acquire_lock_for(key, ttl: new_ttl, &block) | ||
end | ||
|
||
it 'accepts an optional retry_count argument' do | ||
key = 'a key' | ||
new_retry_count = 11 | ||
block = proc {} | ||
expect(lock_manager).to receive(:lock).with(key, retry_count: new_retry_count, &block) | ||
subject.acquire_lock_for(key, retry_count: new_retry_count, &block) | ||
end | ||
|
||
it 'accepts an optional retry_delay argument' do | ||
key = 'a key' | ||
new_retry_delay = 11 | ||
block = proc {} | ||
expect(lock_manager).to receive(:lock).with(key, retry_delay: new_retry_delay, &block) | ||
subject.acquire_lock_for(key, retry_delay: new_retry_delay, &block) | ||
end | ||
end | ||
end |