Skip to content

Commit 131ba05

Browse files
committed
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 #<struct RubyVM::Shape id=6783, parent_id=6774, edge_name=:@___26417, next_field_index=2, heap_index=0, type=1, capacity=7> to be too_complex?. ```
1 parent f638e14 commit 131ba05

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

test/ruby/test_object_id.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'test/unit'
2+
require "securerandom"
23

34
class TestObjectId < Test::Unit::TestCase
45
def setup
@@ -159,14 +160,14 @@ def setup
159160

160161
@obj = TooComplex.new
161162

162-
@obj.instance_variable_set("@___#{rand(100_000)}", 1)
163+
@obj.instance_variable_set("@___#{SecureRandom.hex}", 1)
163164

164165
8.times do |i|
165166
@obj.instance_variable_set("@TestObjectIdTooComplexClass#{i}", 1)
166167
@obj.remove_instance_variable("@TestObjectIdTooComplexClass#{i}")
167168
end
168169

169-
@obj.instance_variable_set("@___#{rand(100_000)}", 1)
170+
@obj.instance_variable_set("@test", 1)
170171

171172
if defined?(RubyVM::Shape)
172173
assert_predicate(RubyVM::Shape.of(@obj), :too_complex?)

0 commit comments

Comments
 (0)