Skip to content

Commit

Permalink
util/checksum: fix unstable test (#28332)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ranxy authored Sep 25, 2021
1 parent 4ba43a0 commit 955274f
Showing 1 changed file with 47 additions and 47 deletions.
94 changes: 47 additions & 47 deletions util/checksum/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"

encrypt2 "github.com/pingcap/tidb/util/encrypt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestChecksumReadAt(t *testing.T) {
Expand All @@ -32,19 +32,19 @@ func TestChecksumReadAt(t *testing.T) {

csw := NewWriter(NewWriter(NewWriter(NewWriter(f))))
n1, err := csw.Write(w.Bytes())
assert.NoError(t, err)
require.NoError(t, err)
n2, err := csw.Write(w.Bytes())
assert.NoError(t, err)
require.NoError(t, err)
err = csw.Close()
assert.NoError(t, err)
require.NoError(t, err)

assertReadAt := func(off int64, assertErr error, assertN int, assertString string) {
cs := NewReader(NewReader(NewReader(NewReader(f))))
r := make([]byte, 10)
n, err := cs.ReadAt(r, off)
assert.ErrorIs(t, err, assertErr)
assert.Equal(t, assertN, n)
assert.Equal(t, assertString, string(r))
require.ErrorIs(t, err, assertErr)
require.Equal(t, assertN, n)
require.Equal(t, assertString, string(r))
}

assertReadAt(0, nil, 10, "0123456789")
Expand Down Expand Up @@ -88,9 +88,9 @@ func testAddOneByte(t *testing.T, encrypt bool) {
break
}
if i < 5 {
assert.NoError(t, err)
require.NoError(t, err)
} else {
assert.ErrorIs(t, err, errChecksumFail)
require.ErrorIs(t, err, errChecksumFail)
}
}
}
Expand Down Expand Up @@ -131,9 +131,9 @@ func testDeleteOneByte(t *testing.T, encrypt bool) {
break
}
if i < 5 {
assert.NoError(t, err)
require.NoError(t, err)
} else {
assert.ErrorIs(t, err, errChecksumFail)
require.ErrorIs(t, err, errChecksumFail)
}
}
}
Expand All @@ -158,7 +158,7 @@ func testModifyOneByte(t *testing.T, encrypt bool) {
fc := func(b []byte, offset int) []byte {
if offset < modifyPos && offset+len(b) >= modifyPos {
pos := modifyPos - offset
b[pos-1] = 255
b[pos-1] = b[pos-1] - 1
}
return b
}
Expand All @@ -174,9 +174,9 @@ func testModifyOneByte(t *testing.T, encrypt bool) {
break
}
if i != 5 {
assert.NoError(t, err)
require.NoError(t, err)
} else {
assert.ErrorIs(t, err, errChecksumFail)
require.ErrorIs(t, err, errChecksumFail)
}
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func testReadEmptyFile(t *testing.T, encrypt bool) {
underlying = NewReader(underlying)
r := make([]byte, 10)
_, err := underlying.ReadAt(r, int64(i*1020))
assert.ErrorIs(t, err, io.EOF)
require.ErrorIs(t, err, io.EOF)
}
}

Expand All @@ -238,9 +238,9 @@ func testModifyThreeBytes(t *testing.T, encrypt bool) {
if offset < modifyPos && offset+len(b) >= modifyPos {
// modify 3 bytes
if len(b) == 1024 {
b[200] = 255
b[300] = 255
b[400] = 255
b[200] = b[200] - 1
b[300] = b[300] - 1
b[400] = b[400] - 1
}
}
return b
Expand All @@ -257,9 +257,9 @@ func testModifyThreeBytes(t *testing.T, encrypt bool) {
break
}
if i != 5 {
assert.NoError(t, err)
require.NoError(t, err)
} else {
assert.ErrorIs(t, err, errChecksumFail)
require.ErrorIs(t, err, errChecksumFail)
}
}
}
Expand Down Expand Up @@ -296,11 +296,11 @@ func testReadDifferentBlockSize(t *testing.T, encrypt bool) {

w := newTestBuff("0123456789", 510)
_, err = underlying.Write(w.Bytes())
assert.NoError(t, err)
require.NoError(t, err)
_, err = underlying.Write(w.Bytes())
assert.NoError(t, err)
require.NoError(t, err)
err = underlying.Close()
assert.NoError(t, err)
require.NoError(t, err)

assertReadAt := assertReadAtFunc(t, encrypt, ctrCipher)

Expand Down Expand Up @@ -363,28 +363,28 @@ func testWriteDifferentBlockSize(t *testing.T, encrypt bool) {

// Write all data.
_, err = underlying1.Write(w.Bytes())
assert.NoError(t, err)
require.NoError(t, err)
err = underlying1.Close()
assert.NoError(t, err)
require.NoError(t, err)

// Write data by 100 bytes one batch.
lastPos := 0
for i := 100; ; i += 100 {
if i < len(w.Bytes()) {
_, err = underlying2.Write(w.Bytes()[lastPos:i])
assert.NoError(t, err)
require.NoError(t, err)
lastPos = i
} else {
_, err = underlying2.Write(w.Bytes()[lastPos:])
assert.NoError(t, err)
require.NoError(t, err)
break
}
}
err = underlying2.Close()
assert.NoError(t, err)
require.NoError(t, err)

// check two files is same
assert.EqualValues(t, f1.buf.Bytes(), f2.buf.Bytes())
require.EqualValues(t, f1.buf.Bytes(), f2.buf.Bytes())

// check data
assertReadAt := assertReadAtFunc(t, encrypt, ctrCipher)
Expand All @@ -400,16 +400,16 @@ func TestChecksumWriter(t *testing.T) {
// Write 1000 bytes and flush.
w := NewWriter(f)
n, err := w.Write(buf.Bytes())
assert.NoError(t, err)
assert.Equal(t, 1000, n)
require.NoError(t, err)
require.Equal(t, 1000, n)

err = w.Flush()
assert.NoError(t, err)
require.NoError(t, err)
checkFlushedData(t, f, 0, 1000, 1000, nil, buf.Bytes())

// All data flushed, so no data in cache.
cacheOff := w.GetCacheDataOffset()
assert.Equal(t, int64(1000), cacheOff)
require.Equal(t, int64(1000), cacheOff)
}

func TestChecksumWriterAutoFlush(t *testing.T) {
Expand All @@ -419,16 +419,16 @@ func TestChecksumWriterAutoFlush(t *testing.T) {
buf := newTestBuff("0123456789", 102)
w := NewWriter(f)
n, err := w.Write(buf.Bytes())
assert.NoError(t, err)
assert.Equal(t, len(buf.Bytes()), n)
require.NoError(t, err)
require.Equal(t, len(buf.Bytes()), n)

// This write will trigger flush.
n, err = w.Write([]byte("0"))
assert.NoError(t, err)
assert.Equal(t, 1, n)
require.NoError(t, err)
require.Equal(t, 1, n)
checkFlushedData(t, f, 0, 1020, 1020, nil, buf.Bytes())
cacheOff := w.GetCacheDataOffset()
assert.Equal(t, int64(len(buf.Bytes())), cacheOff)
require.Equal(t, int64(len(buf.Bytes())), cacheOff)
}

func newTestBuff(str string, n int) *bytes.Buffer {
Expand Down Expand Up @@ -487,11 +487,11 @@ func assertUnderlyingWrite(t *testing.T, encrypt bool, f io.WriteCloser, fc func

w := newTestBuff("0123456789", 510)
_, err = underlying.Write(w.Bytes())
assert.NoError(t, err)
require.NoError(t, err)
_, err = underlying.Write(w.Bytes())
assert.NoError(t, err)
require.NoError(t, err)
err = underlying.Close()
assert.NoError(t, err)
require.NoError(t, err)
return ctrCipher, false
}

Expand All @@ -516,19 +516,19 @@ func assertReadAtFunc(t *testing.T, encrypt bool, ctrCipher *encrypt2.CtrCipher)

underlying = NewReader(underlying)
n, err := underlying.ReadAt(r, off)
assert.ErrorIs(t, err, assertErr)
assert.Equal(t, assertN, n)
assert.Equal(t, assertString, string(r))
require.ErrorIs(t, err, assertErr)
require.Equal(t, assertN, n)
require.Equal(t, assertString, string(r))
}
}

var checkFlushedData = func(t *testing.T, f io.ReaderAt, off int64, readBufLen int, assertN int, assertErr error, assertRes []byte) {
readBuf := make([]byte, readBufLen)
r := NewReader(f)
n, err := r.ReadAt(readBuf, off)
assert.ErrorIs(t, err, assertErr)
assert.Equal(t, assertN, n)
assert.Equal(t, 0, bytes.Compare(readBuf, assertRes))
require.ErrorIs(t, err, assertErr)
require.Equal(t, assertN, n)
require.Equal(t, 0, bytes.Compare(readBuf, assertRes))
}

func newFakeFile() *fakeFile {
Expand Down

0 comments on commit 955274f

Please sign in to comment.