From b606d5b5e586e02cbaeeea3be38ec97af14994d4 Mon Sep 17 00:00:00 2001 From: Maurice van Veen Date: Tue, 10 Dec 2024 20:57:51 +0100 Subject: [PATCH] De-flake RAFT state adder tests Signed-off-by: Maurice van Veen --- server/raft_helpers_test.go | 6 ++++++ server/raft_test.go | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/server/raft_helpers_test.go b/server/raft_helpers_test.go index 218258a88db..662a6e94603 100644 --- a/server/raft_helpers_test.go +++ b/server/raft_helpers_test.go @@ -246,6 +246,7 @@ func (a *stateAdder) stop() { a.Lock() defer a.Unlock() a.n.Stop() + a.n.WaitForStop() } // Restart the group @@ -273,6 +274,11 @@ func (a *stateAdder) restart() { panic(err) } + // Must reset in-memory state. + // A real restart would not preserve it, but more importantly we have no way to detect if we + // already applied an entry. So, the sum must only be updated based on append entries or snapshots. + a.sum = 0 + a.n, err = a.s.startRaftNode(globalAccountName, a.cfg, pprofLabels{}) if err != nil { panic(err) diff --git a/server/raft_test.go b/server/raft_test.go index 0f30dec288e..df028fc5485 100644 --- a/server/raft_test.go +++ b/server/raft_test.go @@ -605,8 +605,9 @@ func TestNRGHeartbeatOnLeaderChange(t *testing.T) { leader := rg.leader().(*stateAdder) leader.proposeDelta(22) leader.proposeDelta(-11) - leader.proposeDelta(-11) - rg.waitOnTotal(t, 0) + leader.proposeDelta(-10) + // Must observe forward progress, so each iteration will check +1 total. + rg.waitOnTotal(t, int64(i+1)) leader.stop() leader.restart() rg.waitOnLeader()