-
Notifications
You must be signed in to change notification settings - Fork 351
/
metricsinit_test.go
97 lines (85 loc) · 2.05 KB
/
metricsinit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package skipper
import (
"fmt"
"net"
"net/http"
"os"
"syscall"
"testing"
"time"
)
// TODO: what is a more straightforward way to get an unused port?
func availablePort() (port int, err error) {
var l net.Listener
l, err = net.Listen("tcp", ":0")
if err != nil {
return
}
port = l.Addr().(*net.TCPAddr).Port
l.Close()
return
}
func mustAvailablePort(t *testing.T) int {
p, err := availablePort()
if err != nil {
t.Error(t)
}
return p
}
// Initialization order of the metrics.Default global must be done before other packages may start to use it.
func TestInitOrderAndDefault(t *testing.T) {
const (
ringMetricsUpdatePeriod = time.Millisecond
testTimeout = 5 * time.Second
)
port := mustAvailablePort(t)
supportPort := mustAvailablePort(t)
redisPort := mustAvailablePort(t)
sig := make(chan os.Signal, 1)
done := make(chan struct{})
go func() {
options := Options{
Address: fmt.Sprintf(":%d", port),
SupportListener: fmt.Sprintf(":%d", supportPort),
EnableRuntimeMetrics: true,
EnableSwarm: true,
SwarmRedisURLs: []string{fmt.Sprintf("localhost:%d", redisPort)},
EnableRatelimiters: true,
SwarmRedisConnMetricsInterval: ringMetricsUpdatePeriod,
PassiveHealthCheck: map[string]string{
"period": "1m",
"min-requests": "10",
"max-drop-probability": "0.9",
"min-drop-probability": "0.05",
},
}
tornDown := make(chan struct{})
if err := run(options, sig, tornDown); err != nil {
t.Error(err)
}
<-tornDown
close(done)
}()
to := time.After(testTimeout)
func() {
for {
rsp, err := http.Get(fmt.Sprintf("http://localhost:%d/metrics/swarm", supportPort))
if err != nil {
t.Log("error making request", err)
} else {
rsp.Body.Close()
if rsp.StatusCode == http.StatusOK {
return
}
}
select {
case <-time.After(ringMetricsUpdatePeriod):
case <-to:
t.Error("test timeout")
return
}
}
}()
sig <- syscall.SIGTERM
<-done
}