(t *testing.T)
| 22 | ) |
| 23 | |
| 24 | func TestGoCollectorGoroutines(t *testing.T) { |
| 25 | var ( |
| 26 | c = NewGoCollector() |
| 27 | metricCh = make(chan Metric) |
| 28 | waitCh = make(chan struct{}) |
| 29 | endGoroutineCh = make(chan struct{}) |
| 30 | endCollectionCh = make(chan struct{}) |
| 31 | old = -1 |
| 32 | ) |
| 33 | defer func() { |
| 34 | close(endGoroutineCh) |
| 35 | // Drain the collect channel to prevent goroutine leak. |
| 36 | for { |
| 37 | select { |
| 38 | case <-metricCh: |
| 39 | case <-endCollectionCh: |
| 40 | return |
| 41 | } |
| 42 | } |
| 43 | }() |
| 44 | |
| 45 | go func() { |
| 46 | c.Collect(metricCh) |
| 47 | for i := 1; i <= 10; i++ { |
| 48 | // Start 10 goroutines to be sure we'll detect an |
| 49 | // increase even if unrelated goroutines happen to |
| 50 | // terminate during this test. |
| 51 | go func(c <-chan struct{}) { |
| 52 | <-c |
| 53 | }(endGoroutineCh) |
| 54 | } |
| 55 | <-waitCh |
| 56 | c.Collect(metricCh) |
| 57 | close(endCollectionCh) |
| 58 | }() |
| 59 | |
| 60 | for { |
| 61 | select { |
| 62 | case m := <-metricCh: |
| 63 | // m can be Gauge or Counter, |
| 64 | // currently just test the go_goroutines Gauge |
| 65 | // and ignore others. |
| 66 | if m.Desc().fqName != "go_goroutines" { |
| 67 | continue |
| 68 | } |
| 69 | pb := &dto.Metric{} |
| 70 | m.Write(pb) |
| 71 | if pb.GetGauge() == nil { |
| 72 | continue |
| 73 | } |
| 74 | |
| 75 | if old == -1 { |
| 76 | old = int(pb.GetGauge().GetValue()) |
| 77 | close(waitCh) |
| 78 | continue |
| 79 | } |
| 80 | |
| 81 | if diff := old - int(pb.GetGauge().GetValue()); diff > -1 { |
nothing calls this directly
no test coverage detected