ticlient_test: replace pingcap/check with testify (#151)

This commit is contained in:
disksing 2021-06-23 17:16:37 +08:00 committed by GitHub
parent e1c2e9cab5
commit 91f6353565
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 157 additions and 155 deletions

View File

@ -3,6 +3,7 @@ module integration_tests
go 1.16
require (
github.com/ninedraft/israce v0.0.3
github.com/pingcap/check v0.0.0-20200212061837-5e12011dc712
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3
github.com/pingcap/failpoint v0.0.0-20210316064728-7acb0f0a3dfd

View File

@ -372,6 +372,8 @@ github.com/ngaut/pools v0.0.0-20180318154953-b7bc8c42aac7/go.mod h1:iWMfgwqYW+e8
github.com/ngaut/sync2 v0.0.0-20141008032647-7a24ed77b2ef h1:K0Fn+DoFqNqktdZtdV3bPQ/0cuYh2H4rkg0tytX/07k=
github.com/ngaut/sync2 v0.0.0-20141008032647-7a24ed77b2ef/go.mod h1:7WjlapSfwQyo6LNmIvEWzsW1hbBQfpUO4JWnuQRmva8=
github.com/nicksnyder/go-i18n v1.10.0/go.mod h1:HrK7VCrbOvQoUAQ7Vpy7i87N7JZZZ7R2xBGjv0j365Q=
github.com/ninedraft/israce v0.0.3 h1:F/Y1u6OlvgE75Syv1WbBatyg3CjGCdxLojLE7ydv2yE=
github.com/ninedraft/israce v0.0.3/go.mod h1:4L1ITFl340650ZmexVbUcBwG18ozlWiMe47pltZAmn4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/oleiade/reflections v1.0.1/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60=
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=

View File

@ -1,112 +0,0 @@
// Copyright 2021 TiKV Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
// NOTE: The code in this file is based on code from the
// TiDB project, licensed under the Apache License v 2.0
//
// https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/tests/ticlient_slow_test.go
//
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !race
package tikv_test
import (
"context"
. "github.com/pingcap/check"
"github.com/tikv/client-go/v2/kv"
"github.com/tikv/client-go/v2/mockstore"
"github.com/tikv/client-go/v2/tikv"
)
func (s *testTiclientSuite) TestSplitRegionIn2PC(c *C) {
if *mockstore.WithTiKV {
c.Skip("scatter will timeout with single node TiKV")
}
config := tikv.ConfigProbe{}
const preSplitThresholdInTest = 500
old := config.LoadPreSplitDetectThreshold()
defer config.StorePreSplitDetectThreshold(old)
config.StorePreSplitDetectThreshold(preSplitThresholdInTest)
old = config.LoadPreSplitSizeThreshold()
defer config.StorePreSplitSizeThreshold(old)
config.StorePreSplitSizeThreshold(5000)
bo := tikv.NewBackofferWithVars(context.Background(), 1, nil)
checkKeyRegion := func(bo *tikv.Backoffer, start, end []byte, checker Checker) {
// Check regions after split.
loc1, err := s.store.GetRegionCache().LocateKey(bo, start)
c.Assert(err, IsNil)
loc2, err := s.store.GetRegionCache().LocateKey(bo, end)
c.Assert(err, IsNil)
c.Assert(loc1.Region.GetID(), checker, loc2.Region.GetID())
}
mode := []string{"optimistic", "pessimistic"}
var (
startKey []byte
endKey []byte
)
ctx := context.Background()
for _, m := range mode {
if m == "optimistic" {
startKey = encodeKey(s.prefix, s08d("key", 0))
endKey = encodeKey(s.prefix, s08d("key", preSplitThresholdInTest))
} else {
startKey = encodeKey(s.prefix, s08d("pkey", 0))
endKey = encodeKey(s.prefix, s08d("pkey", preSplitThresholdInTest))
}
// Check before test.
checkKeyRegion(bo, startKey, endKey, Equals)
txn := s.beginTxn(c)
if m == "pessimistic" {
txn.SetPessimistic(true)
lockCtx := &kv.LockCtx{}
lockCtx.ForUpdateTS = txn.StartTS()
keys := make([][]byte, 0, preSplitThresholdInTest)
for i := 0; i < preSplitThresholdInTest; i++ {
keys = append(keys, encodeKey(s.prefix, s08d("pkey", i)))
}
err := txn.LockKeys(ctx, lockCtx, keys...)
c.Assert(err, IsNil)
checkKeyRegion(bo, startKey, endKey, Not(Equals))
}
var err error
for i := 0; i < preSplitThresholdInTest; i++ {
if m == "optimistic" {
err = txn.Set(encodeKey(s.prefix, s08d("key", i)), valueBytes(i))
} else {
err = txn.Set(encodeKey(s.prefix, s08d("pkey", i)), valueBytes(i))
}
c.Assert(err, IsNil)
}
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
// Check region split after test.
checkKeyRegion(bo, startKey, endKey, Not(Equals))
}
}

View File

@ -35,113 +35,191 @@ package tikv_test
import (
"context"
"fmt"
"testing"
"time"
. "github.com/pingcap/check"
"github.com/ninedraft/israce"
"github.com/pingcap/tidb/kv"
"github.com/stretchr/testify/suite"
tikvstore "github.com/tikv/client-go/v2/kv"
"github.com/tikv/client-go/v2/mockstore"
"github.com/tikv/client-go/v2/tikv"
)
func TestTiclient(t *testing.T) {
suite.Run(t, new(testTiclientSuite))
}
type testTiclientSuite struct {
suite.Suite
store *tikv.KVStore
// prefix is prefix of each key in this test. It is used for table isolation,
// or it may pollute other data.
prefix string
}
var _ = SerialSuites(&testTiclientSuite{})
func (s *testTiclientSuite) SetUpSuite(c *C) {
s.store = NewTestStore(c)
func (s *testTiclientSuite) SetupSuite() {
s.store = NewTestStoreT(s.T())
s.prefix = fmt.Sprintf("ticlient_%d", time.Now().Unix())
}
func (s *testTiclientSuite) TearDownSuite(c *C) {
func (s *testTiclientSuite) TearDownSuite() {
// Clean all data, or it may pollute other data.
txn := s.beginTxn(c)
txn := s.beginTxn()
scanner, err := txn.Iter(encodeKey(s.prefix, ""), 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(context.Background())
c.Assert(err, IsNil)
s.Require().Nil(err)
err = s.store.Close()
c.Assert(err, IsNil)
s.Require().Nil(err)
}
func (s *testTiclientSuite) beginTxn(c *C) *tikv.KVTxn {
func (s *testTiclientSuite) beginTxn() *tikv.KVTxn {
txn, err := s.store.Begin()
c.Assert(err, IsNil)
s.Require().Nil(err)
return txn
}
func (s *testTiclientSuite) TestSingleKey(c *C) {
txn := s.beginTxn(c)
func (s *testTiclientSuite) TestSingleKey() {
txn := s.beginTxn()
err := txn.Set(encodeKey(s.prefix, "key"), []byte("value"))
c.Assert(err, IsNil)
s.Nil(err)
err = txn.LockKeys(context.Background(), new(tikvstore.LockCtx), encodeKey(s.prefix, "key"))
c.Assert(err, IsNil)
s.Nil(err)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
s.Nil(err)
txn = s.beginTxn(c)
txn = s.beginTxn()
val, err := txn.Get(context.TODO(), encodeKey(s.prefix, "key"))
c.Assert(err, IsNil)
c.Assert(val, BytesEquals, []byte("value"))
s.Nil(err)
s.Equal(val, []byte("value"))
txn = s.beginTxn(c)
txn = s.beginTxn()
err = txn.Delete(encodeKey(s.prefix, "key"))
c.Assert(err, IsNil)
s.Nil(err)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
s.Nil(err)
}
func (s *testTiclientSuite) TestMultiKeys(c *C) {
func (s *testTiclientSuite) TestMultiKeys() {
const keyNum = 100
txn := s.beginTxn(c)
txn := s.beginTxn()
for i := 0; i < keyNum; 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)
txn = s.beginTxn(c)
txn = s.beginTxn()
for i := 0; i < keyNum; i++ {
val, err1 := txn.Get(context.TODO(), encodeKey(s.prefix, s08d("key", i)))
c.Assert(err1, IsNil)
c.Assert(val, BytesEquals, valueBytes(i))
s.Nil(err1)
s.Equal(val, valueBytes(i))
}
txn = s.beginTxn(c)
txn = s.beginTxn()
for i := 0; i < keyNum; i++ {
err = txn.Delete(encodeKey(s.prefix, s08d("key", i)))
c.Assert(err, IsNil)
s.Nil(err)
}
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
s.Nil(err)
}
func (s *testTiclientSuite) TestNotExist(c *C) {
txn := s.beginTxn(c)
func (s *testTiclientSuite) TestNotExist() {
txn := s.beginTxn()
_, err := txn.Get(context.TODO(), encodeKey(s.prefix, "noSuchKey"))
c.Assert(err, NotNil)
s.NotNil(err)
}
func (s *testTiclientSuite) TestLargeRequest(c *C) {
func (s *testTiclientSuite) TestLargeRequest() {
largeValue := make([]byte, 9*1024*1024) // 9M value.
txn := s.beginTxn(c)
txn := s.beginTxn()
txn.GetUnionStore().SetEntrySizeLimit(1024*1024, 100*1024*1024)
err := txn.Set([]byte("key"), largeValue)
c.Assert(err, NotNil)
s.NotNil(err)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
c.Assert(kv.IsTxnRetryableError(err), IsFalse)
s.Nil(err)
s.False(kv.IsTxnRetryableError(err))
}
func (s *testTiclientSuite) TestSplitRegionIn2PC() {
if *mockstore.WithTiKV {
s.T().Skip("scatter will timeout with single node TiKV")
}
if israce.Race {
s.T().Skip("skip slow test when race is enabled")
}
config := tikv.ConfigProbe{}
const preSplitThresholdInTest = 500
old := config.LoadPreSplitDetectThreshold()
defer config.StorePreSplitDetectThreshold(old)
config.StorePreSplitDetectThreshold(preSplitThresholdInTest)
old = config.LoadPreSplitSizeThreshold()
defer config.StorePreSplitSizeThreshold(old)
config.StorePreSplitSizeThreshold(5000)
bo := tikv.NewBackofferWithVars(context.Background(), 1, nil)
checkKeyRegion := func(bo *tikv.Backoffer, start, end []byte, eq bool) {
// Check regions after split.
loc1, err := s.store.GetRegionCache().LocateKey(bo, start)
s.Nil(err)
loc2, err := s.store.GetRegionCache().LocateKey(bo, end)
s.Nil(err)
s.Equal(loc1.Region.GetID() == loc2.Region.GetID(), eq)
}
mode := []string{"optimistic", "pessimistic"}
var (
startKey []byte
endKey []byte
)
ctx := context.Background()
for _, m := range mode {
if m == "optimistic" {
startKey = encodeKey(s.prefix, s08d("key", 0))
endKey = encodeKey(s.prefix, s08d("key", preSplitThresholdInTest))
} else {
startKey = encodeKey(s.prefix, s08d("pkey", 0))
endKey = encodeKey(s.prefix, s08d("pkey", preSplitThresholdInTest))
}
// Check before test.
checkKeyRegion(bo, startKey, endKey, true)
txn := s.beginTxn()
if m == "pessimistic" {
txn.SetPessimistic(true)
lockCtx := &kv.LockCtx{}
lockCtx.ForUpdateTS = txn.StartTS()
keys := make([][]byte, 0, preSplitThresholdInTest)
for i := 0; i < preSplitThresholdInTest; i++ {
keys = append(keys, encodeKey(s.prefix, s08d("pkey", i)))
}
err := txn.LockKeys(ctx, lockCtx, keys...)
s.Nil(err)
checkKeyRegion(bo, startKey, endKey, false)
}
var err error
for i := 0; i < preSplitThresholdInTest; i++ {
if m == "optimistic" {
err = txn.Set(encodeKey(s.prefix, s08d("key", i)), valueBytes(i))
} else {
err = txn.Set(encodeKey(s.prefix, s08d("pkey", i)), valueBytes(i))
}
s.Nil(err)
}
err = txn.Commit(context.Background())
s.Nil(err)
// Check region split after test.
checkKeyRegion(bo, startKey, endKey, false)
}
}

View File

@ -37,6 +37,7 @@ import (
"flag"
"fmt"
"strings"
"testing"
"unsafe"
. "github.com/pingcap/check"
@ -44,6 +45,7 @@ import (
"github.com/pingcap/tidb/kv"
txndriver "github.com/pingcap/tidb/store/driver/txn"
"github.com/pingcap/tidb/store/mockstore/unistore"
"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/config"
"github.com/tikv/client-go/v2/mockstore"
"github.com/tikv/client-go/v2/tikv"
@ -55,7 +57,38 @@ var (
pdAddrs = flag.String("pd-addrs", "127.0.0.1:2379", "pd addrs")
)
// NewTestStoreT creates a KVStore for testing purpose.
// It accepts `*testing.T` instead of `check.C`.
func NewTestStoreT(t *testing.T) *tikv.KVStore {
if !flag.Parsed() {
flag.Parse()
}
if *mockstore.WithTiKV {
addrs := strings.Split(*pdAddrs, ",")
pdClient, err := pd.NewClient(addrs, pd.SecurityOption{})
require.Nil(t, err)
var securityConfig config.Security
tlsConfig, err := securityConfig.ToTLSConfig()
require.Nil(t, err)
spKV, err := tikv.NewEtcdSafePointKV(addrs, tlsConfig)
require.Nil(t, err)
store, err := tikv.NewKVStore("test-store", &tikv.CodecPDClient{Client: pdClient}, spKV, tikv.NewRPCClient(securityConfig))
require.Nil(t, err)
err = clearStorage(store)
require.Nil(t, err)
return store
}
client, pdClient, cluster, err := unistore.New("")
require.Nil(t, err)
unistore.BootstrapWithSingleStore(cluster)
store, err := tikv.NewTestTiKVStore(client, pdClient, nil, nil, 0)
require.Nil(t, err)
return store
}
// NewTestStore creates a KVStore for testing purpose.
// TODO: remove it after we get rid of pingcap/check.
func NewTestStore(c *C) *tikv.KVStore {
if !flag.Parsed() {
flag.Parse()