From 0c09862494ece343fc6dc536cb722beb8b81ecd6 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 12 Sep 2014 11:48:08 -0700 Subject: [PATCH] raft: add isStateEqual test --- raft/node.go | 2 +- raft/node_test.go | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/raft/node.go b/raft/node.go index 275324306..8b727dded 100644 --- a/raft/node.go +++ b/raft/node.go @@ -35,7 +35,7 @@ type Ready struct { } func isStateEqual(a, b pb.State) bool { - return a.Term == b.Term && a.Vote == b.Vote && a.LastIndex == b.LastIndex + return a.Term == b.Term && a.Vote == b.Vote && a.Commit == b.Commit && a.LastIndex == b.LastIndex } func IsEmptyState(st pb.State) bool { diff --git a/raft/node_test.go b/raft/node_test.go index fde09ecb9..092bb1aba 100644 --- a/raft/node_test.go +++ b/raft/node_test.go @@ -4,6 +4,7 @@ import ( "reflect" "runtime" "testing" + "time" "github.com/coreos/etcd/raft/raftpb" "github.com/coreos/etcd/third_party/code.google.com/p/go.net/context" @@ -82,7 +83,7 @@ func TestNodeStepUnblock(t *testing.T) { } // TestBlockProposal ensures that node will block proposal when it does not -// know who is the current leader; node will direct proposal when it knows +// know who is the current leader; node will accept proposal when it knows // who is the current leader. func TestBlockProposal(t *testing.T) { n := newNode() @@ -193,6 +194,25 @@ func TestNodeRestart(t *testing.T) { } } +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) + } + } +} + func mustEnoughSched() { // possibility enough to sched upto 10 go routines. for i := 0; i < 10000; i++ {