diff --git a/raft_snap_test.go b/raft_snap_test.go index e6058c68..1c54b5b8 100644 --- a/raft_snap_test.go +++ b/raft_snap_test.go @@ -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" ) @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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()) }