Skip to content

Commit

Permalink
Merge pull request #184 from MrDXY/replace-test-errors-with-assert-(r…
Browse files Browse the repository at this point in the history
…aft_snap_test.go)

Test: Replace t.error/fatal with assert/request in [raft_snap_test.go]
  • Loading branch information
ahrtr authored Mar 15, 2024
2 parents a76fbc0 + 609d3d4 commit 4f9122f
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions raft_snap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package raft
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

pb "go.etcd.io/raft/v3/raftpb"
)

Expand All @@ -43,9 +46,7 @@ func TestSendingSnapshotSetPendingSnapshot(t *testing.T) {
sm.trk.Progress[2].Next = sm.raftLog.firstIndex()

sm.Step(pb.Message{From: 2, To: 1, Type: pb.MsgAppResp, Index: sm.trk.Progress[2].Next - 1, Reject: true})
if sm.trk.Progress[2].PendingSnapshot != 11 {
t.Fatalf("PendingSnapshot = %d, want 11", sm.trk.Progress[2].PendingSnapshot)
}
require.Equal(t, uint64(11), sm.trk.Progress[2].PendingSnapshot)
}

func TestPendingSnapshotPauseReplication(t *testing.T) {
Expand All @@ -60,9 +61,7 @@ func TestPendingSnapshotPauseReplication(t *testing.T) {

sm.Step(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("somedata")}}})
msgs := sm.readMessages()
if len(msgs) != 0 {
t.Fatalf("len(msgs) = %d, want 0", len(msgs))
}
require.Empty(t, msgs)
}

func TestSnapshotFailure(t *testing.T) {
Expand All @@ -77,15 +76,9 @@ func TestSnapshotFailure(t *testing.T) {
sm.trk.Progress[2].BecomeSnapshot(11)

sm.Step(pb.Message{From: 2, To: 1, Type: pb.MsgSnapStatus, Reject: true})
if sm.trk.Progress[2].PendingSnapshot != 0 {
t.Fatalf("PendingSnapshot = %d, want 0", sm.trk.Progress[2].PendingSnapshot)
}
if sm.trk.Progress[2].Next != 1 {
t.Fatalf("Next = %d, want 1", sm.trk.Progress[2].Next)
}
if !sm.trk.Progress[2].MsgAppFlowPaused {
t.Errorf("MsgAppFlowPaused = %v, want true", sm.trk.Progress[2].MsgAppFlowPaused)
}
require.Zero(t, sm.trk.Progress[2].PendingSnapshot)
require.Equal(t, uint64(1), sm.trk.Progress[2].Next)
assert.True(t, sm.trk.Progress[2].MsgAppFlowPaused)
}

func TestSnapshotSucceed(t *testing.T) {
Expand All @@ -100,15 +93,9 @@ func TestSnapshotSucceed(t *testing.T) {
sm.trk.Progress[2].BecomeSnapshot(11)

sm.Step(pb.Message{From: 2, To: 1, Type: pb.MsgSnapStatus, Reject: false})
if sm.trk.Progress[2].PendingSnapshot != 0 {
t.Fatalf("PendingSnapshot = %d, want 0", sm.trk.Progress[2].PendingSnapshot)
}
if sm.trk.Progress[2].Next != 12 {
t.Fatalf("Next = %d, want 12", sm.trk.Progress[2].Next)
}
if !sm.trk.Progress[2].MsgAppFlowPaused {
t.Errorf("MsgAppFlowPaused = %v, want true", sm.trk.Progress[2].MsgAppFlowPaused)
}
require.Zero(t, sm.trk.Progress[2].PendingSnapshot)
require.Equal(t, uint64(12), sm.trk.Progress[2].Next)
assert.True(t, sm.trk.Progress[2].MsgAppFlowPaused)
}

func TestSnapshotAbort(t *testing.T) {
Expand All @@ -125,17 +112,11 @@ func TestSnapshotAbort(t *testing.T) {
// A successful msgAppResp that has a higher/equal index than the
// pending snapshot should abort the pending snapshot.
sm.Step(pb.Message{From: 2, To: 1, Type: pb.MsgAppResp, Index: 11})
if sm.trk.Progress[2].PendingSnapshot != 0 {
t.Fatalf("PendingSnapshot = %d, want 0", sm.trk.Progress[2].PendingSnapshot)
}
require.Zero(t, sm.trk.Progress[2].PendingSnapshot)
// The follower entered StateReplicate and the leader send an append
// and optimistically updated the progress (so we see 13 instead of 12).
// There is something to append because the leader appended an empty entry
// to the log at index 12 when it assumed leadership.
if sm.trk.Progress[2].Next != 13 {
t.Fatalf("Next = %d, want 13", sm.trk.Progress[2].Next)
}
if n := sm.trk.Progress[2].Inflights.Count(); n != 1 {
t.Fatalf("expected an inflight message, got %d", n)
}
require.Equal(t, uint64(13), sm.trk.Progress[2].Next)
require.Equal(t, 1, sm.trk.Progress[2].Inflights.Count())
}

0 comments on commit 4f9122f

Please sign in to comment.