Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Replace t.error/fatal with assert/request in [raft_snap_test.go] #184

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
Loading