From 4fb9832589077d9dce5ce52eab291cea728d6ba3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 25 Apr 2014 01:33:25 -0700 Subject: [PATCH] core: clean up the lock cleanup code, tests --- lib/vagrant/environment.rb | 13 ++++--------- test/unit/vagrant/environment_test.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 950b8f28a..8189b3337 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -358,18 +358,13 @@ module Vagrant return if !block_given? # This allows multiple locks in the same process to be nested - return yield if @locks[name] + return yield if @locks[name] || opts[:noop] # The path to this lock lock_path = data_dir.join("lock.#{name}.lock") @logger.debug("Attempting to acquire process-lock: #{name}") - - if name != "dotlock" - lock("dotlock", no_clean: true) do - f = File.open(lock_path, "w+") - end - else + lock("dotlock", noop: name == "dotlock") do f = File.open(lock_path, "w+") end @@ -398,8 +393,8 @@ module Vagrant end # Clean up the lock file, this requires another lock - if !opts[:no_clean] - lock("dotlock", no_clean: true) do + if name != "dotlock" + lock("dotlock") do f.close File.delete(lock_path) end diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 19bc78e27..070df3ecf 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -198,6 +198,13 @@ describe Vagrant::Environment do end describe "#lock" do + def lock_count + subject.data_dir. + children. + find_all { |c| c.to_s.end_with?("lock") }. + length + end + it "does nothing if no block is given" do subject.lock end @@ -228,6 +235,19 @@ describe Vagrant::Environment do expect(success).to be_true end + + it "cleans up all lock files" do + inner_count = nil + + expect(lock_count).to eq(0) + subject.lock do + inner_count = lock_count + end + + expect(inner_count).to_not be_nil + expect(inner_count).to eq(2) + expect(lock_count).to eq(1) + end end describe "#machine" do