/
call_tree_test.rb
94 lines (76 loc) · 3.15 KB
/
call_tree_test.rb
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
# frozen_string_literal: true
require File.expand_path('../test_helper', __FILE__)
require_relative './call_tree_builder'
require 'base64'
class CallTreeTest < Minitest::Test
def test_initialize
method_info = RubyProf::MethodInfo.new(Base64, :encode64)
call_tree = RubyProf::CallTree.new(method_info)
assert_equal(method_info, call_tree.target)
end
def test_measurement
method_info = RubyProf::MethodInfo.new(Base64, :encode64)
call_tree = RubyProf::CallTree.new(method_info)
assert_equal(0, call_tree.total_time)
assert_equal(0, call_tree.self_time)
assert_equal(0, call_tree.wait_time)
assert_equal(0, call_tree.children_time)
assert_equal(0, call_tree.called)
end
def test_compare
method_info_1 = RubyProf::MethodInfo.new(Base64, :encode64)
call_tree_1 = RubyProf::CallTree.new(method_info_1)
method_info_2 = RubyProf::MethodInfo.new(Base64, :encode64)
call_tree_2 = RubyProf::CallTree.new(method_info_2)
assert_equal(0, call_tree_1 <=> call_tree_2)
method_info_1 = RubyProf::MethodInfo.new(Base64, :decode64)
call_tree_1 = RubyProf::CallTree.new(method_info_1)
call_tree_1.measurement.total_time = 1
method_info_2 = RubyProf::MethodInfo.new(Base64, :encode64)
call_tree_2 = RubyProf::CallTree.new(method_info_2)
assert_equal(1, call_tree_1 <=> call_tree_2)
method_info_1 = RubyProf::MethodInfo.new(Base64, :decode64)
call_tree_1 = RubyProf::CallTree.new(method_info_1)
method_info_2 = RubyProf::MethodInfo.new(Base64, :encode64)
call_tree_2 = RubyProf::CallTree.new(method_info_2)
call_tree_2.measurement.total_time = 1
assert_equal(-1, call_tree_1 <=> call_tree_2)
end
def test_to_s
method_info = RubyProf::MethodInfo.new(Base64, :encode64)
call_tree = RubyProf::CallTree.new(method_info)
assert_equal("<RubyProf::CallTree - Base64#encode64>", call_tree.to_s)
end
def test_add_child
method_info_parent = RubyProf::MethodInfo.new(Base64, :encode64)
call_tree_parent = RubyProf::CallTree.new(method_info_parent)
method_info_child = RubyProf::MethodInfo.new(Array, :pack)
call_tree_child = RubyProf::CallTree.new(method_info_child)
assert_equal(0, call_tree_parent.children.size)
assert_nil(call_tree_child.parent)
result = call_tree_parent.add_child(call_tree_child)
assert_equal(1, call_tree_parent.children.size)
assert_equal(call_tree_child, call_tree_parent.children.first)
assert_equal(call_tree_child, result)
assert_equal(call_tree_parent, call_tree_child.parent)
end
def test_add_child_gc
GC.stress = true
begin
method_info_parent = RubyProf::MethodInfo.new(Base64, :encode64)
call_tree_parent = RubyProf::CallTree.new(method_info_parent)
method_info_child = RubyProf::MethodInfo.new(Array, :pack)
call_tree_child = RubyProf::CallTree.new(method_info_child)
call_tree_parent.add_child(call_tree_child)
# Free the child first
call_tree_child = nil
GC.start
# Now free the parent and make sure it doesn't free the child a second time
call_tree_parent = nil
GC.start
assert(true)
ensure
GC.stress = false
end
end
end