-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhealthcheck_test.go
83 lines (78 loc) · 2.12 KB
/
healthcheck_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
package gtm_test
import (
"testing"
"time"
. "github.com/datianshi/simple-cf-gtm"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
)
func testHealthCheck(t *testing.T, when spec.G, it spec.S) {
when("test generic health check", func() {
var healthCheck *DefaultHealthCheck
var endpoints []IP
var ip1, ip2, ip3 IP
var frequency time.Duration
var port int
var wakeUp chan bool
it.Before(func() {
port = 8080
ip1, ip2, ip3 = IP{Address: "192.168.0.1"}, IP{Address: "192.168.0.2"}, IP{Address: "192.168.0.3"}
endpoints = []IP{ip1, ip2, ip3}
wakeUp = make(chan bool)
})
when("Given A health check delete unhealth endpoint ip2", func() {
var removeIP2 bool
it.Before(func() {
removeIP2 = true
frequency = 2 * time.Millisecond
healthCheck = &DefaultHealthCheck{
EndPoints: endpoints,
Frequency: frequency,
CheckHealth: func(ip IP) bool {
if ip == ip2 && removeIP2 {
return false
}
return true
},
SleepFunc: func() {
<-wakeUp
},
}
healthCheck.Start()
})
it("should have 3 ips returns", func() {
result := healthCheck.Receive()
Ω(len(result)).Should(Equal(3))
})
it("should have 3 ips returns on second run after", func() {
healthCheck.Receive()
result := healthCheck.Receive()
Ω(len(result)).Should(Equal(3))
})
it("should only have 2 ips returns on second run after health check is done", func() {
healthCheck.Receive()
wakeUp <- true
result := healthCheck.Receive()
Ω(len(result)).Should(Equal(2))
})
it("should only have 2 ips returns if run again ", func() {
healthCheck.Receive()
wakeUp <- true
healthCheck.Receive()
result := healthCheck.Receive()
Ω(len(result)).Should(Equal(2))
})
it("should come back healthy with all three nodes once health check successful", func() {
healthCheck.Receive()
wakeUp <- true
result := healthCheck.Receive()
Ω(len(result)).Should(Equal(2))
removeIP2 = false
wakeUp <- true
time.Sleep(1 * time.Second)
result = healthCheck.Receive()
Ω(len(result)).Should(Equal(3))
})
})
})
}