Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/couchbase
26 changes: 25 additions & 1 deletion test/couchbase_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def test_it_can_use_configuration_with_connect
end
end

FORK_CHILD_TIMEOUT = 30

# Regression test for RCBC-550
def test_it_can_connect_after_process_fork
skip("Forking not supported on Windows") if Gem.win_platform?
Expand All @@ -64,7 +66,7 @@ def test_it_can_connect_after_process_fork
cluster.disconnect
end

_, status = Process.wait2(pid)
status = wait_for_child_or_kill(pid, timeout: FORK_CHILD_TIMEOUT)

assert_predicate status, :success?, "Child process failed with status #{status.exitstatus}"

Expand All @@ -74,4 +76,26 @@ def test_it_can_connect_after_process_fork
refute_nil cluster
cluster.disconnect
end

private

# Polls for +pid+ to exit, killing it and failing the test if it doesn't within
# +timeout+ seconds, instead of blocking forever like Process.wait2 would.
def wait_for_child_or_kill(pid, timeout:)
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout

loop do
_, status = Process.waitpid2(pid, Process::WNOHANG)
return status if status

if Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
Process.kill("KILL", pid)
Process.waitpid2(pid)

flunk "Child process (pid #{pid}) did not exit within #{timeout}s after fork."
end

sleep 0.1
end
end
end
Loading