Repository: tikv/client-go
Pull request #209 (2021-07-02 17:39:23)

move mocktikv to internal package
Signed-off-by: disksing <i@disksing.com>


Patch:
diff --git a/mockstore/cluster/cluster.go b/internal/mockstore/cluster/cluster.go
similarity index 100%
rename from mockstore/cluster/cluster.go
rename to internal/mockstore/cluster/cluster.go
diff --git a/mockstore/deadlock/deadlock.go b/internal/mockstore/deadlock/deadlock.go
similarity index 100%
rename from mockstore/deadlock/deadlock.go
rename to internal/mockstore/deadlock/deadlock.go
diff --git a/mockstore/deadlock/deadlock_test.go b/internal/mockstore/deadlock/deadlock_test.go
similarity index 100%
rename from mockstore/deadlock/deadlock_test.go
rename to internal/mockstore/deadlock/deadlock_test.go
diff --git a/mockstore/deadlock/main_test.go b/internal/mockstore/deadlock/main_test.go
similarity index 100%
rename from mockstore/deadlock/main_test.go
rename to internal/mockstore/deadlock/main_test.go
diff --git a/mockstore/mocktikv/cluster.go b/internal/mockstore/mocktikv/cluster.go
similarity index 100%
rename from mockstore/mocktikv/cluster.go
rename to internal/mockstore/mocktikv/cluster.go
diff --git a/mockstore/mocktikv/cluster_manipulate.go b/internal/mockstore/mocktikv/cluster_manipulate.go
similarity index 100%
rename from mockstore/mocktikv/cluster_manipulate.go
rename to internal/mockstore/mocktikv/cluster_manipulate.go
diff --git a/mockstore/mocktikv/errors.go b/internal/mockstore/mocktikv/errors.go
similarity index 100%
rename from mockstore/mocktikv/errors.go
rename to internal/mockstore/mocktikv/errors.go
diff --git a/mockstore/mocktikv/main_test.go b/internal/mockstore/mocktikv/main_test.go
similarity index 100%
rename from mockstore/mocktikv/main_test.go
rename to internal/mockstore/mocktikv/main_test.go
diff --git a/mockstore/mocktikv/marshal_test.go b/internal/mockstore/mocktikv/marshal_test.go
similarity index 100%
rename from mockstore/mocktikv/marshal_test.go
rename to internal/mockstore/mocktikv/marshal_test.go
diff --git a/mockstore/mocktikv/mock.go b/internal/mockstore/mocktikv/mock.go
similarity index 100%
rename from mockstore/mocktikv/mock.go
rename to internal/mockstore/mocktikv/mock.go
diff --git a/mockstore/mocktikv/mock_tikv_test.go b/internal/mockstore/mocktikv/mock_tikv_test.go
similarity index 100%
rename from mockstore/mocktikv/mock_tikv_test.go
rename to internal/mockstore/mocktikv/mock_tikv_test.go
diff --git a/mockstore/mocktikv/mvcc.go b/internal/mockstore/mocktikv/mvcc.go
similarity index 100%
rename from mockstore/mocktikv/mvcc.go
rename to internal/mockstore/mocktikv/mvcc.go
diff --git a/mockstore/mocktikv/mvcc_leveldb.go b/internal/mockstore/mocktikv/mvcc_leveldb.go
similarity index 99%
rename from mockstore/mocktikv/mvcc_leveldb.go
rename to internal/mockstore/mocktikv/mvcc_leveldb.go
index eba9de6..fed50be 100644
--- a/mockstore/mocktikv/mvcc_leveldb.go
+++ b/internal/mockstore/mocktikv/mvcc_leveldb.go
@@ -47,7 +47,7 @@ import (
 	"github.com/pingcap/kvproto/pkg/kvrpcpb"
 	"github.com/pingcap/parser/terror"
 	"github.com/tikv/client-go/v2/internal/logutil"
-	"github.com/tikv/client-go/v2/mockstore/deadlock"
+	"github.com/tikv/client-go/v2/internal/mockstore/deadlock"
 	"github.com/tikv/client-go/v2/oracle"
 	"github.com/tikv/client-go/v2/util/codec"
 	"go.uber.org/zap"
diff --git a/mockstore/mocktikv/mvcc_test.go b/internal/mockstore/mocktikv/mvcc_test.go
similarity index 100%
rename from mockstore/mocktikv/mvcc_test.go
rename to internal/mockstore/mocktikv/mvcc_test.go
diff --git a/mockstore/mocktikv/pd.go b/internal/mockstore/mocktikv/pd.go
similarity index 100%
rename from mockstore/mocktikv/pd.go
rename to internal/mockstore/mocktikv/pd.go
diff --git a/mockstore/mocktikv/rpc.go b/internal/mockstore/mocktikv/rpc.go
similarity index 100%
rename from mockstore/mocktikv/rpc.go
rename to internal/mockstore/mocktikv/rpc.go
diff --git a/mockstore/mocktikv/session.go b/internal/mockstore/mocktikv/session.go
similarity index 100%
rename from mockstore/mocktikv/session.go
rename to internal/mockstore/mocktikv/session.go
diff --git a/mockstore/mocktikv/utils.go b/internal/mockstore/mocktikv/utils.go
similarity index 100%
rename from mockstore/mocktikv/utils.go
rename to internal/mockstore/mocktikv/utils.go
diff --git a/mockstore/cluster/stub.go b/mockstore/cluster/stub.go
new file mode 100644
index 0000000..d93d01b
--- /dev/null
+++ b/mockstore/cluster/stub.go
@@ -0,0 +1,22 @@
+// 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.
+
+package cluster
+
+import "github.com/tikv/client-go/v2/internal/mockstore/cluster"
+
+// For backward campatibility.
+// TODO: remove it.
+
+// Cluster simulates a TiKV cluster.
+type Cluster = cluster.Cluster
diff --git a/mockstore/mocktikv/stub.go b/mockstore/mocktikv/stub.go
new file mode 100644
index 0000000..061f024
--- /dev/null
+++ b/mockstore/mocktikv/stub.go
@@ -0,0 +1,57 @@
+// 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.
+
+package mocktikv
+
+import "github.com/tikv/client-go/v2/internal/mockstore/mocktikv"
+
+// For backward campatibility.
+// TODO: remove it.
+
+// CoprRPCHandler is the interface to handle coprocessor RPC commands.
+type CoprRPCHandler = mocktikv.CoprRPCHandler
+
+// MVCCStore is a mvcc key-value storage.
+type MVCCStore = mocktikv.MVCCStore
+
+// MustNewMVCCStore creates a MVCCStore for testing.
+var MustNewMVCCStore = mocktikv.MustNewMVCCStore
+
+// Pair is a KV pair read from MvccStore or an error if any occurs.
+type Pair = mocktikv.Pair
+
+// Cluster simulates a TiKV cluster.
+type Cluster = mocktikv.Cluster
+
+// RPCClient sends kv RPC calls to mock cluster.
+type RPCClient = mocktikv.RPCClient
+
+// Session stores session scope rpc data.
+type Session = mocktikv.Session
+
+// NewTiKVAndPDClient creates a TiKV client and PD client from options.
+var NewTiKVAndPDClient = mocktikv.NewTiKVAndPDClient
+
+// BootstrapWithSingleStore initializes a Cluster with 1 Region and 1 Store.
+var BootstrapWithSingleStore = mocktikv.BootstrapWithSingleStore
+
+// BootstrapWithMultiStores initializes a Cluster with 1 Region and n Stores.
+var BootstrapWithMultiStores = mocktikv.BootstrapWithMultiStores
+
+// BootstrapWithMultiRegions initializes a Cluster with multiple Regions and 1
+// Store. The number of Regions will be len(splitKeys) + 1.
+var BootstrapWithMultiRegions = mocktikv.BootstrapWithMultiRegions
+
+// ErrLocked is returned when trying to Read/Write on a locked key. Client should
+// backoff or cleanup the lock then retry.
+type ErrLocked = mocktikv.ErrLocked


Test patch:
diff --git a/integration_tests/2pc_test.go b/integration_tests/2pc_test.go
index 17855d2..f2d8e3e 100644
--- a/integration_tests/2pc_test.go
+++ b/integration_tests/2pc_test.go
@@ -55,9 +55,8 @@ import (
 	"github.com/tikv/client-go/v2/config"
 	tikverr "github.com/tikv/client-go/v2/error"
 	"github.com/tikv/client-go/v2/kv"
-	"github.com/tikv/client-go/v2/mockstore/cluster"
-	"github.com/tikv/client-go/v2/mockstore/mocktikv"
 	"github.com/tikv/client-go/v2/oracle"
+	"github.com/tikv/client-go/v2/testutils"
 	"github.com/tikv/client-go/v2/tikv"
 	"github.com/tikv/client-go/v2/tikvrpc"
 )
@@ -73,7 +72,7 @@ func TestCommitter(t *testing.T) {
 
 type testCommitterSuite struct {
 	suite.Suite
-	cluster cluster.Cluster
+	cluster testutils.Cluster
 	store   tikv.StoreProbe
 }
 
@@ -89,13 +88,11 @@ func (s *testCommitterSuite) TearDownSuite() {
 }
 
 func (s *testCommitterSuite) SetupTest() {
-	mvccStore, err := mocktikv.NewMVCCLevelDB("")
+	client, cluster, pdClient, err := testutils.NewMockTiKV("", nil)
 	s.Require().Nil(err)
-	cluster := mocktikv.NewCluster(mvccStore)
-	mocktikv.BootstrapWithMultiRegions(cluster, []byte("a"), []byte("b"), []byte("c"))
+	testutils.BootstrapWithMultiRegions(cluster, []byte("a"), []byte("b"), []byte("c"))
 	s.cluster = cluster
-	client := mocktikv.NewRPCClient(cluster, mvccStore, nil)
-	pdCli := &tikv.CodecPDClient{Client: mocktikv.NewPDClient(cluster)}
+	pdCli := &tikv.CodecPDClient{Client: pdClient}
 	spkv := tikv.NewMockSafePointKV()
 	store, err := tikv.NewKVStore("mocktikv-store", pdCli, spkv, client)
 	store.EnableTxnLocalLatches(8096)
diff --git a/integration_tests/async_commit_test.go b/integration_tests/async_commit_test.go
index 50474e4..8b1ad56 100644
--- a/integration_tests/async_commit_test.go
+++ b/integration_tests/async_commit_test.go
@@ -47,8 +47,8 @@ import (
 	"github.com/stretchr/testify/suite"
 	tikverr "github.com/tikv/client-go/v2/error"
 	"github.com/tikv/client-go/v2/kv"
-	"github.com/tikv/client-go/v2/mockstore/cluster"
 	"github.com/tikv/client-go/v2/oracle"
+	"github.com/tikv/client-go/v2/testutils"
 	"github.com/tikv/client-go/v2/tikv"
 	"github.com/tikv/client-go/v2/tikvrpc"
 	"github.com/tikv/client-go/v2/util"
@@ -62,7 +62,7 @@ func TestAsyncCommit(t *testing.T) {
 // testAsyncCommitSuite and testAsyncCommitFailSuite.
 type testAsyncCommitCommon struct {
 	suite.Suite
-	cluster cluster.Cluster
+	cluster testutils.Cluster
 	store   *tikv.KVStore
 }
 
diff --git a/integration_tests/delete_range_test.go b/integration_tests/delete_range_test.go
index a233a0d..16bd429 100644
--- a/integration_tests/delete_range_test.go
+++ b/integration_tests/delete_range_test.go
@@ -41,8 +41,7 @@ import (
 
 	"github.com/pingcap/tidb/store/mockstore/mockcopr"
 	"github.com/stretchr/testify/suite"
-	"github.com/tikv/client-go/v2/mockstore/cluster"
-	"github.com/tikv/client-go/v2/mockstore/mocktikv"
+	"github.com/tikv/client-go/v2/testutils"
 	"github.com/tikv/client-go/v2/tikv"
 )
 
@@ -52,14 +51,14 @@ func TestDeleteRange(t *testing.T) {
 
 type testDeleteRangeSuite struct {
 	suite.Suite
-	cluster cluster.Cluster
+	cluster testutils.Cluster
 	store   *tikv.KVStore
 }
 
 func (s *testDeleteRangeSuite) SetupTest() {
-	client, cluster, pdClient, err := mocktikv.NewTiKVAndPDClient("", mockcopr.NewCoprRPCHandler())
+	client, cluster, pdClient, err := testutils.NewMockTiKV("", mockcopr.NewCoprRPCHandler())
 	s.Require().Nil(err)
-	mocktikv.BootstrapWithMultiRegions(cluster, []byte("b"), []byte("c"), []byte("d"))
+	testutils.BootstrapWithMultiRegions(cluster, []byte("b"), []byte("c"), []byte("d"))
 	s.cluster = cluster
 	store, err := tikv.NewTestTiKVStore(client, pdClient, nil, nil, 0)
 	s.Require().Nil(err)
diff --git a/integration_tests/range_task_test.go b/integration_tests/range_task_test.go
index 56b1860..2b8b786 100644
--- a/integration_tests/range_task_test.go
+++ b/integration_tests/range_task_test.go
@@ -42,8 +42,7 @@ import (
 	"github.com/pingcap/tidb/store/mockstore/mockcopr"
 	"github.com/stretchr/testify/suite"
 	"github.com/tikv/client-go/v2/kv"
-	"github.com/tikv/client-go/v2/mockstore/cluster"
-	"github.com/tikv/client-go/v2/mockstore/mocktikv"
+	"github.com/tikv/client-go/v2/testutils"
 	"github.com/tikv/client-go/v2/tikv"
 )
 
@@ -53,7 +52,7 @@ func TestRangeTask(t *testing.T) {
 
 type testRangeTaskSuite struct {
 	suite.Suite
-	cluster cluster.Cluster
+	cluster testutils.Cluster
 	store   *tikv.KVStore
 
 	testRanges     []kv.KeyRange
@@ -84,23 +83,14 @@ func (s *testRangeTaskSuite) SetupTest() {
 	}
 	allRegionRanges = append(allRegionRanges, makeRange("z", ""))
 
-	client, cluster, pdClient, err := mocktikv.NewTiKVAndPDClient("", mockcopr.NewCoprRPCHandler())
+	client, cluster, pdClient, err := testutils.NewMockTiKV("", mockcopr.NewCoprRPCHandler())
 	s.Require().Nil(err)
-	mocktikv.BootstrapWithMultiRegions(cluster, splitKeys...)
+	testutils.BootstrapWithMultiRegions(cluster, splitKeys...)
 	s.cluster = cluster
 
 	store, err := tikv.NewTestTiKVStore(client, pdClient, nil, nil, 0)
 	s.Require().Nil(err)
 
-	// TODO: make this possible
-	// store, err := mockstore.NewMockStore(
-	// 	mockstore.WithStoreType(mockstore.MockTiKV),
-	// 	mockstore.WithClusterInspector(func(c cluster.Cluster) {
-	// 		mockstore.BootstrapWithMultiRegions(splitKeys...)
-	// 		s.cluster = c
-	// 	}),
-	// )
-	// s.Nil(err)
 	s.store = store
 
 	s.testRanges = []kv.KeyRange{
diff --git a/integration_tests/rawkv_test.go b/integration_tests/rawkv_test.go
index 6157fbb..731edde 100644
--- a/integration_tests/rawkv_test.go
+++ b/integration_tests/rawkv_test.go
@@ -40,7 +40,7 @@ import (
 
 	"github.com/pingcap/tidb/store/mockstore/unistore"
 	"github.com/stretchr/testify/suite"
-	"github.com/tikv/client-go/v2/mockstore/cluster"
+	"github.com/tikv/client-go/v2/testutils"
 	"github.com/tikv/client-go/v2/tikv"
 )
 
@@ -50,7 +50,7 @@ func TestRawKV(t *testing.T) {
 
 type testRawKVSuite struct {
 	suite.Suite
-	cluster cluster.Cluster
+	cluster testutils.Cluster
 	client  tikv.RawKVClientProbe
 	bo      *tikv.Backoffer
 }
diff --git a/integration_tests/split_test.go b/integration_tests/split_test.go
index c3131de..e7bc143 100644
--- a/integration_tests/split_test.go
+++ b/integration_tests/split_test.go
@@ -42,8 +42,7 @@ import (
 	"github.com/pingcap/kvproto/pkg/pdpb"
 	"github.com/pingcap/tidb/store/mockstore/mockcopr"
 	"github.com/stretchr/testify/suite"
-	"github.com/tikv/client-go/v2/mockstore/cluster"
-	"github.com/tikv/client-go/v2/mockstore/mocktikv"
+	"github.com/tikv/client-go/v2/testutils"
 	"github.com/tikv/client-go/v2/tikv"
 	pd "github.com/tikv/pd/client"
 )
@@ -54,27 +53,19 @@ func TestSplit(t *testing.T) {
 
 type testSplitSuite struct {
 	suite.Suite
-	cluster cluster.Cluster
+	cluster testutils.Cluster
 	store   tikv.StoreProbe
 	bo      *tikv.Backoffer
 }
 
 func (s *testSplitSuite) SetupTest() {
-	client, cluster, pdClient, err := mocktikv.NewTiKVAndPDClient("", mockcopr.NewCoprRPCHandler())
+	client, cluster, pdClient, err := testutils.NewMockTiKV("", mockcopr.NewCoprRPCHandler())
 	s.Require().Nil(err)
-	mocktikv.BootstrapWithSingleStore(cluster)
+	testutils.BootstrapWithSingleStore(cluster)
 	s.cluster = cluster
 	store, err := tikv.NewTestTiKVStore(client, pdClient, nil, nil, 0)
 	s.Require().Nil(err)
 
-	// TODO: make this possible
-	// store, err := mockstore.NewMockStore(
-	// 	mockstore.WithClusterInspector(func(c cluster.Cluster) {
-	// 		mockstore.BootstrapWithSingleStore(c)
-	// 		s.cluster = c
-	// 	}),
-	// )
-	// c.Assert(err, IsNil)
 	s.store = tikv.StoreProbe{KVStore: store}
 	s.bo = tikv.NewBackofferWithVars(context.Background(), 5000, nil)
 }
diff --git a/internal/locate/region_cache_test.go b/internal/locate/region_cache_test.go
index efd2dbd..cd1cbb1 100644
--- a/internal/locate/region_cache_test.go
+++ b/internal/locate/region_cache_test.go
@@ -46,9 +46,9 @@ import (
 	"github.com/pingcap/kvproto/pkg/kvrpcpb"
 	"github.com/pingcap/kvproto/pkg/metapb"
 	"github.com/stretchr/testify/suite"
+	"github.com/tikv/client-go/v2/internal/mockstore/mocktikv"
 	"github.com/tikv/client-go/v2/internal/retry"
 	"github.com/tikv/client-go/v2/kv"
-	"github.com/tikv/client-go/v2/mockstore/mocktikv"
 	"github.com/tikv/client-go/v2/oracle"
 	"github.com/tikv/client-go/v2/tikvrpc"
 	pd "github.com/tikv/pd/client"
diff --git a/internal/locate/region_request3_test.go b/internal/locate/region_request3_test.go
index 83a9421..6f8edb0 100644
--- a/internal/locate/region_request3_test.go
+++ b/internal/locate/region_request3_test.go
@@ -45,9 +45,9 @@ import (
 	"github.com/pingcap/kvproto/pkg/metapb"
 	"github.com/stretchr/testify/suite"
 	tikverr "github.com/tikv/client-go/v2/error"
+	"github.com/tikv/client-go/v2/internal/mockstore/mocktikv"
 	"github.com/tikv/client-go/v2/internal/retry"
 	"github.com/tikv/client-go/v2/kv"
-	"github.com/tikv/client-go/v2/mockstore/mocktikv"
 	"github.com/tikv/client-go/v2/tikvrpc"
 )
 
diff --git a/internal/locate/region_request_test.go b/internal/locate/region_request_test.go
index cdbd264..b590ee4 100644
--- a/internal/locate/region_request_test.go
+++ b/internal/locate/region_request_test.go
@@ -51,8 +51,8 @@ import (
 	"github.com/stretchr/testify/suite"
 	"github.com/tikv/client-go/v2/config"
 	"github.com/tikv/client-go/v2/internal/client"
+	"github.com/tikv/client-go/v2/internal/mockstore/mocktikv"
 	"github.com/tikv/client-go/v2/internal/retry"
-	"github.com/tikv/client-go/v2/mockstore/mocktikv"
 	"github.com/tikv/client-go/v2/tikvrpc"
 	"google.golang.org/grpc"
 )
diff --git a/testutils/mockstore.go b/testutils/mockstore.go
new file mode 100644
index 0000000..4aaaddc
--- /dev/null
+++ b/testutils/mockstore.go
@@ -0,0 +1,60 @@
+// 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.
+
+package testutils
+
+import (
+	"github.com/tikv/client-go/v2/internal/mockstore/cluster"
+	"github.com/tikv/client-go/v2/internal/mockstore/mocktikv"
+	pd "github.com/tikv/pd/client"
+)
+
+// Cluster simulates a TiKV cluster.
+type Cluster = cluster.Cluster
+
+// CoprRPCHandler is the interface to handle coprocessor RPC commands.
+type CoprRPCHandler = mocktikv.CoprRPCHandler
+
+// MVCCStore is a mvcc key-value storage.
+type MVCCStore = mocktikv.MVCCStore
+
+// MVCCPair is a KV pair read from MvccStore or an error if any occurs.
+type MVCCPair = mocktikv.Pair
+
+// MockCluster simulates a TiKV cluster.
+type MockCluster = mocktikv.Cluster
+
+// MockClient sends kv RPC calls to mock cluster.
+type MockClient = mocktikv.RPCClient
+
+// RPCSession stores session scope rpc data.
+type RPCSession = mocktikv.Session
+
+// NewMockTiKV creates a TiKV client and PD client from options.
+func NewMockTiKV(path string, coprHandler CoprRPCHandler) (*MockClient, *MockCluster, pd.Client, error) {
+	return mocktikv.NewTiKVAndPDClient(path, coprHandler)
+}
+
+// BootstrapWithSingleStore initializes a Cluster with 1 Region and 1 Store.
+var BootstrapWithSingleStore = mocktikv.BootstrapWithSingleStore
+
+// BootstrapWithMultiStores initializes a Cluster with 1 Region and n Stores.
+var BootstrapWithMultiStores = mocktikv.BootstrapWithMultiStores
+
+// BootstrapWithMultiRegions initializes a Cluster with multiple Regions and 1
+// Store. The number of Regions will be len(splitKeys) + 1.
+var BootstrapWithMultiRegions = mocktikv.BootstrapWithMultiRegions
+
+// ErrLocked is returned when trying to Read/Write on a locked key. Client should
+// backoff or cleanup the lock then retry.
+type ErrLocked = mocktikv.ErrLocked
diff --git a/tikv/rawkv_test.go b/tikv/rawkv_test.go
index 94ceede..597560e 100644
--- a/tikv/rawkv_test.go
+++ b/tikv/rawkv_test.go
@@ -38,9 +38,9 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/suite"
+	"github.com/tikv/client-go/v2/internal/mockstore/mocktikv"
 	"github.com/tikv/client-go/v2/internal/retry"
 	"github.com/tikv/client-go/v2/kv"
-	"github.com/tikv/client-go/v2/mockstore/mocktikv"
 )
 
 func TestRawKV(t *testing.T) {