2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-22 03:23:50 +02:00
|
|
|
require "lock_file"
|
2017-02-20 22:43:09 +01:00
|
|
|
|
2024-02-18 15:11:11 -08:00
|
|
|
RSpec.describe LockFile do
|
2024-07-30 17:51:02 +01:00
|
|
|
subject(:lock_file) { described_class.new(:lock, Pathname("foo")) }
|
2017-02-20 22:43:09 +01:00
|
|
|
|
2024-11-23 23:45:17 +01:00
|
|
|
let(:lock_file_copy) { described_class.new(:lock, Pathname("foo")) }
|
|
|
|
|
2017-02-20 22:43:09 +01:00
|
|
|
describe "#lock" do
|
2024-11-23 23:45:17 +01:00
|
|
|
it "ensures the lock file is created" do
|
|
|
|
expect(lock_file.path).not_to exist
|
|
|
|
lock_file.lock
|
|
|
|
expect(lock_file.path).to exist
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not raise an error when the same instance is locked multiple times" do
|
2021-01-31 13:14:23 -05:00
|
|
|
lock_file.lock
|
2017-02-20 22:43:09 +01:00
|
|
|
|
2021-01-31 13:14:23 -05:00
|
|
|
expect { lock_file.lock }.not_to raise_error
|
2017-02-20 22:43:09 +01:00
|
|
|
end
|
|
|
|
|
2024-11-23 23:45:17 +01:00
|
|
|
it "raises an error if another instance is already locked" do
|
2021-01-31 13:14:23 -05:00
|
|
|
lock_file.lock
|
2017-02-20 22:43:09 +01:00
|
|
|
|
2023-03-08 23:14:46 +00:00
|
|
|
expect do
|
2024-11-23 23:45:17 +01:00
|
|
|
lock_file_copy.lock
|
2023-03-08 23:14:46 +00:00
|
|
|
end.to raise_error(OperationInProgressError)
|
2017-02-20 22:43:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#unlock" do
|
|
|
|
it "does not raise an error when already unlocked" do
|
2021-01-31 13:14:23 -05:00
|
|
|
expect { lock_file.unlock }.not_to raise_error
|
2017-02-20 22:43:09 +01:00
|
|
|
end
|
|
|
|
|
2017-05-22 03:23:50 +02:00
|
|
|
it "unlocks when locked" do
|
2021-01-31 13:14:23 -05:00
|
|
|
lock_file.lock
|
|
|
|
lock_file.unlock
|
2017-02-20 22:43:09 +01:00
|
|
|
|
2024-11-23 23:45:17 +01:00
|
|
|
expect { lock_file_copy.lock }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows deleting a lock file only by the instance that locked it" do
|
|
|
|
lock_file.lock
|
|
|
|
expect(lock_file.path).to exist
|
|
|
|
|
|
|
|
expect(lock_file_copy.path).to exist
|
|
|
|
lock_file_copy.unlock(unlink: true)
|
|
|
|
expect(lock_file_copy.path).to exist
|
|
|
|
expect(lock_file.path).to exist
|
|
|
|
|
|
|
|
lock_file.unlock(unlink: true)
|
|
|
|
expect(lock_file.path).not_to exist
|
2017-02-20 22:43:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|