store/tikv: move ticlient_test to /tests (#23731)

This commit is contained in:
disksing
2021-04-06 19:32:31 +08:00
committed by GitHub
parent 0402268a16
commit 8a3cb769c6
3 changed files with 144 additions and 121 deletions

View File

@ -13,14 +13,14 @@
// +build !race
package tikv
package tikv_test
import (
"context"
"sync/atomic"
. "github.com/pingcap/check"
tidbkv "github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/kv"
)
@ -28,23 +28,24 @@ func (s *testTiclientSuite) TestSplitRegionIn2PC(c *C) {
if *WithTiKV {
c.Skip("scatter will timeout with single node TiKV")
}
config := tikv.ConfigProbe{}
const preSplitThresholdInTest = 500
old := atomic.LoadUint32(&preSplitDetectThreshold)
defer atomic.StoreUint32(&preSplitDetectThreshold, old)
atomic.StoreUint32(&preSplitDetectThreshold, preSplitThresholdInTest)
old := config.LoadPreSplitDetectThreshold()
defer config.StorePreSplitDetectThreshold(old)
config.StorePreSplitDetectThreshold(preSplitThresholdInTest)
old = atomic.LoadUint32(&preSplitSizeThreshold)
defer atomic.StoreUint32(&preSplitSizeThreshold, old)
atomic.StoreUint32(&preSplitSizeThreshold, 5000)
old = config.LoadPreSplitSizeThreshold()
defer config.StorePreSplitSizeThreshold(old)
config.StorePreSplitSizeThreshold(5000)
bo := NewBackofferWithVars(context.Background(), 1, nil)
checkKeyRegion := func(bo *Backoffer, start, end []byte, checker Checker) {
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.regionCache.LocateKey(bo, start)
loc1, err := s.store.GetRegionCache().LocateKey(bo, start)
c.Assert(err, IsNil)
loc2, err := s.store.regionCache.LocateKey(bo, end)
loc2, err := s.store.GetRegionCache().LocateKey(bo, end)
c.Assert(err, IsNil)
c.Assert(loc1.Region.id, checker, loc2.Region.id)
c.Assert(loc1.Region.GetID(), checker, loc2.Region.GetID())
}
mode := []string{"optimistic", "pessimistic"}
var (
@ -66,7 +67,7 @@ func (s *testTiclientSuite) TestSplitRegionIn2PC(c *C) {
if m == "pessimistic" {
txn.SetOption(kv.Pessimistic, true)
lockCtx := &tidbkv.LockCtx{}
lockCtx.ForUpdateTS = txn.startTS
lockCtx.ForUpdateTS = txn.StartTS()
keys := make([]tidbkv.Key, 0, preSplitThresholdInTest)
for i := 0; i < preSplitThresholdInTest; i++ {
keys = append(keys, encodeKey(s.prefix, s08d("pkey", i)))

View File

@ -0,0 +1,129 @@
// Copyright 2021 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.
package tikv_test
import (
"context"
"fmt"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/tikv"
)
type testTiclientSuite struct {
OneByOneSuite
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 _ = Suite(&testTiclientSuite{})
func (s *testTiclientSuite) SetUpSuite(c *C) {
s.OneByOneSuite.SetUpSuite(c)
s.store = NewTestStore(c)
s.prefix = fmt.Sprintf("ticlient_%d", time.Now().Unix())
}
func (s *testTiclientSuite) TearDownSuite(c *C) {
// Clean all data, or it may pollute other data.
txn := s.beginTxn(c)
scanner, err := txn.Iter(encodeKey(s.prefix, ""), nil)
c.Assert(err, IsNil)
c.Assert(scanner, NotNil)
for scanner.Valid() {
k := scanner.Key()
err = txn.Delete(k)
c.Assert(err, IsNil)
scanner.Next()
}
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
err = s.store.Close()
c.Assert(err, IsNil)
s.OneByOneSuite.TearDownSuite(c)
}
func (s *testTiclientSuite) beginTxn(c *C) *tikv.KVTxn {
txn, err := s.store.Begin()
c.Assert(err, IsNil)
return txn
}
func (s *testTiclientSuite) TestSingleKey(c *C) {
txn := s.beginTxn(c)
err := txn.Set(encodeKey(s.prefix, "key"), []byte("value"))
c.Assert(err, IsNil)
err = txn.LockKeys(context.Background(), new(kv.LockCtx), encodeKey(s.prefix, "key"))
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
txn = s.beginTxn(c)
val, err := txn.Get(context.TODO(), encodeKey(s.prefix, "key"))
c.Assert(err, IsNil)
c.Assert(val, BytesEquals, []byte("value"))
txn = s.beginTxn(c)
err = txn.Delete(encodeKey(s.prefix, "key"))
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
}
func (s *testTiclientSuite) TestMultiKeys(c *C) {
const keyNum = 100
txn := s.beginTxn(c)
for i := 0; i < keyNum; i++ {
err := txn.Set(encodeKey(s.prefix, s08d("key", i)), valueBytes(i))
c.Assert(err, IsNil)
}
err := txn.Commit(context.Background())
c.Assert(err, IsNil)
txn = s.beginTxn(c)
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))
}
txn = s.beginTxn(c)
for i := 0; i < keyNum; i++ {
err = txn.Delete(encodeKey(s.prefix, s08d("key", i)))
c.Assert(err, IsNil)
}
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
}
func (s *testTiclientSuite) TestNotExist(c *C) {
txn := s.beginTxn(c)
_, err := txn.Get(context.TODO(), encodeKey(s.prefix, "noSuchKey"))
c.Assert(err, NotNil)
}
func (s *testTiclientSuite) TestLargeRequest(c *C) {
largeValue := make([]byte, 9*1024*1024) // 9M value.
txn := s.beginTxn(c)
err := txn.Set([]byte("key"), largeValue)
c.Assert(err, NotNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
c.Assert(kv.IsTxnRetryableError(err), IsFalse)
}

View File

@ -19,11 +19,9 @@ import (
"fmt"
"strings"
"sync"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockstore/unistore"
"github.com/pingcap/tidb/store/tikv/config"
"github.com/pingcap/tidb/store/tikv/util/codec"
@ -83,111 +81,6 @@ func clearStorage(store *KVStore) error {
return txn.Commit(context.Background())
}
type testTiclientSuite struct {
OneByOneSuite
store *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 _ = Suite(&testTiclientSuite{})
func (s *testTiclientSuite) SetUpSuite(c *C) {
s.OneByOneSuite.SetUpSuite(c)
s.store = NewTestStore(c)
s.prefix = fmt.Sprintf("ticlient_%d", time.Now().Unix())
}
func (s *testTiclientSuite) TearDownSuite(c *C) {
// Clean all data, or it may pollute other data.
txn := s.beginTxn(c)
scanner, err := txn.Iter(encodeKey(s.prefix, ""), nil)
c.Assert(err, IsNil)
c.Assert(scanner, NotNil)
for scanner.Valid() {
k := scanner.Key()
err = txn.Delete(k)
c.Assert(err, IsNil)
scanner.Next()
}
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
err = s.store.Close()
c.Assert(err, IsNil)
s.OneByOneSuite.TearDownSuite(c)
}
func (s *testTiclientSuite) beginTxn(c *C) *KVTxn {
txn, err := s.store.Begin()
c.Assert(err, IsNil)
return txn
}
func (s *testTiclientSuite) TestSingleKey(c *C) {
txn := s.beginTxn(c)
err := txn.Set(encodeKey(s.prefix, "key"), []byte("value"))
c.Assert(err, IsNil)
err = txn.LockKeys(context.Background(), new(kv.LockCtx), encodeKey(s.prefix, "key"))
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
txn = s.beginTxn(c)
val, err := txn.Get(context.TODO(), encodeKey(s.prefix, "key"))
c.Assert(err, IsNil)
c.Assert(val, BytesEquals, []byte("value"))
txn = s.beginTxn(c)
err = txn.Delete(encodeKey(s.prefix, "key"))
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
}
func (s *testTiclientSuite) TestMultiKeys(c *C) {
const keyNum = 100
txn := s.beginTxn(c)
for i := 0; i < keyNum; i++ {
err := txn.Set(encodeKey(s.prefix, s08d("key", i)), valueBytes(i))
c.Assert(err, IsNil)
}
err := txn.Commit(context.Background())
c.Assert(err, IsNil)
txn = s.beginTxn(c)
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))
}
txn = s.beginTxn(c)
for i := 0; i < keyNum; i++ {
err = txn.Delete(encodeKey(s.prefix, s08d("key", i)))
c.Assert(err, IsNil)
}
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
}
func (s *testTiclientSuite) TestNotExist(c *C) {
txn := s.beginTxn(c)
_, err := txn.Get(context.TODO(), encodeKey(s.prefix, "noSuchKey"))
c.Assert(err, NotNil)
}
func (s *testTiclientSuite) TestLargeRequest(c *C) {
largeValue := make([]byte, 9*1024*1024) // 9M value.
txn := s.beginTxn(c)
err := txn.Set([]byte("key"), largeValue)
c.Assert(err, NotNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
c.Assert(kv.IsTxnRetryableError(err), IsFalse)
}
func encodeKey(prefix, s string) []byte {
return codec.EncodeBytes(nil, []byte(fmt.Sprintf("%s_%s", prefix, s)))
}