scan_test, safepoint_test: replace pingcap/check with testify (#157)

Signed-off-by: disksing <i@disksing.com>
This commit is contained in:
disksing 2021-06-24 17:24:33 +08:00 committed by GitHub
parent ad7f45e411
commit 65beebd7ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 113 additions and 101 deletions

View File

@ -35,35 +35,39 @@ package tikv_test
import (
"context"
"fmt"
"testing"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/parser/terror"
tikverr "github.com/tikv/client-go/v2/error"
"github.com/stretchr/testify/suite"
"github.com/tikv/client-go/v2/error"
"github.com/tikv/client-go/v2/tikv"
)
func TestSafepoint(t *testing.T) {
suite.Run(t, new(testSafePointSuite))
}
type testSafePointSuite struct {
suite.Suite
store tikv.StoreProbe
prefix string
}
var _ = SerialSuites(&testSafePointSuite{})
func (s *testSafePointSuite) SetUpSuite(c *C) {
s.store = tikv.StoreProbe{KVStore: NewTestStore(c)}
func (s *testSafePointSuite) SetupSuite() {
s.store = tikv.StoreProbe{KVStore: NewTestStoreT(s.T())}
s.prefix = fmt.Sprintf("seek_%d", time.Now().Unix())
}
func (s *testSafePointSuite) TearDownSuite(c *C) {
func (s *testSafePointSuite) TearDownSuite() {
err := s.store.Close()
c.Assert(err, IsNil)
s.Require().Nil(err)
}
func (s *testSafePointSuite) beginTxn(c *C) tikv.TxnProbe {
func (s *testSafePointSuite) beginTxn() tikv.TxnProbe {
txn, err := s.store.Begin()
c.Assert(err, IsNil)
s.Require().Nil(err)
return txn
}
@ -89,52 +93,52 @@ func (s *testSafePointSuite) waitUntilErrorPlugIn(t uint64) {
}
}
func (s *testSafePointSuite) TestSafePoint(c *C) {
txn := s.beginTxn(c)
func (s *testSafePointSuite) TestSafePoint() {
txn := s.beginTxn()
for i := 0; i < 10; i++ {
err := txn.Set(encodeKey(s.prefix, s08d("key", i)), valueBytes(i))
c.Assert(err, IsNil)
s.Nil(err)
}
err := txn.Commit(context.Background())
c.Assert(err, IsNil)
s.Nil(err)
// for txn get
txn2 := s.beginTxn(c)
txn2 := s.beginTxn()
_, err = txn2.Get(context.TODO(), encodeKey(s.prefix, s08d("key", 0)))
c.Assert(err, IsNil)
s.Nil(err)
s.waitUntilErrorPlugIn(txn2.StartTS())
_, geterr2 := txn2.Get(context.TODO(), encodeKey(s.prefix, s08d("key", 0)))
c.Assert(geterr2, NotNil)
s.NotNil(geterr2)
_, isFallBehind := errors.Cause(geterr2).(*tikverr.ErrGCTooEarly)
isMayFallBehind := terror.ErrorEqual(errors.Cause(geterr2), tikverr.NewErrPDServerTimeout("start timestamp may fall behind safe point"))
_, isFallBehind := errors.Cause(geterr2).(*error.ErrGCTooEarly)
isMayFallBehind := terror.ErrorEqual(errors.Cause(geterr2), error.NewErrPDServerTimeout("start timestamp may fall behind safe point"))
isBehind := isFallBehind || isMayFallBehind
c.Assert(isBehind, IsTrue)
s.True(isBehind)
// for txn seek
txn3 := s.beginTxn(c)
txn3 := s.beginTxn()
s.waitUntilErrorPlugIn(txn3.StartTS())
_, seekerr := txn3.Iter(encodeKey(s.prefix, ""), nil)
c.Assert(seekerr, NotNil)
_, isFallBehind = errors.Cause(geterr2).(*tikverr.ErrGCTooEarly)
isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), tikverr.NewErrPDServerTimeout("start timestamp may fall behind safe point"))
s.NotNil(seekerr)
_, isFallBehind = errors.Cause(geterr2).(*error.ErrGCTooEarly)
isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), error.NewErrPDServerTimeout("start timestamp may fall behind safe point"))
isBehind = isFallBehind || isMayFallBehind
c.Assert(isBehind, IsTrue)
s.True(isBehind)
// for snapshot batchGet
keys := mymakeKeys(10, s.prefix)
txn4 := s.beginTxn(c)
txn4 := s.beginTxn()
s.waitUntilErrorPlugIn(txn4.StartTS())
_, batchgeterr := toTiDBTxn(&txn4).BatchGet(context.Background(), toTiDBKeys(keys))
c.Assert(batchgeterr, NotNil)
_, isFallBehind = errors.Cause(geterr2).(*tikverr.ErrGCTooEarly)
isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), tikverr.NewErrPDServerTimeout("start timestamp may fall behind safe point"))
s.NotNil(batchgeterr)
_, isFallBehind = errors.Cause(geterr2).(*error.ErrGCTooEarly)
isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), error.NewErrPDServerTimeout("start timestamp may fall behind safe point"))
isBehind = isFallBehind || isMayFallBehind
c.Assert(isBehind, IsTrue)
s.True(isBehind)
}

View File

@ -34,76 +34,80 @@ package tikv_test
import (
"context"
"testing"
. "github.com/pingcap/check"
"github.com/stretchr/testify/suite"
"github.com/tikv/client-go/v2/tikv"
)
type testScanMockSuite struct {
func TestScanMock(t *testing.T) {
suite.Run(t, new(testScanMockSuite))
}
var _ = SerialSuites(&testScanMockSuite{})
type testScanMockSuite struct {
suite.Suite
}
func (s *testScanMockSuite) TestScanMultipleRegions(c *C) {
store := tikv.StoreProbe{KVStore: NewTestStore(c)}
func (s *testScanMockSuite) TestScanMultipleRegions() {
store := tikv.StoreProbe{KVStore: NewTestStoreT(s.T())}
defer store.Close()
txn, err := store.Begin()
c.Assert(err, IsNil)
s.Nil(err)
for ch := byte('a'); ch <= byte('z'); ch++ {
err = txn.Set([]byte{ch}, []byte{ch})
c.Assert(err, IsNil)
s.Nil(err)
}
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
s.Nil(err)
txn, err = store.Begin()
c.Assert(err, IsNil)
s.Nil(err)
scanner, err := txn.NewScanner([]byte("a"), nil, 10, false)
c.Assert(err, IsNil)
s.Nil(err)
for ch := byte('a'); ch <= byte('z'); ch++ {
c.Assert([]byte{ch}, BytesEquals, scanner.Key())
c.Assert(scanner.Next(), IsNil)
s.Equal([]byte{ch}, scanner.Key())
s.Nil(scanner.Next())
}
c.Assert(scanner.Valid(), IsFalse)
s.False(scanner.Valid())
scanner, err = txn.NewScanner([]byte("a"), []byte("i"), 10, false)
c.Assert(err, IsNil)
s.Nil(err)
for ch := byte('a'); ch <= byte('h'); ch++ {
c.Assert([]byte{ch}, BytesEquals, scanner.Key())
c.Assert(scanner.Next(), IsNil)
s.Equal([]byte{ch}, scanner.Key())
s.Nil(scanner.Next())
}
c.Assert(scanner.Valid(), IsFalse)
s.False(scanner.Valid())
}
func (s *testScanMockSuite) TestReverseScan(c *C) {
store := tikv.StoreProbe{KVStore: NewTestStore(c)}
func (s *testScanMockSuite) TestReverseScan() {
store := tikv.StoreProbe{KVStore: NewTestStoreT(s.T())}
defer store.Close()
txn, err := store.Begin()
c.Assert(err, IsNil)
s.Nil(err)
for ch := byte('a'); ch <= byte('z'); ch++ {
err = txn.Set([]byte{ch}, []byte{ch})
c.Assert(err, IsNil)
s.Nil(err)
}
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
s.Nil(err)
txn, err = store.Begin()
c.Assert(err, IsNil)
s.Nil(err)
scanner, err := txn.NewScanner(nil, []byte("z"), 10, true)
c.Assert(err, IsNil)
s.Nil(err)
for ch := byte('y'); ch >= byte('a'); ch-- {
c.Assert(string([]byte{ch}), Equals, string(scanner.Key()))
c.Assert(scanner.Next(), IsNil)
s.Equal(string([]byte{ch}), string(scanner.Key()))
s.Nil(scanner.Next())
}
c.Assert(scanner.Valid(), IsFalse)
s.False(scanner.Valid())
scanner, err = txn.NewScanner([]byte("a"), []byte("i"), 10, true)
c.Assert(err, IsNil)
s.Nil(err)
for ch := byte('h'); ch >= byte('a'); ch-- {
c.Assert(string([]byte{ch}), Equals, string(scanner.Key()))
c.Assert(scanner.Next(), IsNil)
s.Equal(string([]byte{ch}), string(scanner.Key()))
s.Nil(scanner.Next())
}
c.Assert(scanner.Valid(), IsFalse)
s.False(scanner.Valid())
}

View File

@ -36,8 +36,9 @@ import (
"bytes"
"context"
"fmt"
"testing"
. "github.com/pingcap/check"
"github.com/stretchr/testify/suite"
"github.com/tikv/client-go/v2/kv"
"github.com/tikv/client-go/v2/logutil"
"github.com/tikv/client-go/v2/tikv"
@ -48,43 +49,46 @@ import (
var scanBatchSize = tikv.ConfigProbe{}.GetScanBatchSize()
func TestScan(t *testing.T) {
suite.Run(t, new(testScanSuite))
}
type testScanSuite struct {
suite.Suite
store *tikv.KVStore
recordPrefix []byte
rowNums []int
ctx context.Context
}
var _ = SerialSuites(&testScanSuite{})
func (s *testScanSuite) SetUpSuite(c *C) {
s.store = NewTestStore(c)
func (s *testScanSuite) SetupSuite() {
s.store = NewTestStoreT(s.T())
s.recordPrefix = []byte("prefix")
s.rowNums = append(s.rowNums, 1, scanBatchSize, scanBatchSize+1, scanBatchSize*3)
// Avoid using async commit logic.
s.ctx = context.WithValue(context.Background(), util.SessionID, uint64(0))
}
func (s *testScanSuite) TearDownSuite(c *C) {
txn := s.beginTxn(c)
func (s *testScanSuite) TearDownSuite() {
txn := s.beginTxn()
scanner, err := txn.Iter(s.recordPrefix, nil)
c.Assert(err, IsNil)
c.Assert(scanner, NotNil)
s.Require().Nil(err)
s.Require().NotNil(scanner)
for scanner.Valid() {
k := scanner.Key()
err = txn.Delete(k)
c.Assert(err, IsNil)
s.Require().Nil(err)
scanner.Next()
}
err = txn.Commit(s.ctx)
c.Assert(err, IsNil)
s.Require().Nil(err)
err = s.store.Close()
c.Assert(err, IsNil)
s.Require().Nil(err)
}
func (s *testScanSuite) beginTxn(c *C) *tikv.KVTxn {
func (s *testScanSuite) beginTxn() *tikv.KVTxn {
txn, err := s.store.Begin()
c.Assert(err, IsNil)
s.Require().Nil(err)
return txn
}
@ -99,8 +103,8 @@ func (s *testScanSuite) makeValue(i int) []byte {
return []byte(fmt.Sprintf("%d", i))
}
func (s *testScanSuite) TestScan(c *C) {
check := func(c *C, scan unionstore.Iterator, rowNum int, keyOnly bool) {
func (s *testScanSuite) TestScan() {
check := func(scan unionstore.Iterator, rowNum int, keyOnly bool) {
for i := 0; i < rowNum; i++ {
k := scan.Key()
expectedKey := s.makeKey(i)
@ -113,10 +117,10 @@ func (s *testScanSuite) TestScan(c *C) {
zap.String("expected", kv.StrKey(expectedKey)),
zap.Bool("keyOnly", keyOnly))
}
c.Assert(k, BytesEquals, expectedKey)
s.Equal(k, expectedKey)
if !keyOnly {
v := scan.Value()
c.Assert(v, BytesEquals, s.makeValue(i))
s.Equal(v, s.makeValue(i))
}
// Because newScan return first item without calling scan.Next() just like go-hbase,
// for-loop count will decrease 1.
@ -125,61 +129,61 @@ func (s *testScanSuite) TestScan(c *C) {
}
}
scan.Next()
c.Assert(scan.Valid(), IsFalse)
s.False(scan.Valid())
}
for _, rowNum := range s.rowNums {
txn := s.beginTxn(c)
txn := s.beginTxn()
for i := 0; i < rowNum; i++ {
err := txn.Set(s.makeKey(i), s.makeValue(i))
c.Assert(err, IsNil)
s.Nil(err)
}
err := txn.Commit(s.ctx)
c.Assert(err, IsNil)
s.Nil(err)
mockTableID := int64(999)
if rowNum > 123 {
_, err = s.store.SplitRegions(s.ctx, [][]byte{s.makeKey(123)}, false, &mockTableID)
c.Assert(err, IsNil)
s.Nil(err)
}
if rowNum > 456 {
_, err = s.store.SplitRegions(s.ctx, [][]byte{s.makeKey(456)}, false, &mockTableID)
c.Assert(err, IsNil)
s.Nil(err)
}
txn2 := s.beginTxn(c)
txn2 := s.beginTxn()
val, err := txn2.Get(context.TODO(), s.makeKey(0))
c.Assert(err, IsNil)
c.Assert(val, BytesEquals, s.makeValue(0))
s.Nil(err)
s.Equal(val, s.makeValue(0))
// Test scan without upperBound
scan, err := txn2.Iter(s.recordPrefix, nil)
c.Assert(err, IsNil)
check(c, scan, rowNum, false)
s.Nil(err)
check(scan, rowNum, false)
// Test scan with upperBound
upperBound := rowNum / 2
scan, err = txn2.Iter(s.recordPrefix, s.makeKey(upperBound))
c.Assert(err, IsNil)
check(c, scan, upperBound, false)
s.Nil(err)
check(scan, upperBound, false)
txn3 := s.beginTxn(c)
txn3 := s.beginTxn()
txn3.GetSnapshot().SetKeyOnly(true)
// Test scan without upper bound
scan, err = txn3.Iter(s.recordPrefix, nil)
c.Assert(err, IsNil)
check(c, scan, rowNum, true)
s.Nil(err)
check(scan, rowNum, true)
// test scan with upper bound
scan, err = txn3.Iter(s.recordPrefix, s.makeKey(upperBound))
c.Assert(err, IsNil)
check(c, scan, upperBound, true)
s.Nil(err)
check(scan, upperBound, true)
// Restore KeyOnly to false
txn3.GetSnapshot().SetKeyOnly(false)
scan, err = txn3.Iter(s.recordPrefix, nil)
c.Assert(err, IsNil)
check(c, scan, rowNum, true)
s.Nil(err)
check(scan, rowNum, true)
// test scan with upper bound
scan, err = txn3.Iter(s.recordPrefix, s.makeKey(upperBound))
c.Assert(err, IsNil)
check(c, scan, upperBound, true)
s.Nil(err)
check(scan, upperBound, true)
}
}