summaryrefslogtreecommitdiff
path: root/test/objspace/test_ractor.rb
blob: 996d83fbd214dd1d8323e8b1774b8fc436ef41da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require "test/unit"

class TestObjSpaceRactor < Test::Unit::TestCase
  def test_tracing_does_not_crash
    assert_ractor(<<~RUBY, require: 'objspace')
      ObjectSpace.trace_object_allocations do
        r = Ractor.new do
          _obj = 'a' * 1024
        end

        r.join
      end
    RUBY
  end

  def test_undefine_finalizer
    assert_ractor(<<~'RUBY', require: 'objspace')
      def fin
        ->(id) { }
      end
      ractors = 5.times.map do
        Ractor.new do
          10_000.times do
            o = Object.new
            ObjectSpace.define_finalizer(o, fin)
            ObjectSpace.undefine_finalizer(o)
          end
        end
      end

      ractors.each(&:join)
    RUBY
  end

  def test_copy_finalizer
    assert_ractor(<<~'RUBY', require: 'objspace')
      def fin
        ->(id) { }
      end
      OBJ = Object.new
      ObjectSpace.define_finalizer(OBJ, fin)
      OBJ.freeze

      ractors = 5.times.map do
        Ractor.new do
          10_000.times do
            OBJ.clone
          end
        end
      end

      ractors.each(&:join)
    RUBY
  end

  def test_trace_object_allocations_with_ractor_tracepoint
    # Test that ObjectSpace.trace_object_allocations works globally across all Ractors
    assert_ractor(<<~'RUBY', require: 'objspace')
      ObjectSpace.trace_object_allocations do
        obj1 = Object.new; line1 = __LINE__
        assert_equal __FILE__, ObjectSpace.allocation_sourcefile(obj1)
        assert_equal line1, ObjectSpace.allocation_sourceline(obj1)

        r = Ractor.new {
          obj = Object.new; line = __LINE__
          [line, obj]
        }

        obj2 = Object.new; line2 = __LINE__
        assert_equal __FILE__, ObjectSpace.allocation_sourcefile(obj2)
        assert_equal line2, ObjectSpace.allocation_sourceline(obj2)

        expected_line, ractor_obj = r.value
        assert_equal __FILE__, ObjectSpace.allocation_sourcefile(ractor_obj)
        assert_equal expected_line, ObjectSpace.allocation_sourceline(ractor_obj)

        obj3 = Object.new; line3 = __LINE__
        assert_equal __FILE__, ObjectSpace.allocation_sourcefile(obj3)
        assert_equal line3, ObjectSpace.allocation_sourceline(obj3)
      end
    RUBY
  end
end