-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstate_test.go
87 lines (78 loc) · 1.73 KB
/
state_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
package dinghy
import "testing"
func TestNewState(t *testing.T) {
type args struct {
id int
}
tests := []struct {
name string
args args
wantID int
}{
{"00", args{1}, 1},
{"00", args{123}, 123},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewState(tt.args.id, 0, 0)
equals(t, tt.wantID, got.ID())
equals(t, 1, got.Term())
})
}
}
func TestState_ID(t *testing.T) {
tests := []struct {
name string
state *State
want int
}{
{"00", NewState(1, 0, 0), 1},
{"01", NewState(123, 0, 0), 123},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
equals(t, tt.want, tt.state.ID())
})
}
}
func TestState_LeaderID(t *testing.T) {
s := NewState(1, 0, 0)
equals(t, UnknownLeaderID, s.LeaderID())
s.LeaderID(33)
equals(t, 33, s.LeaderID())
}
func TestState_State(t *testing.T) {
s := NewState(1, 0, 0)
equals(t, StateFollower, s.State())
s.State(StateCandidate)
equals(t, StateCandidate, s.State())
s.State(StateLeader)
equals(t, StateLeader, s.State())
}
func TestState_Term(t *testing.T) {
s := NewState(1, 0, 0)
equals(t, 1, s.Term())
s.Term(s.Term() + 1)
equals(t, 2, s.Term())
}
func TestState_VotedFor(t *testing.T) {
s := NewState(1, 0, 0)
equals(t, NoVote, s.VotedFor())
s.VotedFor(33)
equals(t, 33, s.VotedFor())
}
func TestState_StepDown(t *testing.T) {
s := NewState(1, 0, 0)
s.State(StateLeader)
s.VotedFor(33)
s.StepDown(5)
equals(t, 5, s.Term())
equals(t, StateFollower, s.State())
equals(t, NoVote, s.VotedFor())
}
func TestState_StateString(t *testing.T) {
s := NewState(1, 0, 0)
equals(t, "candidate", s.StateString(StateCandidate))
equals(t, "follower", s.StateString(StateFollower))
equals(t, "leader", s.StateString(StateLeader))
}