Skip to content

Commit

Permalink
store/tikv: share type OneByOneSuite for tests related to tikv (#25098)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMouche authored Jun 7, 2021
1 parent 840494d commit f3e2b79
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 117 deletions.
8 changes: 5 additions & 3 deletions session/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
storeerr "github.com/pingcap/tidb/store/driver/error"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/mockstore"
"github.com/pingcap/tidb/store/tikv/oracle"

"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
Expand Down Expand Up @@ -231,7 +233,7 @@ func (s *testPessimisticSuite) TestDeadlock(c *C) {
}

func (s *testPessimisticSuite) TestSingleStatementRollback(c *C) {
if *withTiKV {
if *mockstore.WithTiKV {
c.Skip("skip with tikv because cluster manipulate is not available")
}
tk := testkit.NewTestKitWithInit(c, s.store)
Expand Down Expand Up @@ -2077,7 +2079,7 @@ func (s *testPessimisticSuite) TestSelectForUpdateConflictRetry(c *C) {

func (s *testPessimisticSuite) TestAsyncCommitWithSchemaChange(c *C) {
// TODO: implement commit_ts calculation in unistore
if !*withTiKV {
if !*mockstore.WithTiKV {
return
}

Expand Down Expand Up @@ -2151,7 +2153,7 @@ func (s *testPessimisticSuite) TestAsyncCommitWithSchemaChange(c *C) {

func (s *testPessimisticSuite) Test1PCWithSchemaChange(c *C) {
// TODO: implement commit_ts calculation in unistore
if !*withTiKV {
if !*mockstore.WithTiKV {
return
}

Expand Down
10 changes: 5 additions & 5 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mockcopr"
"github.com/pingcap/tidb/store/tikv"
tikvmockstore "github.com/pingcap/tidb/store/tikv/mockstore"
"github.com/pingcap/tidb/store/tikv/mockstore/cluster"
tikvutil "github.com/pingcap/tidb/store/tikv/util"
"github.com/pingcap/tidb/table/tables"
Expand All @@ -67,7 +68,6 @@ import (
)

var (
withTiKV = flag.Bool("with-tikv", false, "run tests with TiKV cluster started. (not use the mock server)")
pdAddrs = flag.String("pd-addrs", "127.0.0.1:2379", "pd addrs")
pdAddrChan chan string
initPdAddrsOnce sync.Once
Expand Down Expand Up @@ -183,7 +183,7 @@ func initPdAddrs() {
func (s *testSessionSuiteBase) SetUpSuite(c *C) {
testleak.BeforeTest()

if *withTiKV {
if *tikvmockstore.WithTiKV {
initPdAddrs()
s.pdAddr = <-pdAddrChan
var d driver.TiKVDriver
Expand Down Expand Up @@ -219,7 +219,7 @@ func (s *testSessionSuiteBase) TearDownSuite(c *C) {
s.dom.Close()
s.store.Close()
testleak.AfterTest(c)()
if *withTiKV {
if *tikvmockstore.WithTiKV {
pdAddrChan <- s.pdAddr
}
}
Expand Down Expand Up @@ -3370,7 +3370,7 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) {
func (s *testSessionSerialSuite) TestGlobalAndLocalTxn(c *C) {
// Because the PD config of check_dev_2 test is not compatible with local/global txn yet,
// so we will skip this test for now.
if *withTiKV {
if *tikvmockstore.WithTiKV {
return
}
tk := testkit.NewTestKitWithInit(c, s.store)
Expand Down Expand Up @@ -3766,7 +3766,7 @@ func (s *testSessionSerialSuite) TestDoDDLJobQuit(c *C) {

func (s *testBackupRestoreSuite) TestBackupAndRestore(c *C) {
// only run BR SQL integration test with tikv store.
if *withTiKV {
if *tikvmockstore.WithTiKV {
cfg := config.GetGlobalConfig()
cfg.Store = "tikv"
cfg.Path = s.pdAddr
Expand Down
3 changes: 2 additions & 1 deletion store/driver/sql_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/mockstore"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testkit"
)
Expand All @@ -54,7 +55,7 @@ func (s *testSQLSuiteBase) SetUpSuite(c *C) {
var err error
s.store = NewTestStore(c)
// actual this is better done in `OneByOneSuite.SetUpSuite`, but this would cause circle dependency
if *WithTiKV {
if *mockstore.WithTiKV {
session.ResetStoreForWithTiKVTest(s.store)
}

Expand Down
26 changes: 4 additions & 22 deletions store/driver/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"context"
"flag"
"fmt"
"sync"
"testing"

. "github.com/pingcap/check"
Expand All @@ -26,6 +25,7 @@ import (
"github.com/pingcap/tidb/store/copr"
"github.com/pingcap/tidb/store/mockstore/unistore"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/mockstore"
)

func TestT(t *testing.T) {
Expand All @@ -34,9 +34,7 @@ func TestT(t *testing.T) {
}

var (
withTiKVGlobalLock sync.RWMutex
WithTiKV = flag.Bool("with-tikv", false, "run tests with TiKV cluster started. (not use the mock server)")
pdAddrs = flag.String("pd-addrs", "127.0.0.1:2379", "pd addrs")
pdAddrs = flag.String("pd-addrs", "127.0.0.1:2379", "pd addrs")
)

// NewTestStore creates a kv.Storage for testing purpose.
Expand All @@ -45,7 +43,7 @@ func NewTestStore(c *C) kv.Storage {
flag.Parse()
}

if *WithTiKV {
if *mockstore.WithTiKV {
var d TiKVDriver
store, err := d.Open(fmt.Sprintf("tikv://%s", *pdAddrs))
c.Assert(err, IsNil)
Expand Down Expand Up @@ -82,20 +80,4 @@ func clearStorage(store kv.Storage) error {
}

// OneByOneSuite is a suite, When with-tikv flag is true, there is only one storage, so the test suite have to run one by one.
type OneByOneSuite struct{}

func (s *OneByOneSuite) SetUpSuite(c *C) {
if *WithTiKV {
withTiKVGlobalLock.Lock()
} else {
withTiKVGlobalLock.RLock()
}
}

func (s *OneByOneSuite) TearDownSuite(c *C) {
if *WithTiKV {
withTiKVGlobalLock.Unlock()
} else {
withTiKVGlobalLock.RUnlock()
}
}
type OneByOneSuite = mockstore.OneByOneSuite
4 changes: 2 additions & 2 deletions store/tikv/client/client_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ type testClientFailSuite struct {

func (s *testClientFailSuite) SetUpSuite(_ *C) {
// This lock make testClientFailSuite runs exclusively.
withTiKVGlobalLock.Lock()
s.LockGlobalTiKV()
}

func (s testClientFailSuite) TearDownSuite(_ *C) {
withTiKVGlobalLock.Unlock()
s.UnLockGlobalTiKV()
}

func (s *testClientFailSuite) TestPanicInRecvLoop(c *C) {
Expand Down
25 changes: 2 additions & 23 deletions store/tikv/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package client

import (
"context"
"flag"
"fmt"
"sync"
"sync/atomic"
Expand All @@ -29,33 +28,13 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/tikvpb"
"github.com/pingcap/tidb/store/tikv/config"
"github.com/pingcap/tidb/store/tikv/mockstore"
"github.com/pingcap/tidb/store/tikv/tikvrpc"
"google.golang.org/grpc/metadata"
)

var (
withTiKVGlobalLock sync.RWMutex
WithTiKV = flag.Bool("with-tikv", false, "run tests with TiKV cluster started. (not use the mock server)")
)

// OneByOneSuite is a suite, When with-tikv flag is true, there is only one storage, so the test suite have to run one by one.
type OneByOneSuite struct{}

func (s *OneByOneSuite) SetUpSuite(c *C) {
if *WithTiKV {
withTiKVGlobalLock.Lock()
} else {
withTiKVGlobalLock.RLock()
}
}

func (s *OneByOneSuite) TearDownSuite(c *C) {
if *WithTiKV {
withTiKVGlobalLock.Unlock()
} else {
withTiKVGlobalLock.RUnlock()
}
}
type OneByOneSuite = mockstore.OneByOneSuite

func TestT(t *testing.T) {
CustomVerboseFlag = true
Expand Down
58 changes: 58 additions & 0 deletions store/tikv/mockstore/test_suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2018 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 mockstore

import (
"flag"
"sync"

"github.com/pingcap/check"
)

var (
withTiKVGlobalLock sync.RWMutex
// WithTiKV is the flag which indicates whether it runs with tikv.
WithTiKV = flag.Bool("with-tikv", false, "run tests with TiKV cluster started. (not use the mock server)")
)

// OneByOneSuite is a suite, When with-tikv flag is true, there is only one storage, so the test suite have to run one by one.
type OneByOneSuite struct{}

// SetUpSuite implements the interface check.Suite.
func (s *OneByOneSuite) SetUpSuite(c *check.C) {
if *WithTiKV {
withTiKVGlobalLock.Lock()
} else {
withTiKVGlobalLock.RLock()
}
}

// TearDownSuite implements the interface check.Suite.
func (s *OneByOneSuite) TearDownSuite(c *check.C) {
if *WithTiKV {
withTiKVGlobalLock.Unlock()
} else {
withTiKVGlobalLock.RUnlock()
}
}

// LockGlobalTiKV locks withTiKVGlobalLock.
func (s *OneByOneSuite) LockGlobalTiKV() {
withTiKVGlobalLock.Lock()
}

// UnLockGlobalTiKV unlocks withTiKVGlobalLock
func (s *OneByOneSuite) UnLockGlobalTiKV() {
withTiKVGlobalLock.Unlock()
}
5 changes: 3 additions & 2 deletions store/tikv/tests/1pc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/metrics"
"github.com/pingcap/tidb/store/tikv/mockstore"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/store/tikv/util"
)
Expand Down Expand Up @@ -179,7 +180,7 @@ func (s *testOnePCSuite) Test1PCIsolation(c *C) {

func (s *testOnePCSuite) Test1PCDisallowMultiRegion(c *C) {
// This test doesn't support tikv mode.
if *WithTiKV {
if *mockstore.WithTiKV {
return
}

Expand Down Expand Up @@ -247,7 +248,7 @@ func (s *testOnePCSuite) Test1PCLinearizability(c *C) {

func (s *testOnePCSuite) Test1PCWithMultiDC(c *C) {
// It requires setting placement rules to run with TiKV
if *WithTiKV {
if *mockstore.WithTiKV {
return
}

Expand Down
11 changes: 6 additions & 5 deletions store/tikv/tests/async_commit_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/store/tikv"
tikverr "github.com/pingcap/tidb/store/tikv/error"
"github.com/pingcap/tidb/store/tikv/mockstore"
"github.com/pingcap/tidb/store/tikv/util"
)

Expand All @@ -43,7 +44,7 @@ func (s *testAsyncCommitFailSuite) SetUpTest(c *C) {
// committing primary region task.
func (s *testAsyncCommitFailSuite) TestFailAsyncCommitPrewriteRpcErrors(c *C) {
// This test doesn't support tikv mode because it needs setting failpoint in unistore.
if *WithTiKV {
if *mockstore.WithTiKV {
return
}

Expand Down Expand Up @@ -75,7 +76,7 @@ func (s *testAsyncCommitFailSuite) TestFailAsyncCommitPrewriteRpcErrors(c *C) {

func (s *testAsyncCommitFailSuite) TestAsyncCommitPrewriteCancelled(c *C) {
// This test doesn't support tikv mode because it needs setting failpoint in unistore.
if *WithTiKV {
if *mockstore.WithTiKV {
return
}

Expand Down Expand Up @@ -135,7 +136,7 @@ func (s *testAsyncCommitFailSuite) TestPointGetWithAsyncCommit(c *C) {

func (s *testAsyncCommitFailSuite) TestSecondaryListInPrimaryLock(c *C) {
// This test doesn't support tikv mode.
if *WithTiKV {
if *mockstore.WithTiKV {
return
}

Expand Down Expand Up @@ -237,7 +238,7 @@ func (s *testAsyncCommitFailSuite) TestAsyncCommitContextCancelCausingUndetermin
// TestAsyncCommitRPCErrorThenWriteConflict verifies that the determined failure error overwrites undetermined error.
func (s *testAsyncCommitFailSuite) TestAsyncCommitRPCErrorThenWriteConflict(c *C) {
// This test doesn't support tikv mode because it needs setting failpoint in unistore.
if *WithTiKV {
if *mockstore.WithTiKV {
return
}

Expand All @@ -260,7 +261,7 @@ func (s *testAsyncCommitFailSuite) TestAsyncCommitRPCErrorThenWriteConflict(c *C
// overwrites the undetermined error in the parent.
func (s *testAsyncCommitFailSuite) TestAsyncCommitRPCErrorThenWriteConflictInChild(c *C) {
// This test doesn't support tikv mode because it needs setting failpoint in unistore.
if *WithTiKV {
if *mockstore.WithTiKV {
return
}

Expand Down
7 changes: 4 additions & 3 deletions store/tikv/tests/async_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/store/mockstore/unistore"
"github.com/pingcap/tidb/store/tikv"
tikverr "github.com/pingcap/tidb/store/tikv/error"
"github.com/pingcap/tidb/store/tikv/mockstore"
"github.com/pingcap/tidb/store/tikv/mockstore/cluster"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/store/tikv/tikvrpc"
Expand All @@ -47,7 +48,7 @@ type testAsyncCommitCommon struct {
}

func (s *testAsyncCommitCommon) setUpTest(c *C) {
if *WithTiKV {
if *mockstore.WithTiKV {
s.store = NewTestStore(c)
return
}
Expand Down Expand Up @@ -195,7 +196,7 @@ func (s *testAsyncCommitSuite) lockKeysWithAsyncCommit(c *C, keys, values [][]by

func (s *testAsyncCommitSuite) TestCheckSecondaries(c *C) {
// This test doesn't support tikv mode.
if *WithTiKV {
if *mockstore.WithTiKV {
return
}

Expand Down Expand Up @@ -402,7 +403,7 @@ func (s *testAsyncCommitSuite) TestAsyncCommitLinearizability(c *C) {
// TestAsyncCommitWithMultiDC tests that async commit can only be enabled in global transactions
func (s *testAsyncCommitSuite) TestAsyncCommitWithMultiDC(c *C) {
// It requires setting placement rules to run with TiKV
if *WithTiKV {
if *mockstore.WithTiKV {
return
}

Expand Down
Loading

0 comments on commit f3e2b79

Please sign in to comment.