From 1afcaa3e4b4338191cdc337ed43bc92637be26b5 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 21 Jun 2024 11:41:32 -0400 Subject: [PATCH] Fix flaky test_stat_heap_all We only collect GC.stat_heap(nil, stat_heap_all) once, outside of the loop, but assert_equal could allocate objects which can cause a GC to run and cause stat_heap_all to be out-of-sync. --- test/ruby/test_gc.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb index 0793fe2b49..25625c41cb 100644 --- a/test/ruby/test_gc.rb +++ b/test/ruby/test_gc.rb @@ -186,13 +186,12 @@ class TestGc < Test::Unit::TestCase omit "flaky with RJIT, which allocates objects itself" if defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled? stat_heap_all = {} stat_heap = {} - - 2.times do - GC.stat_heap(0, stat_heap) - GC.stat_heap(nil, stat_heap_all) - end + # Initialize to prevent GC in future calls + GC.stat_heap(0, stat_heap) + GC.stat_heap(nil, stat_heap_all) GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT].times do |i| + GC.stat_heap(nil, stat_heap_all) GC.stat_heap(i, stat_heap) # Remove keys that can vary between invocations