From 86454ddac9cf840113fed66ea61f2fc38cd492bc Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 8 Feb 2014 14:50:52 -0800 Subject: [PATCH] core: Environment#lock tests --- lib/vagrant/environment.rb | 3 +++ test/unit/vagrant/environment_test.rb | 33 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index d4adac195..d1d369125 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -343,6 +343,9 @@ module Vagrant # method. During this time, any other environment which attempts # to lock which points to the same lock file will fail. def lock + # If we don't have a block, then locking is useless, so ignore it + return if !block_given? + # This allows multiple locks in the same process to be nested return yield if @lock_acquired diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 4478b5abf..f0499e416 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -194,6 +194,39 @@ describe Vagrant::Environment do end end + describe "#lock" do + it "does nothing if no block is given" do + subject.lock + end + + it "locks the environment" do + another = env.create_vagrant_env + raised = false + + subject.lock do + begin + another.lock + rescue Vagrant::Errors::EnvironmentLockedError + raised = true + end + end + + expect(raised).to be_true + end + + it "allows nested locks on the same environment" do + success = false + + subject.lock do + subject.lock do + success = true + end + end + + expect(success).to be_true + end + end + describe "#machine" do # A helper to register a provider for use in tests. def register_provider(name, config_class=nil, options=nil)