@@ -22,6 +22,7 @@ import (
2222 "io"
2323 "io/ioutil"
2424 "log"
25+ "net"
2526 "net/http"
2627 "reflect"
2728 "sort"
@@ -60,35 +61,44 @@ func (s *fakeOpStatus) String() string {
6061 return "Unknown"
6162}
6263
63- func initMgr () (MetricsManager , * sync.WaitGroup , * http.Server ) {
64- wg := & sync.WaitGroup {}
65- wg .Add (1 )
64+ func initMgr () (MetricsManager , * http.Server ) {
6665 mgr := NewMetricsManager ()
67- srv , err := mgr .StartMetricsEndpoint (httpPattern , addr , nil , wg )
66+ mux := http .NewServeMux ()
67+ err := mgr .PrepareMetricsPath (mux , httpPattern , nil )
6868 if err != nil {
6969 log .Fatalf ("failed to start serving [%v]" , err )
7070 }
71- return mgr , wg , srv
71+ l , err := net .Listen ("tcp" , addr )
72+ if err != nil {
73+ log .Fatalf ("failed to listen on address[%s], error[%v]" , addr , err )
74+ }
75+ srv := & http.Server {Addr : l .Addr ().String (), Handler : mux }
76+ go func () {
77+ if err := srv .Serve (l ); err != http .ErrServerClosed {
78+ log .Fatalf ("failed to start endpoint at:%s/%s, error: %v" , addr , httpPattern , err )
79+ }
80+ }()
81+
82+ return mgr , srv
7283}
7384
74- func shutdown (srv * http.Server , wg * sync. WaitGroup ) {
85+ func shutdown (srv * http.Server ) {
7586 if err := srv .Shutdown (context .Background ()); err != nil {
7687 panic (err )
7788 }
78- wg .Wait ()
7989}
8090
8191func TestNew (t * testing.T ) {
82- mgr , wg , srv := initMgr ()
83- defer shutdown (srv , wg )
92+ mgr , srv := initMgr ()
93+ defer shutdown (srv )
8494 if mgr == nil {
8595 t .Errorf ("failed testing new" )
8696 }
8797}
8898
8999func TestDropNonExistingOperation (t * testing.T ) {
90- mgr , wg , srv := initMgr ()
91- defer shutdown (srv , wg )
100+ mgr , srv := initMgr ()
101+ defer shutdown (srv )
92102 op := OperationKey {
93103 Name : "drop-non-existing-operation-should-be-noop" ,
94104 ResourceID : types .UID ("uid" ),
@@ -97,9 +107,9 @@ func TestDropNonExistingOperation(t *testing.T) {
97107}
98108
99109func TestRecordMetricsForNonExistingOperation (t * testing.T ) {
100- mgr , wg , srv := initMgr ()
110+ mgr , srv := initMgr ()
101111 srvAddr := "http://" + srv .Addr + httpPattern
102- defer shutdown (srv , wg )
112+ defer shutdown (srv )
103113 opKey := OperationKey {
104114 Name : "no-metrics-should-be-recorded-as-operation-did-not-start" ,
105115 ResourceID : types .UID ("uid" ),
@@ -119,9 +129,9 @@ func TestRecordMetricsForNonExistingOperation(t *testing.T) {
119129}
120130
121131func TestDropOperation (t * testing.T ) {
122- mgr , wg , srv := initMgr ()
132+ mgr , srv := initMgr ()
123133 srvAddr := "http://" + srv .Addr + httpPattern
124- defer shutdown (srv , wg )
134+ defer shutdown (srv )
125135 opKey := OperationKey {
126136 Name : "should-have-been-dropped" ,
127137 ResourceID : types .UID ("uid" ),
@@ -176,9 +186,9 @@ snapshot_controller_operation_total_seconds_count{driver_name="driver",operation
176186}
177187
178188func TestUnknownStatus (t * testing.T ) {
179- mgr , wg , srv := initMgr ()
189+ mgr , srv := initMgr ()
180190 srvAddr := "http://" + srv .Addr + httpPattern
181- defer shutdown (srv , wg )
191+ defer shutdown (srv )
182192 opKey := OperationKey {
183193 Name : "unknown-status-operation" ,
184194 ResourceID : types .UID ("uid" ),
@@ -214,9 +224,9 @@ snapshot_controller_operation_total_seconds_count{driver_name="driver",operation
214224}
215225
216226func TestRecordMetrics (t * testing.T ) {
217- mgr , wg , srv := initMgr ()
227+ mgr , srv := initMgr ()
218228 srvAddr := "http://" + srv .Addr + httpPattern
219- defer shutdown (srv , wg )
229+ defer shutdown (srv )
220230 // add an operation
221231 opKey := OperationKey {
222232 Name : "op1" ,
@@ -284,9 +294,9 @@ snapshot_controller_operation_total_seconds_count{driver_name="driver2",operatio
284294}
285295
286296func TestConcurrency (t * testing.T ) {
287- mgr , wg , srv := initMgr ()
297+ mgr , srv := initMgr ()
288298 srvAddr := "http://" + srv .Addr + httpPattern
289- defer shutdown (srv , wg )
299+ defer shutdown (srv )
290300 success := & fakeOpStatus {
291301 statusCode : 0 ,
292302 }
@@ -482,8 +492,8 @@ snapshot_controller_operation_total_seconds_count{driver_name="driver5",operatio
482492func TestInFlightMetric (t * testing.T ) {
483493 inFlightCheckInterval = time .Millisecond * 50
484494
485- mgr , wg , srv := initMgr ()
486- defer shutdown (srv , wg )
495+ mgr , srv := initMgr ()
496+ defer shutdown (srv )
487497 srvAddr := "http://" + srv .Addr + httpPattern
488498
489499 // Start first operation, should be 1
@@ -710,8 +720,8 @@ func containsMetrics(expectedMfs, gotMfs []*cmg.MetricFamily) bool {
710720}
711721
712722func TestProcessStartTimeMetricExist (t * testing.T ) {
713- mgr , wg , srv := initMgr ()
714- defer shutdown (srv , wg )
723+ mgr , srv := initMgr ()
724+ defer shutdown (srv )
715725 metricsFamilies , err := mgr .GetRegistry ().Gather ()
716726 if err != nil {
717727 t .Fatalf ("Error fetching metrics: %v" , err )
0 commit comments