forked from cashapp/pranadb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth_checker_test.go
136 lines (117 loc) · 3.85 KB
/
health_checker_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package remoting
import (
"fmt"
"github.com/squareup/pranadb/common/commontest"
"github.com/stretchr/testify/require"
"sync"
"testing"
"time"
)
func TestStartStopServers(t *testing.T) {
servers, serverAddresses, al, ht := setup()
// Start servers one by one
waitUntilDesiredState(t, []bool{true, false, false}, serverAddresses, servers, al)
waitUntilDesiredState(t, []bool{true, true, false}, serverAddresses, servers, al)
waitUntilDesiredState(t, []bool{true, true, true}, serverAddresses, servers, al)
// Stop one by one
waitUntilDesiredState(t, []bool{false, true, true}, serverAddresses, servers, al)
waitUntilDesiredState(t, []bool{false, false, true}, serverAddresses, servers, al)
waitUntilDesiredState(t, []bool{false, false, false}, serverAddresses, servers, al)
// And start all again
waitUntilDesiredState(t, []bool{true, true, true}, serverAddresses, servers, al)
ht.Stop()
stopServers(t, servers...)
}
func TestNoHeartbeats(t *testing.T) {
servers, serverAddresses, al, ht := setup()
waitUntilDesiredState(t, []bool{true, true, true}, serverAddresses, servers, al)
servers[0].DisableResponses()
waitUntilDesiredState(t, []bool{false, true, true}, serverAddresses, servers, al)
servers[1].DisableResponses()
waitUntilDesiredState(t, []bool{false, false, true}, serverAddresses, servers, al)
servers[2].DisableResponses()
waitUntilDesiredState(t, []bool{false, false, false}, serverAddresses, servers, al)
ht.Stop()
stopServers(t, servers...)
}
func setup() (servers []*server, serverAddresses []string, al *availabilityListener, ht *HealthChecker) {
servers = createServers(3)
serverAddresses = make([]string, 3)
for i := 0; i < 3; i++ {
serverAddresses[i] = fmt.Sprintf("localhost:%d", 7888+i)
}
hbTimeout := 1 * time.Second
hbInterval := 2 * time.Second
ht = NewHealthChecker(serverAddresses, hbTimeout, hbInterval)
al = newAvailabilityListener()
ht.AddAvailabilityListener(al)
ht.Start()
return
}
// Start or stop the servers as appropriate until they are started/stopped according to desiredState - then verify
// availability status is correct
func waitUntilDesiredState(t *testing.T, desiredState []bool, serverAddresses []string, servers []*server, al *availabilityListener) {
t.Helper()
for i, desired := range desiredState {
serverAddress := serverAddresses[i]
current := al.getAvailability(serverAddress)
if !current && desired {
err := servers[i].Start()
require.NoError(t, err)
} else if current && !desired {
err := servers[i].Stop()
require.NoError(t, err)
}
}
commontest.WaitUntil(t, func() (bool, error) {
for i, desired := range desiredState {
serverAddress := serverAddresses[i]
current := al.getAvailability(serverAddress)
if current != desired {
return false, nil
}
}
return true, nil
})
}
type availabilityListener struct {
availStatuses map[string]struct{}
lock sync.Mutex
}
func newAvailabilityListener() *availabilityListener {
return &availabilityListener{
availStatuses: map[string]struct{}{},
}
}
func (a *availabilityListener) AvailabilityChanged(availableAddresses []string) {
a.lock.Lock()
defer a.lock.Unlock()
availMap := map[string]struct{}{}
for _, address := range availableAddresses {
availMap[address] = struct{}{}
}
for address := range a.availStatuses {
_, ok := availMap[address]
if !ok {
delete(a.availStatuses, address)
}
}
for _, address := range availableAddresses {
a.availStatuses[address] = struct{}{}
}
}
func (a *availabilityListener) getAvailability(serverAddress string) bool {
a.lock.Lock()
defer a.lock.Unlock()
_, ok := a.availStatuses[serverAddress]
return ok
}
func createServers(numServers int) []*server {
servers := make([]*server, numServers)
for i := 0; i < numServers; i++ {
listenPort := 7888 + i
listenAddress := fmt.Sprintf("localhost:%d", listenPort)
servers[i] = newServer(listenAddress)
}
return servers
}