diff options
author | John Keiser <john@johnkeiser.com> | 2015-09-29 12:56:06 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-09-29 16:02:09 -0700 |
commit | d8cb2cf50e6ccb275ee5c686ce14842d58376a16 (patch) | |
tree | 102ef850c1a2220d1a3460cdd8062632c811a067 /lib/chef/run_lock.rb | |
parent | 5505f717b65afa82bd27c6365ac6094a16beba2c (diff) | |
download | chef-d8cb2cf50e6ccb275ee5c686ce14842d58376a16.tar.gz |
Add tests for things that have created the lockfile but not yet acquired the lock
Diffstat (limited to 'lib/chef/run_lock.rb')
-rw-r--r-- | lib/chef/run_lock.rb | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/lib/chef/run_lock.rb b/lib/chef/run_lock.rb index cefe637db6..9e0952bdcb 100644 --- a/lib/chef/run_lock.rb +++ b/lib/chef/run_lock.rb @@ -87,27 +87,8 @@ class Chef # Either acquire() or test() methods should be called in order to # get the ownership of run_lock. def test - # ensure the runlock_file path exists - create_path(File.dirname(runlock_file)) - @runlock = File.open(runlock_file,'a+') - - if Chef::Platform.windows? - acquire_win32_mutex - else - # If we support FD_CLOEXEC, then use it. - # NB: ruby-2.0.0-p195 sets FD_CLOEXEC by default, but not - # ruby-1.8.7/1.9.3 - if Fcntl.const_defined?('F_SETFD') && Fcntl.const_defined?('FD_CLOEXEC') - runlock.fcntl(Fcntl::F_SETFD, runlock.fcntl(Fcntl::F_GETFD, 0) | Fcntl::FD_CLOEXEC) - end - # Flock will return 0 if it can acquire the lock otherwise it - # will return false - if runlock.flock(File::LOCK_NB|File::LOCK_EX) == 0 - true - else - false - end - end + create_lock + acquire_lock end # @@ -147,6 +128,34 @@ class Chef end end + # @api private solely for race condition tests + def create_lock + # ensure the runlock_file path exists + create_path(File.dirname(runlock_file)) + @runlock = File.open(runlock_file,'a+') + end + + # @api private solely for race condition tests + def acquire_lock + if Chef::Platform.windows? + acquire_win32_mutex + else + # If we support FD_CLOEXEC, then use it. + # NB: ruby-2.0.0-p195 sets FD_CLOEXEC by default, but not + # ruby-1.8.7/1.9.3 + if Fcntl.const_defined?('F_SETFD') && Fcntl.const_defined?('FD_CLOEXEC') + runlock.fcntl(Fcntl::F_SETFD, runlock.fcntl(Fcntl::F_GETFD, 0) | Fcntl::FD_CLOEXEC) + end + # Flock will return 0 if it can acquire the lock otherwise it + # will return false + if runlock.flock(File::LOCK_NB|File::LOCK_EX) == 0 + true + else + false + end + end + end + private def reset |