-
Notifications
You must be signed in to change notification settings - Fork 38
/
test_helper.go
153 lines (138 loc) · 3.86 KB
/
test_helper.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2013-2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package graft
import (
"fmt"
"os"
"runtime"
"strings"
"testing"
"time"
)
const (
clusterFormationTimeout = MAX_ELECTION_TIMEOUT + 100*time.Millisecond
)
type dummyHandler struct {
}
func (*dummyHandler) AsyncError(err error) {}
func (*dummyHandler) StateChange(from, to State) {}
func (*dummyHandler) CurrentState() []byte { return nil }
func (*dummyHandler) GrantVote(state []byte) bool { return true }
func stackFatalf(t *testing.T, f string, args ...interface{}) {
lines := make([]string, 0, 32)
msg := fmt.Sprintf(f, args...)
lines = append(lines, msg)
// Generate the Stack of callers: Skip us and verify* frames.
for i := 1; true; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
msg := fmt.Sprintf("%d - %s:%d", i, file, line)
lines = append(lines, msg)
}
t.Fatalf("%s", strings.Join(lines, "\n"))
}
func genNodeArgs(t *testing.T) (Handler, RPCDriver, string) {
hand := &dummyHandler{}
rpc := NewMockRpc()
log, err := os.CreateTemp(t.TempDir(), "_grafty_log")
if err != nil {
t.Fatal("Could not create the log file")
}
defer log.Close()
return hand, rpc, log.Name()
}
func createNodes(t *testing.T, name string, numNodes int) []*Node {
ci := ClusterInfo{Name: name, Size: numNodes}
nodes := make([]*Node, numNodes)
for i := 0; i < numNodes; i++ {
hand, rpc, logPath := genNodeArgs(t)
node, err := New(ci, hand, rpc, logPath)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
nodes[i] = node
}
return nodes
}
func countTypes(nodes []*Node) (leaders, followers, candidates int) {
for _, n := range nodes {
switch n.State() {
case LEADER:
leaders++
case FOLLOWER:
followers++
case CANDIDATE:
candidates++
}
}
return
}
func findLeader(nodes []*Node) *Node {
for _, n := range nodes {
if n.State() == LEADER {
return n
}
}
return nil
}
func firstFollower(nodes []*Node) *Node {
for _, n := range nodes {
if n.State() == FOLLOWER {
return n
}
}
return nil
}
func expectedClusterState(t *testing.T, nodes []*Node, leaders, followers, candidates int) {
var currentLeaders, currentFollowers, currentCandidates int
end := time.Now().Add(clusterFormationTimeout)
for time.Now().Before(end) {
currentLeaders, currentFollowers, currentCandidates = countTypes(nodes)
switch {
case leaders != currentLeaders, followers != currentFollowers, candidates != currentCandidates:
time.Sleep(5 * time.Millisecond)
continue
default:
return
}
}
stackFatalf(t, "Cluster doesn't match expect state: expected %d leaders, %d followers, %d candidates, "+
"actual %d Leaders, %d followers, %d candidates\n",
leaders, followers, candidates, currentLeaders, currentFollowers, currentCandidates)
}
func waitForLeader(node *Node, expectedLeader string) string {
curLeader := ""
timeout := time.Now().Add(5 * time.Second)
for time.Now().Before(timeout) {
curLeader = node.Leader()
if curLeader == expectedLeader {
return expectedLeader
}
time.Sleep(10 * time.Millisecond)
}
return curLeader
}
func waitForState(node *Node, expectedState State) State {
curState := FOLLOWER
timeout := time.Now().Add(5 * time.Second)
for time.Now().Before(timeout) {
curState = node.State()
if curState == expectedState {
return expectedState
}
time.Sleep(10 * time.Millisecond)
}
return curState
}