8
8
"testing"
9
9
)
10
10
11
+ // TestAdd adds a value and checks that the value is stored.
11
12
func TestAdd (t * testing.T ) {
12
13
stats := New ()
13
14
stats .Add ("name" , "label" , "value" , 1.23 )
@@ -29,6 +30,7 @@ func TestAdd(t *testing.T) {
29
30
}
30
31
}
31
32
33
+ // TestInc increments a value and checks that the value is stored.
32
34
func TestInc (t * testing.T ) {
33
35
stats := New ()
34
36
stats .Inc ("name" , "label" , "value" , 1 )
@@ -49,3 +51,28 @@ func TestInc(t *testing.T) {
49
51
t .Errorf ("got %s, want %s" , got , want )
50
52
}
51
53
}
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\n name_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