-
Notifications
You must be signed in to change notification settings - Fork 56
/
check_linux_test.go
90 lines (75 loc) · 1.69 KB
/
check_linux_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
package tcp
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestCheckerReadyOK(t *testing.T) {
t.Parallel()
c := NewChecker()
assert(t, !c.IsReady())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go c.CheckingLoop(ctx)
select {
case <-time.After(time.Second):
t.FailNow()
case <-c.WaitReady():
}
}
func TestStopNStartChecker(t *testing.T) {
t.Parallel()
// Create checker
c := NewChecker()
// Start checker
ctx, cancel := context.WithCancel(context.Background())
loopStopped := make(chan bool)
go func() {
err := c.CheckingLoop(ctx)
assert(t, err == nil)
loopStopped <- true
}()
// Close the checker
cancel()
<-loopStopped
// Start the checker again
ctx, cancel = context.WithCancel(context.Background())
defer func() {
cancel()
<-loopStopped
}()
go func() {
err := c.CheckingLoop(ctx)
assert(t, err == nil)
loopStopped <- true
}()
// Ensure the check works
_testChecker(t, c)
}
func _startTestServer() (string, context.CancelFunc) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
addr := ts.Listener.Addr().String()
return addr, ts.Close
}
func _testChecker(t *testing.T, c *Checker) {
select {
case <-c.WaitReady():
case <-time.After(time.Second):
}
timeout := time.Second * 2
// Check dead server
err := c.CheckAddr(AddrDead, timeout)
_, ok := err.(*ErrConnect)
assert(t, ok)
// Launch a server for test
addr, stop := _startTestServer()
defer stop()
// Check alive server
err = c.CheckAddr(addr, timeout)
assert(t, err == nil)
// Check non-routable address, thus timeout
err = c.CheckAddr(AddrTimeout, timeout)
assert(t, err == ErrTimeout)
}