Skip to content

Commit

Permalink
Less racy specs for Process.exit!
Browse files Browse the repository at this point in the history
There would a small chance where the sleep(1) call would complete _before_ the
Thread.new { ... } block finished running. After all, there's no guarantee a
thread starts right away.

By removing the sleep(1) and adding a Thread#join call we can ensure that the
Process.exit!(2) is _only_ called when the @object.exit!(1) call doesn't
immediately terminate the process.

At least I hope, you never know for certain when it comes to multi-threading.
  • Loading branch information
Yorick Peterse committed Nov 13, 2014
1 parent b871cd5 commit ab2d1f3
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions spec/ruby/shared/process/exit.rb
Expand Up @@ -55,10 +55,13 @@

it "exits immediately when called from a thread" do
pid = Process.fork do
Thread.new { @object.exit!(1) }
sleep 1
Thread.new { @object.exit!(1) }.join

# If the above doesn't instantly terminate the process our exit status
# would be "2", leading to the test below to fail.
Process.exit!(2)
end

pid, status = Process.waitpid2(pid)
status.exitstatus.should == 1
end
Expand Down

0 comments on commit ab2d1f3

Please sign in to comment.