@@ -115,42 +115,23 @@ func testHistogram(t *testing.T, profile aggregatortest.Profile, policy policy)
115
115
116
116
agg , ckpt := new2 (descriptor )
117
117
118
- all := aggregatortest .NewNumbers (profile .NumberKind )
119
-
120
- for i := 0 ; i < count ; i ++ {
121
- x := profile .Random (policy .sign ())
122
- all .Append (x )
123
- aggregatortest .CheckedUpdate (t , agg , x , descriptor )
124
- }
125
-
126
- require .NoError (t , agg .SynchronizedMove (ckpt , descriptor ))
127
-
128
- checkZero (t , agg , descriptor )
129
-
130
- all .Sort ()
131
-
132
- asum , err := ckpt .Sum ()
133
- sum := all .Sum ()
134
- require .InEpsilon (t ,
135
- sum .CoerceToFloat64 (profile .NumberKind ),
136
- asum .CoerceToFloat64 (profile .NumberKind ),
137
- 0.000000001 ,
138
- "Same sum - " + policy .name )
139
- require .NoError (t , err )
118
+ // This needs to repeat at least 3 times to uncover a failure to reset
119
+ // for the overall sum and count fields, since the third time through
120
+ // is the first time a `histogram.state` object is reused.
121
+ for repeat := 0 ; repeat < 3 ; repeat ++ {
122
+ all := aggregatortest .NewNumbers (profile .NumberKind )
140
123
141
- count , err := ckpt .Count ()
142
- require .Equal (t , all .Count (), count , "Same count -" + policy .name )
143
- require .NoError (t , err )
124
+ for i := 0 ; i < count ; i ++ {
125
+ x := profile .Random (policy .sign ())
126
+ all .Append (x )
127
+ aggregatortest .CheckedUpdate (t , agg , x , descriptor )
128
+ }
144
129
145
- buckets , err := ckpt .Histogram ()
146
- require .NoError (t , err )
130
+ require .NoError (t , agg .SynchronizedMove (ckpt , descriptor ))
147
131
148
- require . Equal (t , len ( buckets . Counts ), len ( boundaries ) + 1 , "There should be b + 1 counts, where b is the number of boundaries" )
132
+ checkZero (t , agg , descriptor )
149
133
150
- counts := calcBuckets (all .Points (), profile )
151
- for i , v := range counts {
152
- bCount := uint64 (buckets .Counts [i ])
153
- require .Equal (t , v , bCount , "Wrong bucket #%d count: %v != %v" , i , counts , buckets .Counts )
134
+ checkHistogram (t , all , profile , ckpt )
154
135
}
155
136
}
156
137
@@ -191,31 +172,7 @@ func TestHistogramMerge(t *testing.T) {
191
172
192
173
aggregatortest .CheckedMerge (t , ckpt1 , ckpt2 , descriptor )
193
174
194
- all .Sort ()
195
-
196
- asum , err := ckpt1 .Sum ()
197
- sum := all .Sum ()
198
- require .InEpsilon (t ,
199
- sum .CoerceToFloat64 (profile .NumberKind ),
200
- asum .CoerceToFloat64 (profile .NumberKind ),
201
- 0.000000001 ,
202
- "Same sum - absolute" )
203
- require .NoError (t , err )
204
-
205
- count , err := ckpt1 .Count ()
206
- require .Equal (t , all .Count (), count , "Same count - absolute" )
207
- require .NoError (t , err )
208
-
209
- buckets , err := ckpt1 .Histogram ()
210
- require .NoError (t , err )
211
-
212
- require .Equal (t , len (buckets .Counts ), len (boundaries )+ 1 , "There should be b + 1 counts, where b is the number of boundaries" )
213
-
214
- counts := calcBuckets (all .Points (), profile )
215
- for i , v := range counts {
216
- bCount := uint64 (buckets .Counts [i ])
217
- require .Equal (t , v , bCount , "Wrong bucket #%d count: %v != %v" , i , counts , buckets .Counts )
218
- }
175
+ checkHistogram (t , all , profile , ckpt1 )
219
176
})
220
177
}
221
178
@@ -233,22 +190,49 @@ func TestHistogramNotSet(t *testing.T) {
233
190
})
234
191
}
235
192
236
- func calcBuckets (points []number.Number , profile aggregatortest.Profile ) []uint64 {
237
- sortedBoundaries := make ([]float64 , len (boundaries ))
193
+ // checkHistogram ensures the correct aggregated state between `all`
194
+ // (test aggregator) and `agg` (code under test).
195
+ func checkHistogram (t * testing.T , all aggregatortest.Numbers , profile aggregatortest.Profile , agg * histogram.Aggregator ) {
196
+
197
+ all .Sort ()
198
+
199
+ asum , err := agg .Sum ()
200
+ require .NoError (t , err )
201
+
202
+ sum := all .Sum ()
203
+ require .InEpsilon (t ,
204
+ sum .CoerceToFloat64 (profile .NumberKind ),
205
+ asum .CoerceToFloat64 (profile .NumberKind ),
206
+ 0.000000001 )
238
207
208
+ count , err := agg .Count ()
209
+ require .NoError (t , err )
210
+ require .Equal (t , all .Count (), count )
211
+
212
+ buckets , err := agg .Histogram ()
213
+ require .NoError (t , err )
214
+
215
+ require .Equal (t , len (buckets .Counts ), len (boundaries )+ 1 ,
216
+ "There should be b + 1 counts, where b is the number of boundaries" )
217
+
218
+ sortedBoundaries := make ([]float64 , len (boundaries ))
239
219
copy (sortedBoundaries , boundaries )
240
220
sort .Float64s (sortedBoundaries )
241
221
222
+ require .EqualValues (t , sortedBoundaries , buckets .Boundaries )
223
+
242
224
counts := make ([]uint64 , len (sortedBoundaries )+ 1 )
243
225
idx := 0
244
- for _ , p := range points {
226
+ for _ , p := range all . Points () {
245
227
for idx < len (sortedBoundaries ) && p .CoerceToFloat64 (profile .NumberKind ) >= sortedBoundaries [idx ] {
246
228
idx ++
247
229
}
248
230
counts [idx ]++
249
231
}
250
-
251
- return counts
232
+ for i , v := range counts {
233
+ bCount := uint64 (buckets .Counts [i ])
234
+ require .Equal (t , v , bCount , "Wrong bucket #%d count: %v != %v" , i , counts , buckets .Counts )
235
+ }
252
236
}
253
237
254
238
func TestSynchronizedMoveReset (t * testing.T ) {
0 commit comments