Skip to content

Commit

Permalink
redolog: refactor redo related test (#3145)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben1009 authored Oct 27, 2021
1 parent bcf4281 commit 0a50f8b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 64 deletions.
8 changes: 4 additions & 4 deletions cdc/redo/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestParseLogFileName(t *testing.T) {
args arg
wantTs uint64
wantFileType string
wantErr bool
wantErr string
}{
{
name: "happy row .log",
Expand Down Expand Up @@ -92,13 +92,13 @@ func TestParseLogFileName(t *testing.T) {
args: arg{
name: fmt.Sprintf("%s_%s_%d_%s%d%s", "cp", "test", time.Now().Unix(), DefaultDDLLogFileType, 1, LogEXT) + TmpEXT,
},
wantErr: true,
wantErr: ".*bad log name*.",
},
}
for _, tt := range tests {
ts, fileType, err := ParseLogFileName(tt.args.name)
if tt.wantErr {
require.NotNil(t, err, tt.name)
if tt.wantErr != "" {
require.Regexp(t, tt.wantErr, err, tt.name)
} else {
require.Nil(t, err, tt.name)
require.EqualValues(t, tt.wantTs, ts, tt.name)
Expand Down
55 changes: 28 additions & 27 deletions cdc/redo/reader/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/ticdc/cdc/redo/writer"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"
)

func TestNewLogReader(t *testing.T) {
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestLogReaderResetReader(t *testing.T) {
name string
args arg
readerErr error
wantErr bool
wantErr string
wantStartTs, wantEndTs uint64
rowFleName string
ddlFleName string
Expand Down Expand Up @@ -122,7 +123,7 @@ func TestLogReaderResetReader(t *testing.T) {
checkPointTs: 0,
resolvedTs: 200,
},
wantErr: true,
wantErr: context.Canceled.Error(),
},
{
name: "invalid ts",
Expand All @@ -133,7 +134,7 @@ func TestLogReaderResetReader(t *testing.T) {
checkPointTs: 0,
resolvedTs: 200,
},
wantErr: true,
wantErr: ".*should match the boundary*.",
},
{
name: "invalid ts",
Expand All @@ -144,7 +145,7 @@ func TestLogReaderResetReader(t *testing.T) {
checkPointTs: 0,
resolvedTs: 200,
},
wantErr: true,
wantErr: ".*should match the boundary*.",
},
{
name: "reader close err",
Expand All @@ -155,7 +156,7 @@ func TestLogReaderResetReader(t *testing.T) {
checkPointTs: 0,
resolvedTs: 200,
},
wantErr: true,
wantErr: "err",
readerErr: errors.New("err"),
},
}
Expand All @@ -180,8 +181,8 @@ func TestLogReaderResetReader(t *testing.T) {
tt.args.ctx = ctx
}
err := r.ResetReader(tt.args.ctx, tt.args.startTs, tt.args.endTs)
if tt.wantErr {
require.NotNil(t, err, tt.name)
if tt.wantErr != "" {
require.Regexp(t, tt.wantErr, err, tt.name)
} else {
require.Nil(t, err, tt.name)
mockReader.AssertNumberOfCalls(t, "Close", 2)
Expand Down Expand Up @@ -233,7 +234,7 @@ func TestLogReaderReadMeta(t *testing.T) {
name string
dir string
wantCheckPointTs, wantResolvedTs uint64
wantErr bool
wantErr string
}{
{
name: "happy",
Expand All @@ -244,19 +245,19 @@ func TestLogReaderReadMeta(t *testing.T) {
{
name: "no meta file",
dir: dir1,
wantErr: true,
wantErr: ".*no redo meta file found in dir*.",
},
{
name: "wrong dir",
dir: "xxx",
wantErr: true,
wantErr: ".*can't read log file directory*.",
},
{
name: "context cancel",
dir: dir,
wantCheckPointTs: meta.CheckPointTs,
wantResolvedTs: meta.ResolvedTs,
wantErr: true,
wantErr: context.Canceled.Error(),
},
}
for _, tt := range tests {
Expand All @@ -272,8 +273,8 @@ func TestLogReaderReadMeta(t *testing.T) {
ctx = ctx1
}
cts, rts, err := l.ReadMeta(ctx)
if tt.wantErr {
require.NotNil(t, err, tt.name)
if tt.wantErr != "" {
require.Regexp(t, tt.wantErr, err, tt.name)
} else {
require.Nil(t, err, tt.name)
require.Equal(t, tt.wantCheckPointTs, cts, tt.name)
Expand All @@ -290,7 +291,7 @@ func TestLogReaderReadNextLog(t *testing.T) {
tests := []struct {
name string
args arg
wantErr bool
wantErr error
readerErr error
readerErr1 error
readerRet *model.RedoLog
Expand Down Expand Up @@ -341,7 +342,7 @@ func TestLogReaderReadNextLog(t *testing.T) {
},
},
},
wantErr: true,
wantErr: context.Canceled,
},
{
name: "happy1",
Expand Down Expand Up @@ -414,7 +415,7 @@ func TestLogReaderReadNextLog(t *testing.T) {
},
readerErr: errors.New("xx"),
readerErr1: errors.New("xx"),
wantErr: true,
wantErr: errors.New("xx"),
},
}

Expand Down Expand Up @@ -448,8 +449,8 @@ func TestLogReaderReadNextLog(t *testing.T) {
tt.args.ctx = ctx1
}
ret, err := l.ReadNextLog(tt.args.ctx, tt.args.maxNum)
if tt.wantErr {
require.NotNil(t, err, tt.name)
if tt.wantErr != nil {
require.True(t, errors.ErrorEqual(tt.wantErr, err), tt.name)
require.Equal(t, 0, len(ret), tt.name)
} else {
require.Nil(t, err, tt.name)
Expand Down Expand Up @@ -478,7 +479,7 @@ func TestLogReaderReadNexDDL(t *testing.T) {
tests := []struct {
name string
args arg
wantErr bool
wantErr error
readerErr error
readerErr1 error
readerRet *model.RedoLog
Expand Down Expand Up @@ -525,7 +526,7 @@ func TestLogReaderReadNexDDL(t *testing.T) {
},
},
},
wantErr: true,
wantErr: context.Canceled,
},
{
name: "happy1",
Expand Down Expand Up @@ -592,7 +593,7 @@ func TestLogReaderReadNexDDL(t *testing.T) {
},
readerErr: errors.New("xx"),
readerErr1: errors.New("xx"),
wantErr: true,
wantErr: errors.New("xx"),
},
}

Expand Down Expand Up @@ -625,8 +626,8 @@ func TestLogReaderReadNexDDL(t *testing.T) {
tt.args.ctx = ctx1
}
ret, err := l.ReadNextDDL(tt.args.ctx, tt.args.maxNum)
if tt.wantErr {
require.NotNil(t, err, tt.name)
if tt.wantErr != nil {
require.True(t, errors.ErrorEqual(tt.wantErr, err), tt.name)
require.Equal(t, 0, len(ret), tt.name)
} else {
require.Nil(t, err, tt.name)
Expand All @@ -649,7 +650,7 @@ func TestLogReaderReadNexDDL(t *testing.T) {
func TestLogReaderClose(t *testing.T) {
tests := []struct {
name string
wantErr bool
wantErr error
err error
}{
{
Expand All @@ -658,7 +659,7 @@ func TestLogReaderClose(t *testing.T) {
{
name: "err",
err: errors.New("xx"),
wantErr: true,
wantErr: multierr.Append(errors.New("xx"), errors.New("xx")),
},
}

Expand All @@ -671,8 +672,8 @@ func TestLogReaderClose(t *testing.T) {
}
err := l.Close()
mockReader.AssertNumberOfCalls(t, "Close", 2)
if tt.wantErr {
require.NotNil(t, err, tt.name)
if tt.wantErr != nil {
require.True(t, errors.ErrorEqual(tt.wantErr, err), tt.name)
} else {
require.Nil(t, err, tt.name)
}
Expand Down
Loading

0 comments on commit 0a50f8b

Please sign in to comment.