Skip to content

Commit 8868e57

Browse files
committed
add test
1 parent 75628e6 commit 8868e57

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

metrics/metrics_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99
)
1010

11+
// TestAdd adds a value and checks that the value is stored.
1112
func TestAdd(t *testing.T) {
1213
stats := New()
1314
stats.Add("name", "label", "value", 1.23)
@@ -29,6 +30,7 @@ func TestAdd(t *testing.T) {
2930
}
3031
}
3132

33+
// TestInc increments a value and checks that the value is stored.
3234
func TestInc(t *testing.T) {
3335
stats := New()
3436
stats.Inc("name", "label", "value", 1)
@@ -49,3 +51,28 @@ func TestInc(t *testing.T) {
4951
t.Errorf("got %s, want %s", got, want)
5052
}
5153
}
54+
55+
// TestAddMetrics adds two metrics and checks that the sum is stored.
56+
func TestAddMetrics(t *testing.T) {
57+
stats := New()
58+
stats.Add("name", "label", "value", 1.23)
59+
stats2 := New()
60+
stats2.Add("name", "label", "value", 1.23)
61+
stats.AddMetrics(stats2)
62+
w := httptest.NewRecorder()
63+
stats.Write(w)
64+
resp := w.Result()
65+
gz, err := gzip.NewReader(resp.Body)
66+
if err != nil {
67+
t.Errorf("error reading gz: %q", err.Error())
68+
}
69+
body, err := io.ReadAll(gz)
70+
if err != nil {
71+
t.Errorf("error reading body: %q", err.Error())
72+
}
73+
got := string(body)
74+
want := "name_seconds_count{label=\"value\"} 2\nname_seconds_sum{label=\"value\"} 2.460"
75+
if !strings.Contains(got, want) {
76+
t.Errorf("got %s, want %s", got, want)
77+
}
78+
}

0 commit comments

Comments
 (0)