-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_test.go
128 lines (106 loc) · 3.46 KB
/
raft_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
package main
import (
"testing"
)
func TestNewRaft(t *testing.T) {
nodes := []NodeInfo{
{CandidateID: "test1", Endpoint: "test1:8080", URL: "http://test1:80"},
{CandidateID: "test2", Endpoint: "test2:8080", URL: "http://test2:80"},
{CandidateID: "test3", Endpoint: "test3:8080", URL: "http://test3:80"},
}
raft := NewRaft(nodes)
if raft.CurrentTerm != 0 {
t.Error("CurrentTerm should initialize to 0", raft.CurrentTerm)
}
if raft.VotedFor != "" {
t.Error("VotedFor should initialize to an emtpy string:", raft.VotedFor)
}
if raft.CommitedIndex != 0 {
t.Error("CommitedIndex should initialize to 0", raft.CommitedIndex)
}
if raft.LastApplied != 0 {
t.Error("LastApplied should initialize to 0:", raft.LastApplied)
}
for _, node := range nodes {
if raft.NextIndex[node.CandidateID] != 1 {
t.Error("NextIndex should initialize to 1 for all node:", node.CandidateID, "value:", raft.NextIndex[node.CandidateID])
}
if raft.MatchIndex[node.CandidateID] != 0 {
t.Error("MatchIndex should initialize to 0 for all node:", node.CandidateID, "value:", raft.NextIndex[node.CandidateID])
}
}
}
func TestAppendToLog(t *testing.T) {
nodes := []NodeInfo{
{CandidateID: "test1", Endpoint: "test1:8080", URL: "http://test1:80"},
{CandidateID: "test2", Endpoint: "test2:8080", URL: "http://test2:80"},
{CandidateID: "test3", Endpoint: "test3:8080", URL: "http://test3:80"},
}
raft := NewRaft(nodes)
entries := []RaftLog{
{Command: nil, Term: 1},
{Command: "test", Term: 1},
{Command: "test", Term: 1},
}
raft.appendToLog(0, entries)
if len(raft.Log) != 3 {
t.Error("Should add entries:", entries, "got:", raft.Log)
}
entries = []RaftLog{
{Command: nil, Term: 1},
{Command: "test", Term: 2},
{Command: "test", Term: 3},
}
raft.appendToLog(2, entries)
if raft.Log[1].Term != 1 && raft.Log[3].Term != 2 {
t.Error("Should overwrite the log from the 2nd index", raft.Log)
}
entries = []RaftLog{
{Command: nil, Term: 3},
{Command: "test", Term: 3},
{Command: "test", Term: 3},
}
raft.appendToLog(len(raft.Log), entries)
lastLogIdx := len(raft.Log) - 1
if raft.Log[lastLogIdx].Term != 3 ||
raft.Log[lastLogIdx-1].Term != 3 ||
raft.Log[lastLogIdx-2].Term != 3 ||
raft.Log[lastLogIdx-4].Term != 2 {
t.Error("Should append to the end of the log", raft.Log)
}
}
func TestGetLogTerm(t *testing.T) {
nodes := []NodeInfo{
{CandidateID: "test1", Endpoint: "test1:8080", URL: "http://test1:80"},
{CandidateID: "test2", Endpoint: "test2:8080", URL: "http://test2:80"},
{CandidateID: "test3", Endpoint: "test3:8080", URL: "http://test3:80"},
}
raft := NewRaft(nodes)
entries := []RaftLog{
{Command: nil, Term: 1},
{Command: "test", Term: 1},
{Command: "test", Term: 1},
}
raft.appendToLog(0, entries)
if raft.getLogTerm(1) != 1 {
t.Error("Should get term from entry 1 (index=0) and should equal 1")
}
if raft.getLogTerm(0) != 0 {
t.Error("Should return 0 as entry 0 is less than 1")
}
if raft.getLogTerm(50) != 0 {
t.Error("Should return 0 as entry 50 is greater than log length")
}
}
func TestUpdateCommitedIndex(t *testing.T) {
nodes := []NodeInfo{
{CandidateID: "test1", Endpoint: "test1:8080", URL: "http://test1:80"},
{CandidateID: "test2", Endpoint: "test2:8080", URL: "http://test2:80"},
{CandidateID: "test3", Endpoint: "test3:8080", URL: "http://test3:80"},
}
raft := NewRaft(nodes)
raft.updateCommitedIndex(1)
if raft.CommitedIndex != 1 {
t.Error("Should update CommitedIndex to 1, got:", raft.CommitedIndex)
}
}