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