From 3976d68ed317218f18da58bb59b7b7fd909d334b Mon Sep 17 00:00:00 2001 From: Piotr Tabor Date: Thu, 25 Feb 2021 22:34:23 +0100 Subject: [PATCH] raftExample: Allow closing raftexample node when snapshotting. Fix race that made the raftExample test fail. --- Makefile | 2 +- contrib/raftexample/raft.go | 8 ++++++-- contrib/raftexample/raftexample_test.go | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index d53972d56..ee0c3fd9b 100644 --- a/Makefile +++ b/Makefile @@ -151,7 +151,7 @@ test: $(TEST_OPTS) ./test.sh 2>&1 | tee test-$(TEST_SUFFIX).log ! egrep "(--- FAIL:|DATA RACE|panic: test timed out|appears to have leaked)" -B50 -A10 test-$(TEST_SUFFIX).log -test-small: +test-smoke: $(info log-file: test-$(TEST_SUFFIX).log) PASSES="fmt build unit" ./test.sh 2<&1 | tee test-$(TEST_SUFFIX).log diff --git a/contrib/raftexample/raft.go b/contrib/raftexample/raft.go index b79145441..9915128e0 100644 --- a/contrib/raftexample/raft.go +++ b/contrib/raftexample/raft.go @@ -364,9 +364,13 @@ func (rc *raftNode) maybeTriggerSnapshot(applyDoneC <-chan struct{}) { return } - // wait until all committed entries are applied + // wait until all committed entries are applied (or server is closed) if applyDoneC != nil { - <-applyDoneC + select { + case <-applyDoneC: + case <-rc.stopc: + return + } } log.Printf("start snapshot [applied index: %d | last snapshot index: %d]", rc.appliedIndex, rc.snapshotIndex) diff --git a/contrib/raftexample/raftexample_test.go b/contrib/raftexample/raftexample_test.go index c49d8d25c..de3e48a73 100644 --- a/contrib/raftexample/raftexample_test.go +++ b/contrib/raftexample/raftexample_test.go @@ -94,9 +94,11 @@ func (clus *cluster) Close() (err error) { } func (clus *cluster) closeNoErrors(t *testing.T) { + t.Log("closing cluster...") if err := clus.Close(); err != nil { t.Fatal(err) } + t.Log("closing cluster [done]") } // TestProposeOnCommit starts three nodes and feeds commits back into the proposal