From fc8cd44c72510a02d6ac2f52bd62910d5d1279cf Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Tue, 3 Jan 2017 15:01:44 -0800 Subject: [PATCH] raft: use status to test node stop n.Tick() is async. It can be racy when running with n.Stop(). n.Status() is sync and has a feedback mechnism internally. So there wont be any race between n.Status() and n.Stop() call. --- raft/node_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/raft/node_test.go b/raft/node_test.go index f502b7c1b..ca7f8c7a2 100644 --- a/raft/node_test.go +++ b/raft/node_test.go @@ -422,9 +422,7 @@ func TestNodeStop(t *testing.T) { close(donec) }() - elapsed := r.electionElapsed - n.Tick() - testutil.WaitSchedule() + status := n.Status() n.Stop() select { @@ -433,13 +431,15 @@ func TestNodeStop(t *testing.T) { t.Fatalf("timed out waiting for node to stop!") } - if r.electionElapsed != elapsed+1 { - t.Errorf("elapsed = %d, want %d", r.electionElapsed, elapsed+1) + emptyStatus := Status{} + + if reflect.DeepEqual(status, emptyStatus) { + t.Errorf("status = %v, want not empty", status) } - // Further ticks should have no effect, the node is stopped. - n.Tick() - if r.electionElapsed != elapsed+1 { - t.Errorf("elapsed = %d, want %d", r.electionElapsed, elapsed+1) + // Further status should return be empty, the node is stopped. + status = n.Status() + if !reflect.DeepEqual(status, emptyStatus) { + t.Errorf("status = %v, want empty", status) } // Subsequent Stops should have no effect. n.Stop()