Replace all GC.disable with EnvUtil.without_gc

This commit is contained in:
Peter Zhu 2024-09-16 15:29:38 -04:00
parent 82151a8630
commit 0160dafc4c
Notes: git 2024-09-17 14:34:47 +00:00
5 changed files with 98 additions and 113 deletions

View File

@ -244,7 +244,7 @@ class TestObjSpace < Test::Unit::TestCase
def test_trace_object_allocations_start_stop_clear def test_trace_object_allocations_start_stop_clear
ObjectSpace.trace_object_allocations_clear # clear object_table to get rid of erroneous detection for obj3 ObjectSpace.trace_object_allocations_clear # clear object_table to get rid of erroneous detection for obj3
GC.disable # suppress potential object reuse. see [Bug #11271] EnvUtil.without_gc do # suppress potential object reuse. see [Bug #11271]
begin begin
ObjectSpace.trace_object_allocations_start ObjectSpace.trace_object_allocations_start
begin begin
@ -275,8 +275,7 @@ class TestObjSpace < Test::Unit::TestCase
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj1)) assert_equal(nil, ObjectSpace.allocation_sourcefile(obj1))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj2)) assert_equal(nil, ObjectSpace.allocation_sourcefile(obj2))
assert_equal(nil, ObjectSpace.allocation_sourcefile(obj3)) assert_equal(nil, ObjectSpace.allocation_sourcefile(obj3))
ensure end
GC.enable
end end
def test_trace_object_allocations_gc_stress def test_trace_object_allocations_gc_stress

View File

@ -40,6 +40,7 @@ class TestGc < Test::Unit::TestCase
end end
def test_enable_disable def test_enable_disable
EnvUtil.without_gc do
GC.enable GC.enable
assert_equal(false, GC.enable) assert_equal(false, GC.enable)
assert_equal(false, GC.disable) assert_equal(false, GC.disable)
@ -48,8 +49,7 @@ class TestGc < Test::Unit::TestCase
assert_nil(GC.start) assert_nil(GC.start)
assert_equal(true, GC.enable) assert_equal(true, GC.enable)
assert_equal(false, GC.enable) assert_equal(false, GC.enable)
ensure end
GC.enable
end end
def test_gc_config_full_mark_by_default def test_gc_config_full_mark_by_default
@ -227,12 +227,9 @@ class TestGc < Test::Unit::TestCase
GC.stat(stat) GC.stat(stat)
GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT].times do |i| GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT].times do |i|
begin EnvUtil.without_gc do
reenable_gc = !GC.disable
GC.stat_heap(i, stat_heap) GC.stat_heap(i, stat_heap)
GC.stat(stat) GC.stat(stat)
ensure
GC.enable if reenable_gc
end end
assert_equal (GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE] + GC::INTERNAL_CONSTANTS[:RVALUE_OVERHEAD]) * (2**i), stat_heap[:slot_size] assert_equal (GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE] + GC::INTERNAL_CONSTANTS[:RVALUE_OVERHEAD]) * (2**i), stat_heap[:slot_size]
@ -768,23 +765,17 @@ class TestGc < Test::Unit::TestCase
end end
def test_gc_disabled_start def test_gc_disabled_start
begin EnvUtil.without_gc do
disabled = GC.disable
c = GC.count c = GC.count
GC.start GC.start
assert_equal 1, GC.count - c assert_equal 1, GC.count - c
ensure
GC.enable unless disabled
end end
begin EnvUtil.without_gc do
disabled = GC.disable
c = GC.count c = GC.count
GC.start(immediate_mark: false, immediate_sweep: false) GC.start(immediate_mark: false, immediate_sweep: false)
10_000.times { Object.new } 10_000.times { Object.new }
assert_equal 1, GC.count - c assert_equal 1, GC.count - c
ensure
GC.enable unless disabled
end end
end end
@ -859,8 +850,7 @@ class TestGc < Test::Unit::TestCase
end end
def test_old_to_young_reference def test_old_to_young_reference
original_gc_disabled = GC.disable EnvUtil.without_gc do
require "objspace" require "objspace"
old_obj = Object.new old_obj = Object.new
@ -880,7 +870,6 @@ class TestGc < Test::Unit::TestCase
# Takes 4 GC to promote to old generation # Takes 4 GC to promote to old generation
GC.start GC.start
assert_include ObjectSpace.dump(young_obj), '"old":true' assert_include ObjectSpace.dump(young_obj), '"old":true'
ensure end
GC.enable if !original_gc_disabled
end end
end end

View File

@ -191,7 +191,7 @@ End
end end
def test_finalizer_thread_raise def test_finalizer_thread_raise
GC.disable EnvUtil.without_gc do
fzer = proc do |id| fzer = proc do |id|
sleep 0.2 sleep 0.2
end end
@ -213,8 +213,7 @@ End
assert(false) assert(false)
rescue my_error rescue my_error
end end
ensure end
GC.enable
end end
def test_each_object def test_each_object

View File

@ -4,14 +4,13 @@ require 'etc'
class TestSleep < Test::Unit::TestCase class TestSleep < Test::Unit::TestCase
def test_sleep_5sec def test_sleep_5sec
GC.disable EnvUtil.without_gc do
start = Process.clock_gettime(Process::CLOCK_MONOTONIC) start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
sleep 5 sleep 5
slept = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start slept = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
bottom = 5.0 bottom = 5.0
assert_operator(slept, :>=, bottom) assert_operator(slept, :>=, bottom)
assert_operator(slept, :<=, 6.0, "[ruby-core:18015]: longer than expected") assert_operator(slept, :<=, 6.0, "[ruby-core:18015]: longer than expected")
ensure end
GC.enable
end end
end end

View File

@ -6,8 +6,8 @@ class TestStringMemory < Test::Unit::TestCase
def capture_allocations(klass) def capture_allocations(klass)
allocations = [] allocations = []
EnvUtil.without_gc do
GC.start GC.start
GC.disable
generation = GC.count generation = GC.count
ObjectSpace.trace_object_allocations do ObjectSpace.trace_object_allocations do
@ -30,8 +30,7 @@ class TestStringMemory < Test::Unit::TestCase
# (the parallel testing framework may create strings in a separate thread) # (the parallel testing framework may create strings in a separate thread)
path == __FILE__ path == __FILE__
end end
ensure end
GC.enable
end end
def test_byteslice_prefix def test_byteslice_prefix