summaryrefslogtreecommitdiff
path: root/spec/ruby/library/objectspace/trace_object_allocations_spec.rb
blob: 3100511dc912a80cff18b70236ef5a1da87e750a (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require_relative '../../spec_helper'
require 'objspace'

describe "ObjectSpace.trace_object_allocations" do
  it "runs a block" do
    ScratchPad.clear
    ObjectSpace.trace_object_allocations do
      ScratchPad.record :a
    end
    ScratchPad.recorded.should == :a
  end

  it "records info for allocation_class_path" do
    ObjectSpace.trace_object_allocations do
      o = Object.new
      ObjectSpace.allocation_class_path(o).should == "Class"
      a = [1, 2, 3]
      ObjectSpace.allocation_class_path(a).should == nil
    end
  end

  it "records info for allocation_generation" do
    ObjectSpace.trace_object_allocations do
      o = Object.new
      ObjectSpace.allocation_generation(o).should.kind_of?(Integer)
      a = [1, 2, 3]
      ObjectSpace.allocation_generation(a).should.kind_of?(Integer)
    end
  end

  it "records info for allocation_method_id" do
    ObjectSpace.trace_object_allocations do
      o = Object.new
      ObjectSpace.allocation_method_id(o).should == :new
      a = [1, 2, 3]
      ObjectSpace.allocation_method_id(a).should == nil
    end
  end

  it "records info for allocation_sourcefile" do
    ObjectSpace.trace_object_allocations do
      o = Object.new
      ObjectSpace.allocation_sourcefile(o).should == __FILE__
      a = [1, 2, 3]
      ObjectSpace.allocation_sourcefile(a).should == __FILE__
    end
  end

  it "records info for allocation_sourceline" do
    ObjectSpace.trace_object_allocations do
      o = Object.new
      ObjectSpace.allocation_sourceline(o).should == __LINE__ - 1
      a = [1, 2, 3]
      ObjectSpace.allocation_sourceline(a).should == __LINE__ - 1
    end
  end

  it "can be cleared using trace_object_allocations_clear" do
    ObjectSpace.trace_object_allocations do
      o = Object.new
      ObjectSpace.allocation_class_path(o).should == "Class"
      ObjectSpace.trace_object_allocations_clear
      ObjectSpace.allocation_class_path(o).should be_nil
    end
  end

  it "does not clears allocation data after returning" do
    o = nil
    ObjectSpace.trace_object_allocations do
      o = Object.new
    end
    ObjectSpace.allocation_class_path(o).should == "Class"
  end

  it "can be used without a block using trace_object_allocations_start and _stop" do
    ObjectSpace.trace_object_allocations_start
    begin
      o = Object.new
      ObjectSpace.allocation_class_path(o).should == "Class"
      a = [1, 2, 3]
      ObjectSpace.allocation_class_path(a).should == nil
    ensure
      ObjectSpace.trace_object_allocations_stop
    end
  end

  it "does not clears allocation data after trace_object_allocations_stop" do
    ObjectSpace.trace_object_allocations_start
    begin
      o = Object.new
    ensure
      ObjectSpace.trace_object_allocations_stop
    end
    ObjectSpace.allocation_class_path(o).should == "Class"
  end

  it "can be nested" do
    ObjectSpace.trace_object_allocations do
      ObjectSpace.trace_object_allocations do
        o = Object.new
        ObjectSpace.allocation_class_path(o).should == "Class"
      end
    end
  end

  it "can be nested without a block using trace_object_allocations_start and _stop" do
    ObjectSpace.trace_object_allocations_start
    begin
      ObjectSpace.trace_object_allocations_start
      begin
        o = Object.new
        ObjectSpace.allocation_class_path(o).should == "Class"
      ensure
        ObjectSpace.trace_object_allocations_stop
      end
    ensure
      ObjectSpace.trace_object_allocations_stop
    end
  end

  it "can be nested with more _stop than _start" do
    ObjectSpace.trace_object_allocations_start
    begin
      o = Object.new
      ObjectSpace.allocation_class_path(o).should == "Class"
      ObjectSpace.trace_object_allocations_stop
    ensure
      ObjectSpace.trace_object_allocations_stop
    end
  end
end