-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestharness.go
160 lines (142 loc) · 3.55 KB
/
testharness.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
154
155
156
157
158
159
160
// Test harness for writing tests for Raft.
//
// Eli Bendersky [https://eli.thegreenplace.net]
// This code is in the public domain.
package raft
import (
"log"
"math/rand"
"testing"
"time"
)
func init() {
log.SetFlags(log.Ltime | log.Lmicroseconds)
rand.Seed(time.Now().UnixNano())
}
type Harness struct {
// cluster is a list of all the raft servers participating in a cluster.
cluster []*Server
// connected has a bool per server in cluster, specifying whether this server
// is currently connected to peers (if false, it's partitioned and no messages
// will pass to or from it).
connected []bool
n int
t *testing.T
}
// NewHarness creates a new test Harness, initialized with n servers connected
// to each other.
func NewHarness(t *testing.T, n int) *Harness {
ns := make([]*Server, n)
connected := make([]bool, n)
ready := make(chan interface{})
// Create all Servers in this cluster, assign ids and peer ids.
for i := 0; i < n; i++ {
peerIds := make([]int, 0)
for p := 0; p < n; p++ {
if p != i {
peerIds = append(peerIds, p)
}
}
ns[i] = NewServer(i, peerIds, ready)
ns[i].Serve()
}
// Connect all peers to each other.
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if i != j {
ns[i].ConnectToPeer(j, ns[j].GetListenAddr())
}
}
connected[i] = true
}
close(ready)
return &Harness{
cluster: ns,
connected: connected,
n: n,
t: t,
}
}
// Shutdown shuts down all the servers in the harness and waits for them to
// stop running.
func (h *Harness) Shutdown() {
for i := 0; i < h.n; i++ {
h.cluster[i].DisconnectAll()
h.connected[i] = false
}
for i := 0; i < h.n; i++ {
h.cluster[i].Shutdown()
}
}
// DisconnectPeer disconnects a server from all other servers in the cluster.
func (h *Harness) DisconnectPeer(id int) {
tlog("Disconnect %d", id)
h.cluster[id].DisconnectAll()
for j := 0; j < h.n; j++ {
if j != id {
h.cluster[j].DisconnectPeer(id)
}
}
h.connected[id] = false
}
// ReconnectPeer connects a server to all other servers in the cluster.
func (h *Harness) ReconnectPeer(id int) {
tlog("Reconnect %d", id)
for j := 0; j < h.n; j++ {
if j != id {
if err := h.cluster[id].ConnectToPeer(j, h.cluster[j].GetListenAddr()); err != nil {
h.t.Fatal(err)
}
if err := h.cluster[j].ConnectToPeer(id, h.cluster[id].GetListenAddr()); err != nil {
h.t.Fatal(err)
}
}
}
h.connected[id] = true
}
// CheckSingleLeader checks that only a single server thinks it's the leader.
// Returns the leader's id and term. It retries several times if no leader is
// identified yet.
func (h *Harness) CheckSingleLeader() (int, int) {
for r := 0; r < 5; r++ {
leaderId := -1
leaderTerm := -1
for i := 0; i < h.n; i++ {
if h.connected[i] {
_, term, isLeader := h.cluster[i].cm.Report()
if isLeader {
if leaderId < 0 {
leaderId = i
leaderTerm = term
} else {
h.t.Fatalf("both %d and %d think they're leaders", leaderId, i)
}
}
}
}
if leaderId >= 0 {
return leaderId, leaderTerm
}
time.Sleep(150 * time.Millisecond)
}
h.t.Fatalf("leader not found")
return -1, -1
}
// CheckNoLeader checks that no connected server considers itself the leader.
func (h *Harness) CheckNoLeader() {
for i := 0; i < h.n; i++ {
if h.connected[i] {
_, _, isLeader := h.cluster[i].cm.Report()
if isLeader {
h.t.Fatalf("server %d leader; want none", i)
}
}
}
}
func tlog(format string, a ...interface{}) {
format = "[TEST] " + format
log.Printf(format, a...)
}
func sleepMs(n int) {
time.Sleep(time.Duration(n) * time.Millisecond)
}