-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
197 lines (162 loc) · 4.9 KB
/
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package raft
import (
"fmt"
"io/ioutil"
"os"
"time"
)
const (
testHeartbeatInterval = 50 * time.Millisecond
testElectionTimeout = 200 * time.Millisecond
)
const (
testListenerLoggerEnabled = false
)
func init() {
RegisterCommand(&testCommand1{})
RegisterCommand(&testCommand2{})
}
//------------------------------------------------------------------------------
//
// Helpers
//
//------------------------------------------------------------------------------
//--------------------------------------
// Logs
//--------------------------------------
func getLogPath() string {
f, _ := ioutil.TempFile("", "raft-log-")
f.Close()
os.Remove(f.Name())
return f.Name()
}
func setupLog(entries []*LogEntry) (*Log, string) {
f, _ := ioutil.TempFile("", "raft-log-")
for _, entry := range entries {
entry.Encode(f)
}
err := f.Close()
if err != nil {
panic(err)
}
log := newLog()
log.ApplyFunc = func(e *LogEntry, c Command) (interface{}, error) {
return nil, nil
}
if err := log.open(f.Name()); err != nil {
panic(err)
}
return log, f.Name()
}
//--------------------------------------
// Servers
//--------------------------------------
func newTestServer(name string, transporter Transporter) Server {
p, _ := ioutil.TempDir("", "raft-server-")
if err := os.MkdirAll(p, 0644); err != nil {
panic(err.Error())
}
server, _ := NewServer(name, p, transporter, nil, nil, "")
if testListenerLoggerEnabled {
fn := func(e Event) {
server := e.Source().(Server)
warnf("[%s] %s %v -> %v\n", server.Name(), e.Type(), e.PrevValue(), e.Value())
}
server.AddEventListener(StateChangeEventType, fn)
server.AddEventListener(LeaderChangeEventType, fn)
server.AddEventListener(TermChangeEventType, fn)
}
return server
}
func newTestServerWithPath(name string, transporter Transporter, p string) Server {
server, _ := NewServer(name, p, transporter, nil, nil, "")
return server
}
func newTestServerWithLog(name string, transporter Transporter, entries []*LogEntry) Server {
server := newTestServer(name, transporter)
f, err := os.Create(server.LogPath())
if err != nil {
panic(err)
}
for _, entry := range entries {
entry.Encode(f)
}
f.Close()
return server
}
func newTestCluster(names []string, transporter Transporter, lookup map[string]Server) []Server {
servers := []Server{}
e0, _ := newLogEntry(newLog(), nil, 1, 1, &testCommand1{Val: "foo", I: 20})
for _, name := range names {
if lookup[name] != nil {
panic(fmt.Sprintf("raft: Duplicate server in test cluster! %v", name))
}
server := newTestServerWithLog(name, transporter, []*LogEntry{e0})
server.SetElectionTimeout(testElectionTimeout)
servers = append(servers, server)
lookup[name] = server
}
for _, server := range servers {
server.SetHeartbeatInterval(testHeartbeatInterval)
server.Start()
for _, peer := range servers {
server.AddPeer(peer.Name(), "")
}
}
return servers
}
//--------------------------------------
// Transporter
//--------------------------------------
type testTransporter struct {
sendVoteRequestFunc func(server Server, peer *Peer, req *RequestVoteRequest) *RequestVoteResponse
sendAppendEntriesRequestFunc func(server Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse
sendSnapshotRequestFunc func(server Server, peer *Peer, req *SnapshotRequest) *SnapshotResponse
}
func (t *testTransporter) SendVoteRequest(server Server, peer *Peer, req *RequestVoteRequest) *RequestVoteResponse {
return t.sendVoteRequestFunc(server, peer, req)
}
func (t *testTransporter) SendAppendEntriesRequest(server Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse {
return t.sendAppendEntriesRequestFunc(server, peer, req)
}
func (t *testTransporter) SendSnapshotRequest(server Server, peer *Peer, req *SnapshotRequest) *SnapshotResponse {
return t.sendSnapshotRequestFunc(server, peer, req)
}
func (t *testTransporter) SendSnapshotRecoveryRequest(server Server, peer *Peer, req *SnapshotRecoveryRequest) *SnapshotRecoveryResponse {
return t.SendSnapshotRecoveryRequest(server, peer, req)
}
type testStateMachine struct {
saveFunc func() ([]byte, error)
recoveryFunc func([]byte) error
}
func (sm *testStateMachine) Save() ([]byte, error) {
return sm.saveFunc()
}
func (sm *testStateMachine) Recovery(state []byte) error {
return sm.recoveryFunc(state)
}
//--------------------------------------
// Command1
//--------------------------------------
type testCommand1 struct {
Val string `json:"val"`
I int `json:"i"`
}
func (c *testCommand1) CommandName() string {
return "cmd_1"
}
func (c *testCommand1) Apply(server Server) (interface{}, error) {
return nil, nil
}
//--------------------------------------
// Command2
//--------------------------------------
type testCommand2 struct {
X int `json:"x"`
}
func (c *testCommand2) CommandName() string {
return "cmd_2"
}
func (c *testCommand2) Apply(server Server) (interface{}, error) {
return nil, nil
}