Skip to content

Commit

Permalink
store: migrate test-infra to testify (#27993)
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun committed Sep 16, 2021
1 parent 7b79dd1 commit 463e6ba
Show file tree
Hide file tree
Showing 4 changed files with 454 additions and 332 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,24 @@ package store
import (
"context"
"fmt"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/unistore"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/testutils"
)

type testBatchCopSuite struct {
}

var _ = SerialSuites(&testBatchCopSuite{})

func newStoreWithBootstrap(tiflashNum int) (kv.Storage, *domain.Domain, error) {
store, err := mockstore.NewMockStore(
func createMockTiKVStoreOptions(tiflashNum int) []mockstore.MockTiKVStoreOption {
return []mockstore.MockTiKVStoreOption{
mockstore.WithClusterInspector(func(c testutils.Cluster) {
mockCluster := c.(*unistore.Cluster)
_, _, region1 := mockstore.BootstrapWithSingleStore(c)
Expand All @@ -55,98 +49,82 @@ func newStoreWithBootstrap(tiflashNum int) (kv.Storage, *domain.Domain, error) {
}
}),
mockstore.WithStoreType(mockstore.EmbedUnistore),
)

if err != nil {
return nil, nil, errors.Trace(err)
}

session.SetSchemaLease(0)
session.DisableStats4Test()

dom, err := session.BootstrapSession(store)
if err != nil {
return nil, nil, err
}

dom.SetStatsUpdating(true)
return store, dom, errors.Trace(err)
}

func testGetTableByName(c *C, ctx sessionctx.Context, db, table string) table.Table {
func testGetTableByName(t *testing.T, ctx sessionctx.Context, db, table string) table.Table {
dom := domain.GetDomain(ctx)
// Make sure the table schema is the new schema.
err := dom.Reload()
c.Assert(err, IsNil)
require.NoError(t, err)
tbl, err := dom.InfoSchema().TableByName(model.NewCIStr(db), model.NewCIStr(table))
c.Assert(err, IsNil)
require.NoError(t, err)
return tbl
}

func (s *testBatchCopSuite) TestStoreErr(c *C) {
store, dom, err := newStoreWithBootstrap(1)
c.Assert(err, IsNil)
func TestStoreErr(t *testing.T) {
store, clean := testkit.CreateMockStore(t, createMockTiKVStoreOptions(1)...)
defer clean()

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount", `return(true)`))
defer func() {
dom.Close()
store.Close()
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount"))
}()

c.Assert(failpoint.Enable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount", `return(true)`), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount")

tk := testkit.NewTestKit(c, store)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int not null, b int not null)")
tk.MustExec("alter table t set tiflash replica 1")
tb := testGetTableByName(c, tk.Se, "test", "t")
err = domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, tb.Meta().ID, true)
c.Assert(err, IsNil)
tb := testGetTableByName(t, tk.Session(), "test", "t")

err := domain.GetDomain(tk.Session()).DDL().UpdateTableReplicaInfo(tk.Session(), tb.Meta().ID, true)
require.NoError(t, err)

tk.MustExec("insert into t values(1,0)")
tk.MustExec("set @@session.tidb_isolation_read_engines=\"tiflash\"")
tk.MustExec("set @@session.tidb_allow_mpp=OFF")

c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopCancelled", "1*return(true)"), IsNil)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopCancelled", "1*return(true)"))

err = tk.QueryToErr("select count(*) from t")
c.Assert(errors.Cause(err), Equals, context.Canceled)
require.Equal(t, context.Canceled, errors.Cause(err))

c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopRpcErrtiflash0", "1*return(\"tiflash0\")"), IsNil)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopRpcErrtiflash0", "1*return(\"tiflash0\")"))

tk.MustQuery("select count(*) from t").Check(testkit.Rows("1"))

c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopRpcErrtiflash0", "return(\"tiflash0\")"), IsNil)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopRpcErrtiflash0", "return(\"tiflash0\")"))
err = tk.QueryToErr("select count(*) from t")
c.Assert(err, NotNil)
require.Error(t, err)
}

func (s *testBatchCopSuite) TestStoreSwitchPeer(c *C) {
store, dom, err := newStoreWithBootstrap(2)
c.Assert(err, IsNil)
func TestStoreSwitchPeer(t *testing.T) {
store, clean := testkit.CreateMockStore(t, createMockTiKVStoreOptions(2)...)
defer clean()

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount", `return(true)`))
defer func() {
dom.Close()
store.Close()
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount"))
}()

c.Assert(failpoint.Enable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount", `return(true)`), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount")

tk := testkit.NewTestKit(c, store)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int not null, b int not null)")
tk.MustExec("alter table t set tiflash replica 1")
tb := testGetTableByName(c, tk.Se, "test", "t")
err = domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, tb.Meta().ID, true)
c.Assert(err, IsNil)
tb := testGetTableByName(t, tk.Session(), "test", "t")

err := domain.GetDomain(tk.Session()).DDL().UpdateTableReplicaInfo(tk.Session(), tb.Meta().ID, true)
require.NoError(t, err)

tk.MustExec("insert into t values(1,0)")
tk.MustExec("set @@session.tidb_isolation_read_engines=\"tiflash\"")
tk.MustExec("set @@session.tidb_allow_mpp=OFF")

c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopRpcErrtiflash0", "return(\"tiflash0\")"), IsNil)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopRpcErrtiflash0", "return(\"tiflash0\")"))

tk.MustQuery("select count(*) from t").Check(testkit.Rows("1"))

c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopRpcErrtiflash1", "return(\"tiflash1\")"), IsNil)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/BatchCopRpcErrtiflash1", "return(\"tiflash1\")"))
err = tk.QueryToErr("select count(*) from t")
c.Assert(err, NotNil)

require.Error(t, err)
}
31 changes: 31 additions & 0 deletions store/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package store

import (
"testing"

"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
goleak.VerifyTestMain(m, opts...)
}
Loading

0 comments on commit 463e6ba

Please sign in to comment.