forked from libp2p/go-libp2p-raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor_test.go
91 lines (77 loc) · 1.93 KB
/
actor_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
package libp2praft
import (
"os"
"testing"
)
func TestNewActor(t *testing.T) {
actor := NewActor(nil)
st, err := actor.SetState(&raftState{"testing"})
if st != nil || err == nil {
t.Error("should fail when setting an state and raft is nil")
}
if actor.IsLeader() {
t.Error("should definitely not be a leader if raft is nil")
}
}
func TestSetState(t *testing.T) {
peer1, peer2, pids := makeTwoPeers(t)
defer peer1.Close()
defer peer2.Close()
raft1, _, tr1 := makeTestingRaft(t, peer1, pids, nil)
defer shutdown(t, raft1)
defer tr1.Close()
raft2, _, tr2 := makeTestingRaft(t, peer2, pids, nil)
defer shutdown(t, raft2)
defer tr2.Close()
defer os.RemoveAll(raftTmpFolder)
actor1 := NewActor(raft1)
actor2 := NewActor(raft2)
waitForLeader(t, raft1)
t.Log(actor1.Leader())
t.Log(actor2.Leader())
testLeader := func(actor *Actor) {
st, err := actor.SetState(&raftState{"testingLeader"})
if err != nil {
t.Fatal("the leader should be able to set the state")
}
rSt := st.(*raftState)
if rSt.Msg != "testingLeader" {
t.Error("the returned state is not correct")
}
}
testFollower := func(actor *Actor) {
st, err := actor.SetState(&raftState{"testingFollower"})
if st != nil || err == nil {
t.Error("the follower should not be able to set the state")
}
}
if actor1.IsLeader() {
testLeader(actor1)
} else {
testFollower(actor1)
}
if actor2.IsLeader() {
testLeader(actor2)
} else {
testFollower(actor2)
}
}
func TestActorLeader(t *testing.T) {
peer1, peer2, pids := makeTwoPeers(t)
defer peer1.Close()
defer peer2.Close()
raft1, _, tr1 := makeTestingRaft(t, peer1, pids, nil)
defer shutdown(t, raft1)
defer tr1.Close()
raft2, _, tr2 := makeTestingRaft(t, peer2, pids, nil)
defer shutdown(t, raft2)
defer tr2.Close()
defer os.RemoveAll(raftTmpFolder)
actor1 := NewActor(raft1)
waitForLeader(t, raft1)
l, err := actor1.Leader()
if err != nil {
t.Fatal(err)
}
t.Log("Leader is", l)
}