|
1 | 1 | require "test/unit"
|
| 2 | +require "objspace" |
2 | 3 |
|
3 | 4 | class TestObjSpaceRactor < Test::Unit::TestCase
|
4 | 5 | def test_tracing_does_not_crash
|
@@ -52,4 +53,50 @@ def fin
|
52 | 53 | ractors.each(&:join)
|
53 | 54 | RUBY
|
54 | 55 | end
|
| 56 | + |
| 57 | + def test_find_path_to_unshareable_object |
| 58 | + # Direct shareable object |
| 59 | + assert_nil(ObjectSpace.find_path_to_unshareable_object(1)) |
| 60 | + assert_nil(ObjectSpace.find_path_to_unshareable_object(true)) |
| 61 | + assert_nil(ObjectSpace.find_path_to_unshareable_object(:symbol)) |
| 62 | + assert_nil(ObjectSpace.find_path_to_unshareable_object("frozen".freeze)) |
| 63 | + |
| 64 | + # Direct unshareable object |
| 65 | + obj = "unfrozen" |
| 66 | + path = ObjectSpace.find_path_to_unshareable_object(obj) |
| 67 | + assert_equal([obj], path) |
| 68 | + |
| 69 | + # Hash containing unshareable object |
| 70 | + obj = { a: 1, b: "frozen".freeze, c: "unfrozen" } |
| 71 | + path = ObjectSpace.find_path_to_unshareable_object(obj) |
| 72 | + assert_equal([ |
| 73 | + { a: 1, b: "frozen".freeze, c: "unfrozen" }, |
| 74 | + "unfrozen" |
| 75 | + ], path) |
| 76 | + assert_equal(true, path.include?(obj[:c])) |
| 77 | + |
| 78 | + # Array containing unshareable object |
| 79 | + obj = [1, 2, "unfrozen", "frozen".freeze] |
| 80 | + path = ObjectSpace.find_path_to_unshareable_object(obj) |
| 81 | + assert_not_nil(path) |
| 82 | + assert_equal(obj, path[0]) |
| 83 | + assert_equal(true, path.include?(obj[2])) |
| 84 | + |
| 85 | + # Custom class |
| 86 | + klass = Class.new do |
| 87 | + attr_accessor :value |
| 88 | + end |
| 89 | + obj = klass.new |
| 90 | + obj.value = "unfrozen" |
| 91 | + path = ObjectSpace.find_path_to_unshareable_object(obj) |
| 92 | + assert_equal(obj, path[0]) |
| 93 | + assert_equal(true, path.include?(obj.value)) |
| 94 | + |
| 95 | + # Circular reference |
| 96 | + obj1 = { name: "obj1" } |
| 97 | + obj2 = { name: "obj2", ref: obj1 } |
| 98 | + obj1[:ref] = obj2 |
| 99 | + path = ObjectSpace.find_path_to_unshareable_object(obj1) |
| 100 | + assert(path.any? { |o| o.is_a?(String) && !o.frozen? }) |
| 101 | + end |
55 | 102 | end
|
0 commit comments