Skip to content

Commit

Permalink
De-flake RAFT state adder tests (#6239)
Browse files Browse the repository at this point in the history
Fixes `TestNRGHeartbeatOnLeaderChange` by checking if new entries are
actually being applied. This was not possible before because it would
always check a total of 0. If all proposals would fail, this test would
still pass.

More importantly `a.sum` was not reset, which meant that state before
the call to `restart()` would be preserved, and replayed entries during
catchup/recovery would be re-applied.

Also added a call to `a.n.WaitForStop()` in `a.stop()`. Otherwise things
could still be around and not properly stopped when we restart with
`a.restart()`.

Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
  • Loading branch information
derekcollison authored Dec 10, 2024
2 parents 8fecf06 + 00f3b5f commit a3a6551
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions server/raft_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func (a *stateAdder) stop() {
a.Lock()
defer a.Unlock()
a.n.Stop()
a.n.WaitForStop()
}

// Restart the group
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions server/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a3a6551

Please sign in to comment.