diff options
author | John Keiser <john@johnkeiser.com> | 2016-08-16 11:53:09 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2016-08-16 16:42:04 -0700 |
commit | 35116c250147498b0d2ef21810f4fa11ec4bf05a (patch) | |
tree | f0da64b02e4a64bd920674928e2a29fa488fafd5 | |
parent | 51581a0d11ddfac8db97712c6c3c680910db706c (diff) | |
download | chef-jk/runlock-race.tar.gz |
Fix race where lockfile can be created but not yet acquiredjk/runlock-race
-rw-r--r-- | spec/functional/run_lock_spec.rb | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/spec/functional/run_lock_spec.rb b/spec/functional/run_lock_spec.rb index 19d45d3e7d..bdeda10dc0 100644 --- a/spec/functional/run_lock_spec.rb +++ b/spec/functional/run_lock_spec.rb @@ -46,11 +46,9 @@ describe Chef::RunLock do end WAIT_ON_LOCK_TIME = 1.0 - def wait_on_lock + def wait_on_lock(from_fork) Timeout.timeout(WAIT_ON_LOCK_TIME) do - until File.exist?(lockfile) - sleep 0.1 - end + from_fork.readline end rescue Timeout::Error raise "Lockfile never created, abandoning test" @@ -259,14 +257,16 @@ describe Chef::RunLock do it "test returns true and acquires the lock" do run_lock = Chef::RunLock.new(lockfile) from_tests, to_fork = IO.pipe + from_fork, to_tests = IO.pipe p1 = fork do expect(run_lock.test).to eq(true) + to_tests.puts "lock acquired" # Wait for the test to tell us we can exit before exiting from_tests.readline exit! 0 end - wait_on_lock + wait_on_lock(from_fork) p2 = fork do expect(run_lock.test).to eq(false) @@ -283,14 +283,16 @@ describe Chef::RunLock do it "test returns without waiting when the lock is acquired" do run_lock = Chef::RunLock.new(lockfile) from_tests, to_fork = IO.pipe + from_fork, to_tests = IO.pipe p1 = fork do run_lock.acquire + to_tests.puts "lock acquired" # Wait for the test to tell us we can exit before exiting from_tests.readline exit! 0 end - wait_on_lock + wait_on_lock(from_fork) expect(run_lock.test).to eq(false) to_fork.puts "you can exit now" |