etcd/raft/node_test.go

224 lines
5.1 KiB
Go
Raw Normal View History

2014-05-29 00:53:26 +04:00
package raft
2014-08-29 03:41:42 +04:00
import (
"reflect"
2014-09-12 09:13:28 +04:00
"runtime"
2014-08-29 03:41:42 +04:00
"testing"
2014-09-12 02:31:47 +04:00
"time"
2014-05-29 00:53:26 +04:00
2014-09-04 07:06:16 +04:00
"github.com/coreos/etcd/raft/raftpb"
"github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
2014-08-29 03:41:42 +04:00
)
2014-09-12 06:00:40 +04:00
// TestNodeStep ensures that node.Step sends msgProp to propc chan
// and other kinds of messages to recvc chan.
2014-09-12 02:31:47 +04:00
func TestNodeStep(t *testing.T) {
for i := range mtmap {
n := &Node{
propc: make(chan raftpb.Message, 1),
recvc: make(chan raftpb.Message, 1),
}
n.Step(context.TODO(), raftpb.Message{Type: int64(i)})
2014-09-12 06:00:40 +04:00
// Proposal goes to proc chan. Others go to recvc chan.
2014-09-12 02:31:47 +04:00
if int64(i) == msgProp {
select {
case <-n.propc:
default:
t.Errorf("%d: cannot receive %s on propc chan", i, mtmap[i])
}
} else {
select {
case <-n.recvc:
default:
t.Errorf("%d: cannot receive %s on recvc chan", i, mtmap[i])
}
}
}
}
// Cancel and Stop should unblock Step()
func TestNodeStepUnblock(t *testing.T) {
2014-09-12 06:00:40 +04:00
// a node without buffer to block step
2014-09-12 02:31:47 +04:00
n := &Node{
propc: make(chan raftpb.Message),
done: make(chan struct{}),
}
ctx, cancel := context.WithCancel(context.Background())
stopFunc := func() { close(n.done) }
tests := []struct {
unblock func()
werr error
}{
{stopFunc, ErrStopped},
{cancel, context.Canceled},
}
for i, tt := range tests {
errc := make(chan error, 1)
go func() {
err := n.Step(ctx, raftpb.Message{Type: msgProp})
errc <- err
}()
tt.unblock()
select {
case err := <-errc:
if err != tt.werr {
t.Errorf("#%d: err = %v, want %v", err, tt.werr)
}
//clean up side-effect
if ctx.Err() != nil {
ctx = context.TODO()
}
select {
case <-n.done:
n.done = make(chan struct{})
default:
}
case <-time.After(time.Millisecond * 100):
t.Errorf("#%d: failed to unblock step", i)
}
}
}
2014-09-12 07:32:55 +04:00
// TestBlockProposal ensures that node will block proposal when it does not
2014-09-12 22:48:08 +04:00
// know who is the current leader; node will accept proposal when it knows
2014-09-12 07:32:55 +04:00
// who is the current leader.
func TestBlockProposal(t *testing.T) {
n := newNode()
r := newRaft(1, []int64{1}, 10, 1)
2014-09-12 09:13:28 +04:00
go n.run(r)
2014-09-12 23:28:15 +04:00
defer n.Stop()
2014-09-12 09:13:28 +04:00
errc := make(chan error, 1)
go func() {
errc <- n.Propose(context.TODO(), []byte("somedata"))
}()
2014-09-12 23:27:18 +04:00
forceGosched()
2014-09-12 09:13:28 +04:00
select {
case err := <-errc:
t.Errorf("err = %v, want blocking", err)
default:
}
n.Campaign(context.TODO())
2014-09-12 23:27:18 +04:00
forceGosched()
2014-09-12 09:13:28 +04:00
select {
case err := <-errc:
if err != nil {
t.Errorf("err = %v, want %v", err, nil)
2014-09-12 07:32:55 +04:00
}
2014-09-12 09:13:28 +04:00
default:
t.Errorf("blocking proposal, want unblocking")
2014-09-12 07:32:55 +04:00
}
2014-09-12 09:13:28 +04:00
}
2014-09-12 07:32:55 +04:00
2014-09-12 09:13:28 +04:00
func TestReadyContainUpdates(t *testing.T) {
tests := []struct {
rd Ready
wcontain bool
}{
{Ready{}, false},
{Ready{State: raftpb.State{Vote: 1}}, true},
{Ready{Entries: make([]raftpb.Entry, 1, 1)}, true},
{Ready{CommittedEntries: make([]raftpb.Entry, 1, 1)}, true},
{Ready{Messages: make([]raftpb.Message, 1, 1)}, true},
}
for i, tt := range tests {
if tt.rd.containsUpdates() != tt.wcontain {
t.Errorf("#%d: containUpdates = %v, want %v", i, tt.rd.containsUpdates(), tt.wcontain)
}
2014-09-12 07:32:55 +04:00
}
}
2014-08-29 03:41:42 +04:00
func TestNode(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2014-09-04 22:18:09 +04:00
wants := []Ready{
2014-08-29 03:41:42 +04:00
{
2014-09-12 10:17:13 +04:00
State: raftpb.State{Term: 1, Commit: 1, LastIndex: 1},
2014-08-29 03:41:42 +04:00
Entries: []raftpb.Entry{{Term: 1, Index: 1}},
CommittedEntries: []raftpb.Entry{{Term: 1, Index: 1}},
},
{
2014-09-12 10:17:13 +04:00
State: raftpb.State{Term: 1, Commit: 2, LastIndex: 2},
2014-08-29 03:41:42 +04:00
Entries: []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
CommittedEntries: []raftpb.Entry{{Term: 1, Index: 2, Data: []byte("foo")}},
},
}
2014-09-04 22:18:09 +04:00
n := Start(1, []int64{1}, 0, 0)
n.Campaign(ctx)
if g := <-n.Ready(); !reflect.DeepEqual(g, wants[0]) {
t.Errorf("#%d: g = %+v,\n w %+v", 1, g, wants[0])
}
n.Propose(ctx, []byte("foo"))
if g := <-n.Ready(); !reflect.DeepEqual(g, wants[1]) {
t.Errorf("#%d: g = %+v,\n w %+v", 2, g, wants[1])
2014-08-29 03:41:42 +04:00
}
select {
2014-09-04 22:18:09 +04:00
case rd := <-n.Ready():
2014-08-29 03:41:42 +04:00
t.Errorf("unexpected Ready: %+v", rd)
default:
}
}
2014-09-05 08:15:39 +04:00
func TestNodeRestart(t *testing.T) {
entries := []raftpb.Entry{
{Term: 1, Index: 1},
{Term: 1, Index: 2, Data: []byte("foo")},
}
2014-09-12 10:17:13 +04:00
st := raftpb.State{Term: 1, Commit: 1, LastIndex: 2}
2014-09-05 08:15:39 +04:00
want := Ready{
2014-09-09 08:45:10 +04:00
State: emptyState,
2014-09-09 03:16:58 +04:00
// commit upto index commit index in st
2014-09-09 03:10:13 +04:00
CommittedEntries: entries[:st.Commit],
2014-09-05 08:15:39 +04:00
}
n := Restart(1, []int64{1}, 0, 0, st, entries)
if g := <-n.Ready(); !reflect.DeepEqual(g, want) {
t.Errorf("g = %+v,\n w %+v", g, want)
}
select {
case rd := <-n.Ready():
t.Errorf("unexpected Ready: %+v", rd)
default:
}
}
2014-09-12 09:13:28 +04:00
2014-09-12 22:48:08 +04:00
func TestIsStateEqual(t *testing.T) {
tests := []struct {
st raftpb.State
we bool
}{
{emptyState, true},
{raftpb.State{Vote: 1}, false},
{raftpb.State{Commit: 1}, false},
{raftpb.State{Term: 1}, false},
{raftpb.State{LastIndex: 1}, false},
}
for i, tt := range tests {
if isStateEqual(tt.st, emptyState) != tt.we {
t.Errorf("#%d, equal = %v, want %v", i, isStateEqual(tt.st, emptyState), tt.we)
}
}
}
2014-09-12 23:18:30 +04:00
// WARNING: This is a hack.
// Remove this when we are able to block/check the status of the go-routines.
2014-09-12 23:27:18 +04:00
func forceGosched() {
2014-09-12 09:13:28 +04:00
// possibility enough to sched upto 10 go routines.
for i := 0; i < 10000; i++ {
runtime.Gosched()
}
}