From 36069d5eb2aa38bbed1ef94113dcc1930c9d6cf3 Mon Sep 17 00:00:00 2001 From: Benjamin Garcia Date: Fri, 20 Nov 2026 22:50:40 -0800 Subject: [PATCH] test: measure GVL release over a 250ms window instead of capping at 40 runs --- test/safety_test.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/test/safety_test.rb b/test/safety_test.rb index a1cea90..ba311e6 100644 --- a/test/safety_test.rb +++ b/test/safety_test.rb @@ -160,26 +160,34 @@ def test_gvl_released_and_concurrent_thread_safety mod.load_method(:forward) ready = Queue.new ticks = 0 + counting = false stop = false ticker = Thread.new do ready << true until stop - ticks += 1 + ticks += 1 if counting Thread.pass end end ready.pop - deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 2.0 + start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + min_end = start + 0.25 + max_end = start + 2.0 runs = 0 - while runs < 40 && (ticks < 50 || runs < 5) && Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline + counting = true + while (runs < 5 || Process.clock_gettime(Process::CLOCK_MONOTONIC) < min_end) && + Process.clock_gettime(Process::CLOCK_MONOTONIC) < max_end mod.call(input) runs += 1 end + counting = false + elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start stop = true ticker.join - assert_operator ticks, :>, 10, "Background Ruby thread starved (ticks=#{ticks}, runs=#{runs}) — GVL was held!" + assert_operator ticks, :>, 20, + "Background Ruby thread starved (ticks=#{ticks}, runs=#{runs}, elapsed=#{format('%.3fs', elapsed)}) — GVL was held!" end # B. 4 threads sharing ONE Module instance serialize safely with exact results