diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 3f4a58e98d..b54a438132 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -244,39 +244,38 @@ class TestObjSpace < Test::Unit::TestCase def test_trace_object_allocations_start_stop_clear 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] - begin - ObjectSpace.trace_object_allocations_start + EnvUtil.without_gc do # suppress potential object reuse. see [Bug #11271] begin ObjectSpace.trace_object_allocations_start begin ObjectSpace.trace_object_allocations_start - obj0 = Object.new + begin + ObjectSpace.trace_object_allocations_start + obj0 = Object.new + ensure + ObjectSpace.trace_object_allocations_stop + obj1 = Object.new + end ensure ObjectSpace.trace_object_allocations_stop - obj1 = Object.new + obj2 = Object.new end ensure ObjectSpace.trace_object_allocations_stop - obj2 = Object.new + obj3 = Object.new end - ensure - ObjectSpace.trace_object_allocations_stop - obj3 = Object.new + + assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj0)) + assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj1)) + assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj2)) + assert_equal(nil , ObjectSpace.allocation_sourcefile(obj3)) # after tracing + + ObjectSpace.trace_object_allocations_clear + assert_equal(nil, ObjectSpace.allocation_sourcefile(obj0)) + assert_equal(nil, ObjectSpace.allocation_sourcefile(obj1)) + assert_equal(nil, ObjectSpace.allocation_sourcefile(obj2)) + assert_equal(nil, ObjectSpace.allocation_sourcefile(obj3)) end - - assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj0)) - assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj1)) - assert_equal(__FILE__, ObjectSpace.allocation_sourcefile(obj2)) - assert_equal(nil , ObjectSpace.allocation_sourcefile(obj3)) # after tracing - - ObjectSpace.trace_object_allocations_clear - assert_equal(nil, ObjectSpace.allocation_sourcefile(obj0)) - assert_equal(nil, ObjectSpace.allocation_sourcefile(obj1)) - assert_equal(nil, ObjectSpace.allocation_sourcefile(obj2)) - assert_equal(nil, ObjectSpace.allocation_sourcefile(obj3)) - ensure - GC.enable end def test_trace_object_allocations_gc_stress diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb index de64cbd36d..9a9796dc55 100644 --- a/test/ruby/test_gc.rb +++ b/test/ruby/test_gc.rb @@ -40,16 +40,16 @@ class TestGc < Test::Unit::TestCase end def test_enable_disable - GC.enable - assert_equal(false, GC.enable) - assert_equal(false, GC.disable) - assert_equal(true, GC.disable) - assert_equal(true, GC.disable) - assert_nil(GC.start) - assert_equal(true, GC.enable) - assert_equal(false, GC.enable) - ensure - GC.enable + EnvUtil.without_gc do + GC.enable + assert_equal(false, GC.enable) + assert_equal(false, GC.disable) + assert_equal(true, GC.disable) + assert_equal(true, GC.disable) + assert_nil(GC.start) + assert_equal(true, GC.enable) + assert_equal(false, GC.enable) + end end def test_gc_config_full_mark_by_default @@ -227,12 +227,9 @@ class TestGc < Test::Unit::TestCase GC.stat(stat) GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT].times do |i| - begin - reenable_gc = !GC.disable + EnvUtil.without_gc do GC.stat_heap(i, stat_heap) GC.stat(stat) - ensure - GC.enable if reenable_gc end 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 def test_gc_disabled_start - begin - disabled = GC.disable + EnvUtil.without_gc do c = GC.count GC.start assert_equal 1, GC.count - c - ensure - GC.enable unless disabled end - begin - disabled = GC.disable + EnvUtil.without_gc do c = GC.count GC.start(immediate_mark: false, immediate_sweep: false) 10_000.times { Object.new } assert_equal 1, GC.count - c - ensure - GC.enable unless disabled end end @@ -859,28 +850,26 @@ class TestGc < Test::Unit::TestCase end def test_old_to_young_reference - original_gc_disabled = GC.disable + EnvUtil.without_gc do + require "objspace" - require "objspace" + old_obj = Object.new + 4.times { GC.start } - old_obj = Object.new - 4.times { GC.start } + assert_include ObjectSpace.dump(old_obj), '"old":true' - assert_include ObjectSpace.dump(old_obj), '"old":true' + young_obj = Object.new + old_obj.instance_variable_set(:@test, young_obj) - young_obj = Object.new - old_obj.instance_variable_set(:@test, young_obj) + # Not immediately promoted to old generation + 3.times do + assert_not_include ObjectSpace.dump(young_obj), '"old":true' + GC.start + end - # Not immediately promoted to old generation - 3.times do - assert_not_include ObjectSpace.dump(young_obj), '"old":true' + # Takes 4 GC to promote to old generation GC.start + assert_include ObjectSpace.dump(young_obj), '"old":true' end - - # Takes 4 GC to promote to old generation - GC.start - assert_include ObjectSpace.dump(young_obj), '"old":true' - ensure - GC.enable if !original_gc_disabled end end diff --git a/test/ruby/test_objectspace.rb b/test/ruby/test_objectspace.rb index 0364cf7093..beb66da7e2 100644 --- a/test/ruby/test_objectspace.rb +++ b/test/ruby/test_objectspace.rb @@ -191,30 +191,29 @@ End end def test_finalizer_thread_raise - GC.disable - fzer = proc do |id| - sleep 0.2 - end - 2.times do - o = Object.new - ObjectSpace.define_finalizer(o, fzer) - end - - my_error = Class.new(RuntimeError) - begin - main_th = Thread.current - Thread.new do - sleep 0.1 - main_th.raise(my_error) + EnvUtil.without_gc do + fzer = proc do |id| + sleep 0.2 + end + 2.times do + o = Object.new + ObjectSpace.define_finalizer(o, fzer) + end + + my_error = Class.new(RuntimeError) + begin + main_th = Thread.current + Thread.new do + sleep 0.1 + main_th.raise(my_error) + end + GC.start + puts "After GC" + sleep(10) + assert(false) + rescue my_error end - GC.start - puts "After GC" - sleep(10) - assert(false) - rescue my_error end - ensure - GC.enable end def test_each_object diff --git a/test/ruby/test_sleep.rb b/test/ruby/test_sleep.rb index 61002b8b18..991b73ebd5 100644 --- a/test/ruby/test_sleep.rb +++ b/test/ruby/test_sleep.rb @@ -4,14 +4,13 @@ require 'etc' class TestSleep < Test::Unit::TestCase def test_sleep_5sec - GC.disable - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - sleep 5 - slept = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start - bottom = 5.0 - assert_operator(slept, :>=, bottom) - assert_operator(slept, :<=, 6.0, "[ruby-core:18015]: longer than expected") - ensure - GC.enable + EnvUtil.without_gc do + start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + sleep 5 + slept = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start + bottom = 5.0 + assert_operator(slept, :>=, bottom) + assert_operator(slept, :<=, 6.0, "[ruby-core:18015]: longer than expected") + end end end diff --git a/test/ruby/test_string_memory.rb b/test/ruby/test_string_memory.rb index e264dd5705..a93a3bd54a 100644 --- a/test/ruby/test_string_memory.rb +++ b/test/ruby/test_string_memory.rb @@ -6,32 +6,31 @@ class TestStringMemory < Test::Unit::TestCase def capture_allocations(klass) allocations = [] - GC.start - GC.disable - generation = GC.count + EnvUtil.without_gc do + GC.start + generation = GC.count - ObjectSpace.trace_object_allocations do - yield + ObjectSpace.trace_object_allocations do + yield - ObjectSpace.each_object(klass) do |instance| - allocations << instance if ObjectSpace.allocation_generation(instance) == generation + ObjectSpace.each_object(klass) do |instance| + allocations << instance if ObjectSpace.allocation_generation(instance) == generation + end + end + + return allocations.map do |instance| + [ + ObjectSpace.allocation_sourcefile(instance), + ObjectSpace.allocation_sourceline(instance), + instance.class, + instance, + ] + end.select do |path,| + # drop strings not created in this file + # (the parallel testing framework may create strings in a separate thread) + path == __FILE__ end end - - return allocations.map do |instance| - [ - ObjectSpace.allocation_sourcefile(instance), - ObjectSpace.allocation_sourceline(instance), - instance.class, - instance, - ] - end.select do |path,| - # drop strings not created in this file - # (the parallel testing framework may create strings in a separate thread) - path == __FILE__ - end - ensure - GC.enable end def test_byteslice_prefix