From c6b4eb990e17021a47579e31fe85f83df827fffd Mon Sep 17 00:00:00 2001 From: John Keiser Date: Fri, 5 Aug 2016 12:48:28 -0700 Subject: Remove race conditions from run_lock_spec --- spec/functional/run_lock_spec.rb | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'spec/functional/run_lock_spec.rb') diff --git a/spec/functional/run_lock_spec.rb b/spec/functional/run_lock_spec.rb index f1e480c917..19d45d3e7d 100644 --- a/spec/functional/run_lock_spec.rb +++ b/spec/functional/run_lock_spec.rb @@ -258,11 +258,12 @@ 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 p1 = fork do expect(run_lock.test).to eq(true) - run_lock.save_pid - sleep 2 - exit! 1 + # Wait for the test to tell us we can exit before exiting + from_tests.readline + exit! 0 end wait_on_lock @@ -272,23 +273,29 @@ describe Chef::RunLock do exit! 0 end - Process.waitpid2(p2) - Process.waitpid2(p1) + pid, exit_status = Process.waitpid2(p2) + expect(exit_status).to eq(0) + to_fork.puts "you can exit now" + pid, exit_status = Process.waitpid2(p1) + expect(exit_status).to eq(0) end it "test returns without waiting when the lock is acquired" do run_lock = Chef::RunLock.new(lockfile) + from_tests, to_fork = IO.pipe p1 = fork do run_lock.acquire - run_lock.save_pid - sleep 2 - exit! 1 + # Wait for the test to tell us we can exit before exiting + from_tests.readline + exit! 0 end wait_on_lock - expect(run_lock.test).to eq(false) - Process.waitpid2(p1) + + to_fork.puts "you can exit now" + pid, exit_status = Process.waitpid2(p1) + expect(exit_status).to eq(0) end end -- cgit v1.2.1