@@ -373,37 +373,38 @@ func TestSummaryVecConcurrency(t *testing.T) {
373
373
func TestSummaryDecay (t * testing.T ) {
374
374
if testing .Short () {
375
375
t .Skip ("Skipping test in short mode." )
376
- // More because it depends on timing than because it is particularly long...
377
376
}
378
377
378
+ now := time .Now ()
379
+
379
380
sum := NewSummary (SummaryOpts {
380
381
Name : "test_summary" ,
381
382
Help : "helpless" ,
382
383
MaxAge : 100 * time .Millisecond ,
383
384
Objectives : map [float64 ]float64 {0.1 : 0.001 },
384
385
AgeBuckets : 10 ,
386
+ now : func () time.Time {
387
+ return now
388
+ },
385
389
})
386
390
387
391
m := & dto.Metric {}
388
- i := 0
389
- tick := time .NewTicker (time .Millisecond )
390
- for range tick .C {
391
- i ++
392
+ for i := 1 ; i <= 1000 ; i ++ {
393
+ now = now .Add (time .Millisecond )
392
394
sum .Observe (float64 (i ))
393
395
if i % 10 == 0 {
394
396
sum .Write (m )
395
- if got , want := * m .Summary .Quantile [0 ].Value , math .Max (float64 (i )/ 10 , float64 (i - 90 )); math .Abs (got - want ) > 20 {
397
+ got := * m .Summary .Quantile [0 ].Value
398
+ want := math .Max (float64 (i )/ 10 , float64 (i - 90 ))
399
+ if math .Abs (got - want ) > 20 {
396
400
t .Errorf ("%d. got %f, want %f" , i , got , want )
397
401
}
398
402
m .Reset ()
399
403
}
400
- if i >= 1000 {
401
- break
402
- }
403
404
}
404
- tick . Stop ()
405
- // Wait for MaxAge without observations and make sure quantiles are NaN.
406
- time . Sleep (100 * time .Millisecond )
405
+
406
+ // Simulate waiting for MaxAge without observations
407
+ now = now . Add (100 * time .Millisecond )
407
408
sum .Write (m )
408
409
if got := * m .Summary .Quantile [0 ].Value ; ! math .IsNaN (got ) {
409
410
t .Errorf ("got %f, want NaN after expiration" , got )
0 commit comments