@@ -8,36 +8,61 @@ import (
88 "time"
99)
1010
11+ // blocks until either IsRunning() returns true or timeout is triggered
12+ func isRunningWithTimeout (t * testing.T , timeout time.Duration ) {
13+ timeoutChan := time .After (timeout )
14+ for ! IsRunning () {
15+ select {
16+ case <- timeoutChan :
17+ t .Errorf ("Monitoring is not running even after %v" , timeout )
18+ return
19+
20+ default :
21+ time .Sleep (10 * time .Millisecond )
22+ }
23+ }
24+ }
25+
26+ // block until either length of metrics is the same as \requiredMessages or timeout is triggered
27+ func hasNumberOfMetrics (t * testing.T , timeout time.Duration , requiredMessages int ) {
28+ timeoutChan := time .After (timeout )
29+ for len (metrics ) != requiredMessages {
30+ select {
31+ case <- timeoutChan :
32+ t .Errorf ("Timeout %v triggered when waiting for %v messages, got %v" , timeout , requiredMessages , len (metrics ))
33+ return
34+
35+ default :
36+ time .Sleep (10 * time .Millisecond )
37+ }
38+ }
39+ }
40+
1141func TestSimpleStartStop (t * testing.T ) {
12- go Start (1234 , "/random" , 100 )
13- time . Sleep ( time .Millisecond * 100 )
42+ go Run (1234 , "/random" , 100 )
43+ isRunningWithTimeout ( t , time .Second )
1444 Stop ()
1545}
1646
1747func TestStartMultipleStop (t * testing.T ) {
18- go Start (1234 , "/random" , 100 )
19- time . Sleep ( time .Millisecond * 100 )
48+ go Run (1234 , "/random" , 100 )
49+ isRunningWithTimeout ( t , time .Second )
2050 Stop ()
2151 Stop ()
2252}
2353
2454func cleaningUpAfterTest () {
25- endChannel <- struct {}{}
26- <- endChannel
27- closeChannels ()
28- metrics = make ([]Metric , 0 )
55+ Stop ()
2956}
3057
3158func initTest () {
32- initChannels (100 )
33- // we need metrics channel to block so we don't end to quickly
34- metricsChannel = make (chan Metric , 0 )
35- go eventLoop ()
59+ go Run (12345 , "notimportant" , 100 )
3660}
3761
3862// decorator function that properly inits and cleans after higher level test of Monitoring package
3963func testFunction (t * testing.T , testToRun func (* testing.T )) {
4064 initTest ()
65+ isRunningWithTimeout (t , time .Second )
4166 testToRun (t )
4267 cleaningUpAfterTest ()
4368}
@@ -46,9 +71,7 @@ func TestSendingSingleMetric(t *testing.T) {
4671 testFunction (t , func (t * testing.T ) {
4772 metric := Metric {Name : "test" }
4873 Send (metric )
49- if len (metrics ) != 1 {
50- t .Error ("wrong number of metrics, should be 1" )
51- }
74+ hasNumberOfMetrics (t , time .Second , 1 )
5275
5376 if metrics [0 ].Name != "test" {
5477 t .Errorf ("Got wrong name %s in stored metric" , metrics [0 ].Name )
@@ -60,16 +83,17 @@ func TestExportingMetrics(t *testing.T) {
6083 testFunction (t , func (t * testing.T ) {
6184 metric := Metric {Name : "test" }
6285 Send (metric )
86+ hasNumberOfMetrics (t , time .Second , 1 )
6387
64- metricsRequestChannel <- struct {}{}
65- metrics := <- metricsToRequest
88+ metricsRequestedChannel <- struct {}{}
89+ metricsToExport := <- metricsExportedToRequest
6690
67- if len (metrics ) != 1 {
68- t .Errorf ("Got wrong amount of metrics %d, expected 1" , len (metrics ))
91+ if len (metricsToExport ) != 1 {
92+ t .Errorf ("Got wrong amount of metrics %d, expected 1" , len (metricsToExport ))
6993 }
7094
71- if metrics [0 ].Name != "test" {
72- t .Errorf ("Got wrong name of metric %s, expected test" , metrics [0 ].Name )
95+ if metricsToExport [0 ].Name != "test" {
96+ t .Errorf ("Got wrong name of metric %s, expected test" , metricsToExport [0 ].Name )
7397 }
7498 })
7599}
@@ -81,11 +105,9 @@ func TestBufferLimit(t *testing.T) {
81105 metric .Timestamp = 10
82106 metric .AddTag ("tag1" , 42 )
83107 metric .AddValue ("value1" , 11 )
84- Send (metric )
85108
86- if len (metrics ) != 1 {
87- t .Errorf ("Metrics length is %d, but should be 1 after sending first metric" , len (metrics ))
88- }
109+ Send (metric )
110+ hasNumberOfMetrics (t , time .Second , 1 )
89111
90112 Send (metric )
91113 time .Sleep (100 * time .Millisecond )
@@ -97,20 +119,20 @@ func TestBufferLimit(t *testing.T) {
97119}
98120
99121func TestHttpRun (t * testing.T ) {
100- go Start ( 12345 , "/metrics" , 10 )
122+ go Run ( 9876 , "/metrics" , 10 )
101123 defer Stop ()
102124
103- time . Sleep ( time .Second )
125+ isRunningWithTimeout ( t , time .Second )
104126
105127 metric := Metric {Name : "test" }
106128 metric .Timestamp = 10
107129 metric .AddTag ("tag1" , 42 )
108130 metric .AddValue ("value1" , 11 )
109131 Send (metric )
110132
111- response , err := http .Get ("http://localhost:12345 /metrics" )
133+ response , err := http .Get ("http://localhost:9876 /metrics" )
112134 if err != nil {
113- t .Fatalf ("Failed to GET metrics at port 12345 : %v" , err )
135+ t .Fatalf ("Failed to GET metrics at port 9876 : %v" , err )
114136 }
115137 decoder := json .NewDecoder (response .Body )
116138 var receivedMetrics []Metric
@@ -157,7 +179,7 @@ func TestHttpRun(t *testing.T) {
157179// PASS
158180// ok github.com/AliceO2Group/Control/common/monitoring 44.686s
159181func BenchmarkSendingMetrics (b * testing.B ) {
160- Start (12345 , "/metrics" , 100 )
182+ Run (12345 , "/metrics" , 100 )
161183
162184 // this goroutine keeps clearing results so RAM does not exhausted
163185 go func () {
@@ -168,8 +190,8 @@ func BenchmarkSendingMetrics(b *testing.B) {
168190 break
169191 default :
170192 if len (metrics ) >= 10000000 {
171- metricsRequestChannel <- struct {}{}
172- <- metricsToRequest
193+ metricsRequestedChannel <- struct {}{}
194+ <- metricsExportedToRequest
173195 }
174196 }
175197 time .Sleep (100 * time .Millisecond )
0 commit comments