From 131ba059cad0cff223842a739b2bc87425613b49 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 12 May 2025 09:49:59 +0200 Subject: [PATCH] test_object_id.rb: use better randomness When the test is repeated 20 or more times in the same process it's not that unlikely for `rand(100_000)` to return the same thing twice, causing `TestObjectIdTooComplexClass` to fail. ``` 1) Failure: TestObjectIdTooComplexClass#test_dup_with_id_and_ivar [/tmp/ruby/src/trunk-repeat20-asserts/test/ruby/test_object_id.rb:172]: Expected # to be too_complex?. ``` --- test/ruby/test_object_id.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/ruby/test_object_id.rb b/test/ruby/test_object_id.rb index 2277bba634..97ed70d839 100644 --- a/test/ruby/test_object_id.rb +++ b/test/ruby/test_object_id.rb @@ -1,4 +1,5 @@ require 'test/unit' +require "securerandom" class TestObjectId < Test::Unit::TestCase def setup @@ -159,14 +160,14 @@ class TestObjectIdTooComplexClass < TestObjectId @obj = TooComplex.new - @obj.instance_variable_set("@___#{rand(100_000)}", 1) + @obj.instance_variable_set("@___#{SecureRandom.hex}", 1) 8.times do |i| @obj.instance_variable_set("@TestObjectIdTooComplexClass#{i}", 1) @obj.remove_instance_variable("@TestObjectIdTooComplexClass#{i}") end - @obj.instance_variable_set("@___#{rand(100_000)}", 1) + @obj.instance_variable_set("@test", 1) if defined?(RubyVM::Shape) assert_predicate(RubyVM::Shape.of(@obj), :too_complex?)