MCPcopy
hub / github.com/prometheus/client_golang / TestGoCollectorGC

Function TestGoCollectorGC

prometheus/go_collector_test.go:91–156  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

89}
90
91func TestGoCollectorGC(t *testing.T) {
92 var (
93 c = NewGoCollector()
94 metricCh = make(chan Metric)
95 waitCh = make(chan struct{})
96 endCollectionCh = make(chan struct{})
97 oldGC uint64
98 oldPause float64
99 )
100
101 go func() {
102 c.Collect(metricCh)
103 // force GC
104 runtime.GC()
105 <-waitCh
106 c.Collect(metricCh)
107 close(endCollectionCh)
108 }()
109
110 defer func() {
111 // Drain the collect channel to prevent goroutine leak.
112 for {
113 select {
114 case <-metricCh:
115 case <-endCollectionCh:
116 return
117 }
118 }
119 }()
120
121 first := true
122 for {
123 select {
124 case metric := <-metricCh:
125 pb := &dto.Metric{}
126 metric.Write(pb)
127 if pb.GetSummary() == nil {
128 continue
129 }
130 if len(pb.GetSummary().Quantile) != 5 {
131 t.Errorf("expected 4 buckets, got %d", len(pb.GetSummary().Quantile))
132 }
133 for idx, want := range []float64{0.0, 0.25, 0.5, 0.75, 1.0} {
134 if *pb.GetSummary().Quantile[idx].Quantile != want {
135 t.Errorf("bucket #%d is off, got %f, want %f", idx, *pb.GetSummary().Quantile[idx].Quantile, want)
136 }
137 }
138 if first {
139 first = false
140 oldGC = *pb.GetSummary().SampleCount
141 oldPause = *pb.GetSummary().SampleSum
142 close(waitCh)
143 continue
144 }
145 if diff := *pb.GetSummary().SampleCount - oldGC; diff < 1 {
146 t.Errorf("want at least 1 new garbage collection run, got %d", diff)
147 }
148 if diff := *pb.GetSummary().SampleSum - oldPause; diff <= 0 {

Callers

nothing calls this directly

Calls 3

NewGoCollectorFunction · 0.70
CollectMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected