etcd/raft/node_test.go

70 lines
1.7 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"
"testing"
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
)
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
{
State: raftpb.State{Term: 1, Vote: -1, Commit: 1, LastIndex: 1},
Entries: []raftpb.Entry{{Term: 1, Index: 1}},
CommittedEntries: []raftpb.Entry{{Term: 1, Index: 1}},
},
{
State: raftpb.State{Term: 1, Vote: -1, Commit: 2, LastIndex: 2},
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")},
}
st := raftpb.State{Term: 1, Vote: -1, Commit: 1, LastIndex: 2}
want := Ready{
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:
}
}