etcd/raft/node_test.go

51 lines
1.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"
"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-03 03:59:29 +04:00
n := Start(1, []int64{1}, 0, 0)
2014-08-29 03:41:42 +04:00
ch := make(chan Ready)
go func() {
for {
ch <- <-n.Ready()
}
}()
n.Campaign(ctx)
n.Propose(ctx, []byte("foo"))
want := []Ready{
{
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")}},
},
}
for i, w := range want {
if g := <-ch; !reflect.DeepEqual(g, w) {
t.Errorf("#%d: g = %+v,\n w %+v", i, g, w)
}
}
select {
case rd := <-ch:
t.Errorf("unexpected Ready: %+v", rd)
default:
}
}