From d7a289ee41b800d0ad54a996f83d35adcfaf6bac Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 4 Sep 2014 11:09:55 -0700 Subject: [PATCH 1/2] raft: init prev: vote for none --- raft/node.go | 1 + 1 file changed, 1 insertion(+) diff --git a/raft/node.go b/raft/node.go index 268b3ca5b..870e8746f 100644 --- a/raft/node.go +++ b/raft/node.go @@ -71,6 +71,7 @@ func (n *Node) run(r *raft) { var lead int64 var prev Ready + prev.Vote = none for { if lead != r.lead { log.Printf("raft: leader changed from %#x to %#x", lead, r.lead) From e30505d33bfb8ee04302966aa77e3e6402696e31 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 4 Sep 2014 11:18:09 -0700 Subject: [PATCH 2/2] raft: fix node test --- raft/node_test.go | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/raft/node_test.go b/raft/node_test.go index 1588fcda4..946efbb01 100644 --- a/raft/node_test.go +++ b/raft/node_test.go @@ -12,17 +12,7 @@ func TestNode(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - n := Start(1, []int64{1}, 0, 0) - ch := make(chan Ready) - go func() { - for { - ch <- <-n.Ready() - } - }() - n.Campaign(ctx) - n.Propose(ctx, []byte("foo")) - - want := []Ready{ + wants := []Ready{ { State: raftpb.State{Term: 1, Vote: -1, Commit: 1, LastIndex: 1}, Entries: []raftpb.Entry{{Term: 1, Index: 1}}, @@ -35,16 +25,20 @@ func TestNode(t *testing.T) { }, } - for i, w := range want { - if g := <-ch; !reflect.DeepEqual(g, w) { - t.Errorf("#%d: g = %+v,\n w %+v", i, g, w) - } + 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]) } select { - case rd := <-ch: + case rd := <-n.Ready(): t.Errorf("unexpected Ready: %+v", rd) default: } - }