@@ -21,19 +21,6 @@ def string_id_for(string)
21
21
builder . string_table . fetch ( string )
22
22
end
23
23
24
- describe '#initialize' do
25
- it do
26
- is_expected . to have_attributes (
27
- functions : kind_of ( Datadog ::Profiling ::Pprof ::MessageSet ) ,
28
- locations : kind_of ( Datadog ::Profiling ::Pprof ::MessageSet ) ,
29
- mappings : kind_of ( Datadog ::Profiling ::Pprof ::MessageSet ) ,
30
- sample_types : kind_of ( Datadog ::Profiling ::Pprof ::MessageSet ) ,
31
- samples : [ ] ,
32
- string_table : kind_of ( Datadog ::Profiling ::Pprof ::StringTable )
33
- )
34
- end
35
- end
36
-
37
24
describe '#encode_profile' do
38
25
subject ( :build_profile ) { builder . encode_profile ( profile ) }
39
26
@@ -66,7 +53,7 @@ def string_id_for(string)
66
53
sample_type : builder . sample_types . messages ,
67
54
sample : builder . samples ,
68
55
mapping : builder . mappings . messages ,
69
- location : builder . locations . messages ,
56
+ location : builder . locations . values ,
70
57
function : builder . functions . messages ,
71
58
string_table : builder . string_table . strings
72
59
)
@@ -98,27 +85,13 @@ def string_id_for(string)
98
85
let ( :backtrace_locations ) { Thread . current . backtrace_locations . first ( 3 ) }
99
86
let ( :length ) { backtrace_locations . length }
100
87
101
- let ( :expected_locations ) do
102
- backtrace_locations . each_with_object ( { } ) do |loc , map |
103
- key = [ loc . path , loc . lineno , loc . base_label ]
104
- # Use double instead of instance_double because protobuf doesn't define verifiable methods
105
- map [ key ] = double ( 'Perftools::Profiles::Location' )
106
- end
107
- end
108
-
109
- before do
110
- expect ( builder . locations ) . to receive ( :fetch ) . at_least ( backtrace_locations . length ) . times do |*args , &block |
111
- expect ( expected_locations ) . to include ( args )
112
- expect ( block . source_location ) . to eq ( builder . method ( :build_location ) . source_location )
113
- expected_locations [ args ]
114
- end
115
- end
116
-
117
88
context 'given backtrace locations matching length' do
118
- it do
119
- is_expected . to be_a_kind_of ( Array )
120
- is_expected . to have ( backtrace_locations . length ) . items
121
- is_expected . to include ( *expected_locations . values )
89
+ it { is_expected . to be_a_kind_of ( Array ) }
90
+ it { is_expected . to have ( backtrace_locations . length ) . items }
91
+
92
+ it 'converts the BacktraceLocations to matching Perftools::Profiles::Location objects' do
93
+ # Lines are the simplest to compare, since they aren't converted to ids
94
+ expect ( build_locations . map { |location | location . to_h [ :line ] . first [ :line ] } ) . to eq backtrace_locations . map ( &:lineno )
122
95
end
123
96
end
124
97
@@ -128,56 +101,47 @@ def string_id_for(string)
128
101
let ( :omitted_location ) { double ( 'Perftools::Profiles::Location' ) }
129
102
130
103
before do
131
- expected_locations [ [ '' , 0 , "#{ omitted } #{ described_class ::DESC_FRAMES_OMITTED } " ] ] = omitted_location
104
+ omitted_backtrace_location =
105
+ Datadog ::Profiling ::BacktraceLocation . new ( '' , 0 , "#{ omitted } #{ described_class ::DESC_FRAMES_OMITTED } " )
106
+
107
+ builder . locations [ omitted_backtrace_location ] = omitted_location
132
108
end
133
109
134
- it do
135
- is_expected . to be_a_kind_of ( Array )
136
- is_expected . to have ( backtrace_locations . length + 1 ) . items
137
- is_expected . to include ( *expected_locations . values )
138
- expect ( build_locations . last ) . to be ( omitted_location )
110
+ it { is_expected . to have ( backtrace_locations . length + 1 ) . items }
111
+
112
+ it 'converts the BacktraceLocations to matching Perftools::Profiles::Location objects' do
113
+ expect ( build_locations [ 0 ..-2 ] . map { |location | location . to_h [ :line ] . first [ :line ] } )
114
+ . to eq backtrace_locations . map ( &:lineno )
115
+ end
116
+
117
+ it 'adds a placeholder frame as the last element to indicate the omitted frames' do
118
+ expect ( build_locations . last ) . to be omitted_location
139
119
end
140
120
end
141
121
end
142
122
143
123
describe '#build_location' do
144
- subject ( :build_location ) { builder . build_location ( id , filename , line_number ) }
124
+ subject ( :build_location ) do
125
+ builder . build_location ( location_id , Datadog ::Profiling ::BacktraceLocation . new ( function_name , line_number , filename ) )
126
+ end
145
127
146
- let ( :id ) { id_sequence . next }
147
- let ( :filename ) { double ( 'filename' ) }
128
+ let ( :location_id ) { rand_int }
148
129
let ( :line_number ) { rand_int }
130
+ let ( :function_name ) { 'the_function_name' }
131
+ let ( :filename ) { 'the_file_name.rb' }
149
132
150
- # Use double instead of instance_double because protobuf doesn't define verifiable methods
151
- let ( :function ) { double ( 'Perftools::Profiles::Function' , id : id_sequence . next ) }
152
-
153
- before do
154
- expect ( builder . functions ) . to receive ( :fetch ) do |*args , &block |
155
- expect ( args ) . to eq ( [ filename , nil ] )
156
- expect ( block . source_location ) . to eq ( builder . method ( :build_function ) . source_location )
157
- function
158
- end
159
- end
133
+ it 'creates a new Perftools::Profiles::Location object with the contents of the BacktraceLocation' do
134
+ function = double ( 'Function' , id : rand_int )
160
135
161
- context 'given no function name' do
162
- it do
163
- is_expected . to be_a_kind_of ( Perftools ::Profiles ::Location )
164
- is_expected . to have_attributes (
165
- id : id ,
166
- line : array_including ( kind_of ( Perftools ::Profiles ::Line ) )
167
- )
168
- expect ( build_location . line ) . to have ( 1 ) . items
169
- end
170
-
171
- describe 'returns a Location with Line that' do
172
- subject ( :line ) { build_location . line . first }
136
+ expect ( Perftools ::Profiles ::Function )
137
+ . to receive ( :new ) . with ( hash_including ( filename : string_id_for ( filename ) , name : string_id_for ( function_name ) ) )
138
+ . and_return ( function )
173
139
174
- it do
175
- is_expected . to have_attributes (
176
- function_id : function . id ,
177
- line : line_number
178
- )
179
- end
180
- end
140
+ expect ( build_location ) . to be_a_kind_of ( Perftools ::Profiles ::Location )
141
+ expect ( build_location . to_h ) . to match ( hash_including ( id : location_id ,
142
+ line : [ {
143
+ function_id : function . id , line : line_number
144
+ } ] ) )
181
145
end
182
146
end
183
147
0 commit comments