Allow path and exception for Lock middleware to be procs
This commit is contained in:
parent
9ae3a373c8
commit
7531c94dbf
|
@ -13,6 +13,8 @@ module Vagrant
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
lock_path = @options[:path]
|
lock_path = @options[:path]
|
||||||
|
lock_path = lock_path.call(env) if lock_path.is_a?(Proc)
|
||||||
|
|
||||||
env_key = "has_lock_#{lock_path}"
|
env_key = "has_lock_#{lock_path}"
|
||||||
|
|
||||||
if !env[env_key]
|
if !env[env_key]
|
||||||
|
@ -24,7 +26,9 @@ module Vagrant
|
||||||
# succeeds it returns a 0, so we must explicitly check for
|
# succeeds it returns a 0, so we must explicitly check for
|
||||||
# the proper error case.
|
# the proper error case.
|
||||||
if f.flock(File::LOCK_EX | File::LOCK_NB) === false
|
if f.flock(File::LOCK_EX | File::LOCK_NB) === false
|
||||||
raise @options[:exception]
|
exception = @options[:exception]
|
||||||
|
exception = exception.call(env) if exception.is_a?(Proc)
|
||||||
|
raise exception
|
||||||
end
|
end
|
||||||
|
|
||||||
# Set that we gained the lock and call deeper into the
|
# Set that we gained the lock and call deeper into the
|
||||||
|
|
|
@ -29,6 +29,37 @@ describe Vagrant::Action::Builtin::Lock do
|
||||||
to_not raise_error
|
to_not raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should allow the path to be a proc" do
|
||||||
|
inner_acquire = true
|
||||||
|
app = lambda do |env|
|
||||||
|
File.open(lock_path, "w+") do |f|
|
||||||
|
inner_acquire = f.flock(File::LOCK_EX | File::LOCK_NB)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
options[:path] = lambda { |env| lock_path }
|
||||||
|
|
||||||
|
instance = described_class.new(app, env, options)
|
||||||
|
instance.call(env)
|
||||||
|
|
||||||
|
inner_acquire.should == false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should allow the exception to be a proc" do
|
||||||
|
exception = options[:exception]
|
||||||
|
options[:exception] = lambda { |env| exception }
|
||||||
|
|
||||||
|
File.open(lock_path, "w+") do |f|
|
||||||
|
# Acquire lock
|
||||||
|
f.flock(File::LOCK_EX | File::LOCK_NB).should == 0
|
||||||
|
|
||||||
|
# Test!
|
||||||
|
instance = described_class.new(app, env, options)
|
||||||
|
expect { instance.call(env) }.
|
||||||
|
to raise_error(exception)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "should call the middleware with the lock held" do
|
it "should call the middleware with the lock held" do
|
||||||
inner_acquire = true
|
inner_acquire = true
|
||||||
app = lambda do |env|
|
app = lambda do |env|
|
||||||
|
|
Loading…
Reference in New Issue