From ca4325f6c9710ae5dcda9f735b264ba92bffb68f Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 6 Mar 2025 08:09:57 +0100 Subject: [PATCH] Harden `TestObjSpace#test_memsize_of_root_shared_string` This test occasionally fail because it runs into a String instance that had its `==` method removed. I couldn't identify where this String comes from, but in general when using `each_object` it's best to not assume returned objectd are functional. By just inverting the operands of `==` we ensure it's always `String#==` that is called. ``` 1) Error: TestObjSpace#test_memsize_of_root_shared_string: NoMethodError: undefined method '==' for # /tmp/ruby/src/trunk-random1/test/objspace/test_objspace.rb:35:in 'block in TestObjSpace#test_memsize_of_root_shared_string' /tmp/ruby/src/trunk-random1/test/objspace/test_objspace.rb:35:in 'ObjectSpace.each_object' /tmp/ruby/src/trunk-random1/test/objspace/test_objspace.rb:35:in 'TestObjSpace#test_memsize_of_root_shared_string' ``` --- test/objspace/test_objspace.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 2f78df8faa..c25882a580 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -32,7 +32,7 @@ class TestObjSpace < Test::Unit::TestCase a = "a" * GC::INTERNAL_CONSTANTS[:RVARGC_MAX_ALLOCATE_SIZE] b = a.dup c = nil - ObjectSpace.each_object(String) {|x| break c = x if x == a and x.frozen?} + ObjectSpace.each_object(String) {|x| break c = x if a == x and x.frozen?} rv_size = GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE] assert_equal([rv_size, rv_size, a.length + 1 + rv_size], [a, b, c].map {|x| ObjectSpace.memsize_of(x)}) end