1
1
package prometheus
2
2
3
3
import (
4
- "reflect "
4
+ "runtime "
5
5
"testing"
6
6
"time"
7
7
@@ -35,6 +35,9 @@ func TestGoCollector(t *testing.T) {
35
35
case Gauge :
36
36
pb := & dto.Metric {}
37
37
m .Write (pb )
38
+ if pb .GetGauge () == nil {
39
+ continue
40
+ }
38
41
39
42
if old == - 1 {
40
43
old = int (pb .GetGauge ().GetValue ())
@@ -48,8 +51,66 @@ func TestGoCollector(t *testing.T) {
48
51
}
49
52
50
53
return
51
- default :
52
- t .Errorf ("want type Gauge, got %s" , reflect .TypeOf (metric ))
54
+ }
55
+ case <- time .After (1 * time .Second ):
56
+ t .Fatalf ("expected collect timed out" )
57
+ }
58
+ }
59
+ }
60
+
61
+ func TestGCCollector (t * testing.T ) {
62
+ var (
63
+ c = NewGoCollector ()
64
+ ch = make (chan Metric )
65
+ waitc = make (chan struct {})
66
+ closec = make (chan struct {})
67
+ oldGC uint64
68
+ oldPause float64
69
+ )
70
+ defer close (closec )
71
+
72
+ go func () {
73
+ c .Collect (ch )
74
+ // force GC
75
+ runtime .GC ()
76
+ <- waitc
77
+ c .Collect (ch )
78
+ }()
79
+
80
+ first := true
81
+ for {
82
+ select {
83
+ case metric := <- ch :
84
+ switch m := metric .(type ) {
85
+ case * constSummary , * value :
86
+ pb := & dto.Metric {}
87
+ m .Write (pb )
88
+ if pb .GetSummary () == nil {
89
+ continue
90
+ }
91
+
92
+ if len (pb .GetSummary ().Quantile ) != 5 {
93
+ t .Errorf ("expected 4 buckets, got %d" , len (pb .GetSummary ().Quantile ))
94
+ }
95
+ for idx , want := range []float64 {0.0 , 0.25 , 0.5 , 0.75 , 1.0 } {
96
+ if * pb .GetSummary ().Quantile [idx ].Quantile != want {
97
+ t .Errorf ("bucket #%d is off, got %f, want %f" , idx , * pb .GetSummary ().Quantile [idx ].Quantile , want )
98
+ }
99
+ }
100
+ if first {
101
+ first = false
102
+ oldGC = * pb .GetSummary ().SampleCount
103
+ oldPause = * pb .GetSummary ().SampleSum
104
+ close (waitc )
105
+ continue
106
+ }
107
+ if diff := * pb .GetSummary ().SampleCount - oldGC ; diff != 1 {
108
+ t .Errorf ("want 1 new garbage collection run, got %d" , diff )
109
+ }
110
+ if diff := * pb .GetSummary ().SampleSum - oldPause ; diff <= 0 {
111
+ t .Errorf ("want moar pause, got %f" , diff )
112
+ }
113
+ return
53
114
}
54
115
case <- time .After (1 * time .Second ):
55
116
t .Fatalf ("expected collect timed out" )
0 commit comments