-
Notifications
You must be signed in to change notification settings - Fork 0
/
raftexample_test.go
145 lines (125 loc) · 3.44 KB
/
raftexample_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
package main
import (
"fmt"
"os"
"testing"
"github.com/coreos/etcd/raft/raftpb"
)
type cluster struct {
peers []string
commitC []<-chan *string
errorC []<-chan error
proposeC []chan string
confChangeC []chan raftpb.ConfChange
}
// newCluster creates a cluster of n nodes
func newCluster(n int) *cluster {
peers := make([]string, n)
for i := range peers {
peers[i] = fmt.Sprintf("http://127.0.0.1:%d", 10000+i)
}
clus := &cluster{
peers: peers,
commitC: make([]<-chan *string, len(peers)),
errorC: make([]<-chan error, len(peers)),
proposeC: make([]chan string, len(peers)),
confChangeC: make([]chan raftpb.ConfChange, len(peers)),
}
for i := range clus.peers {
os.RemoveAll(fmt.Sprintf("raftexample-%d", i+1))
os.RemoveAll(fmt.Sprintf("raftexample-%d-snap", i+1))
clus.proposeC[i] = make(chan string, 1)
clus.confChangeC[i] = make(chan raftpb.ConfChange, 1)
clus.commitC[i], clus.errorC[i], _ = newRaftNode(i+1, clus.peers, false, nil, clus.proposeC[i], clus.confChangeC[i])
}
return clus
}
// sinkReplay reads all commits in each node's local log.
func (clus *cluster) sinkReplay() {
for i := range clus.peers {
for s := range clus.commitC[i] {
if s == nil {
break
}
}
}
}
// Close closes all cluster nodes and returns an error if any failed.
func (clus *cluster) Close() (err error) {
for i := range clus.peers {
close(clus.proposeC[i])
for range clus.commitC[i] {
// drain pending commits
}
// wait for channel to close
if erri := <-clus.errorC[i]; erri != nil {
err = erri
}
// clean intermediates
os.RemoveAll(fmt.Sprintf("raftexample-%d", i+1))
os.RemoveAll(fmt.Sprintf("raftexample-%d-snap", i+1))
}
return err
}
func (clus *cluster) closeNoErrors(t *testing.T) {
if err := clus.Close(); err != nil {
t.Fatal(err)
}
}
// TestProposeOnCommit starts three nodes and feeds commits back into the proposal
// channel. The intent is to ensure blocking on a proposal won't block raft progress.
func TestProposeOnCommit(t *testing.T) {
clus := newCluster(3)
defer clus.closeNoErrors(t)
clus.sinkReplay()
donec := make(chan struct{})
for i := range clus.peers {
// feedback for "n" committed entries, then update donec
go func(pC chan<- string, cC <-chan *string, eC <-chan error) {
for n := 0; n < 100; n++ {
s, ok := <-cC
if !ok {
pC = nil
}
select {
case pC <- *s:
continue
case err := <-eC:
t.Fatalf("eC message (%v)", err)
}
}
donec <- struct{}{}
for range cC {
// acknowledge the commits from other nodes so
// raft continues to make progress
}
}(clus.proposeC[i], clus.commitC[i], clus.errorC[i])
// one message feedback per node
go func(i int) { clus.proposeC[i] <- "foo" }(i)
}
for range clus.peers {
<-donec
}
}
// TestCloseProposerBeforeReplay tests closing the producer before raft starts.
func TestCloseProposerBeforeReplay(t *testing.T) {
clus := newCluster(1)
// close before replay so raft never starts
defer clus.closeNoErrors(t)
}
// TestCloseProposerInflight tests closing the producer while
// committed messages are being published to the client.
func TestCloseProposerInflight(t *testing.T) {
clus := newCluster(1)
defer clus.closeNoErrors(t)
clus.sinkReplay()
// some inflight ops
go func() {
clus.proposeC[0] <- "foo"
clus.proposeC[0] <- "bar"
}()
// wait for one message
if c, ok := <-clus.commitC[0]; *c != "foo" || !ok {
t.Fatalf("Commit failed")
}
}