From cc792610f4823218eaabf1300490175a07defe4a Mon Sep 17 00:00:00 2001 From: levisyin Date: Fri, 24 May 2024 12:09:49 +0800 Subject: [PATCH] chore: add test case for `SampleSnapshot.Sum` --- metrics/sample_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/metrics/sample_test.go b/metrics/sample_test.go index 92806c65b2..29f3f0a363 100644 --- a/metrics/sample_test.go +++ b/metrics/sample_test.go @@ -103,20 +103,25 @@ func TestExpDecaySample(t *testing.T) { } snap := sample.Snapshot() if have, want := int(snap.Count()), tc.updates; have != want { - t.Errorf("have %d want %d", have, want) + t.Errorf("unexpected count: have %d want %d", have, want) } if have, want := snap.Size(), min(tc.updates, tc.reservoirSize); have != want { - t.Errorf("have %d want %d", have, want) + t.Errorf("unexpected size: have %d want %d", have, want) } values := snap.(*sampleSnapshot).values if have, want := len(values), min(tc.updates, tc.reservoirSize); have != want { - t.Errorf("have %d want %d", have, want) + t.Errorf("unexpected values length: have %d want %d", have, want) } + sum := int64(0) for _, v := range values { + sum += v if v > int64(tc.updates) || v < 0 { t.Errorf("out of range [0, %d]: %v", tc.updates, v) } } + if have, want := snap.Sum(), sum; have != want { + t.Errorf("unexpected sum: have %d want %d", have, want) + } } }