diff --git a/Makefile b/Makefile index 7cb288823fefb..b6b3cab07a462 100644 --- a/Makefile +++ b/Makefile @@ -125,7 +125,7 @@ ut: tools/bin/ut tools/bin/xprog failpoint-enable gotest: failpoint-enable @echo "Running in native mode." @export log_level=info; export TZ='Asia/Shanghai'; \ - $(GOTEST) -ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -timeout 20m -cover $(PACKAGES_TIDB_TESTS) -coverprofile=coverage.txt -check.p true > gotest.log || { $(FAILPOINT_DISABLE); cat 'gotest.log'; exit 1; } + $(GOTEST) -ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -timeout 20m -cover $(PACKAGES_TIDB_TESTS) -coverprofile=coverage.txt > gotest.log || { $(FAILPOINT_DISABLE); cat 'gotest.log'; exit 1; } @$(FAILPOINT_DISABLE) gotest_in_verify_ci: tools/bin/xprog tools/bin/ut failpoint-enable @@ -151,11 +151,6 @@ race: failpoint-enable @$(FAILPOINT_DISABLE) @$(CLEAN_UT_BINARY) -leak: failpoint-enable - @export log_level=debug; \ - $(GOTEST) -tags leak $(PACKAGES) || { $(FAILPOINT_DISABLE); exit 1; } - @$(FAILPOINT_DISABLE) - server: ifeq ($(TARGET), "") CGO_ENABLED=1 $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS) $(CHECK_FLAG)' -o bin/tidb-server tidb-server/main.go @@ -273,7 +268,7 @@ ifeq ("$(pkg)", "") else @echo "Running unit test for github.com/pingcap/tidb/$(pkg)" @export log_level=fatal; export TZ='Asia/Shanghai'; \ - $(GOTEST) -ldflags '$(TEST_LDFLAGS)' -cover github.com/pingcap/tidb/$(pkg) -check.p true -check.timeout 4s || { $(FAILPOINT_DISABLE); exit 1; } + $(GOTEST) -ldflags '$(TEST_LDFLAGS)' -cover github.com/pingcap/tidb/$(pkg) || { $(FAILPOINT_DISABLE); exit 1; } endif @$(FAILPOINT_DISABLE) @@ -336,7 +331,7 @@ br_unit_test: export ARGS=$$($(BR_PACKAGES)) br_unit_test: @make failpoint-enable @export TZ='Asia/Shanghai'; - $(GOTEST) $(RACE_FLAG) -ldflags '$(LDFLAGS)' -tags leak $(ARGS) -coverprofile=coverage.txt || ( make failpoint-disable && exit 1 ) + $(GOTEST) $(RACE_FLAG) -ldflags '$(LDFLAGS)' $(ARGS) -coverprofile=coverage.txt || ( make failpoint-disable && exit 1 ) @make failpoint-disable br_unit_test_in_verify_ci: export ARGS=$$($(BR_PACKAGES)) br_unit_test_in_verify_ci: tools/bin/gotestsum @@ -344,7 +339,7 @@ br_unit_test_in_verify_ci: tools/bin/gotestsum @export TZ='Asia/Shanghai'; @mkdir -p $(TEST_COVERAGE_DIR) CGO_ENABLED=1 tools/bin/gotestsum --junitfile "$(TEST_COVERAGE_DIR)/br-junit-report.xml" -- $(RACE_FLAG) -ldflags '$(LDFLAGS)' \ - -tags leak $(ARGS) -coverprofile="$(TEST_COVERAGE_DIR)/br_cov.unit_test.out" || ( make failpoint-disable && exit 1 ) + $(ARGS) -coverprofile="$(TEST_COVERAGE_DIR)/br_cov.unit_test.out" || ( make failpoint-disable && exit 1 ) @make failpoint-disable br_integration_test: br_bins build_br build_for_br_integration_test @@ -401,12 +396,12 @@ build_dumpling: dumpling_unit_test: export DUMPLING_ARGS=$$($(DUMPLING_PACKAGES)) dumpling_unit_test: failpoint-enable - $(DUMPLING_GOTEST) $(RACE_FLAG) -coverprofile=coverage.txt -covermode=atomic -tags leak $(DUMPLING_ARGS) || ( make failpoint-disable && exit 1 ) + $(DUMPLING_GOTEST) $(RACE_FLAG) -coverprofile=coverage.txt -covermode=atomic $(DUMPLING_ARGS) || ( make failpoint-disable && exit 1 ) @make failpoint-disable dumpling_unit_test_in_verify_ci: export DUMPLING_ARGS=$$($(DUMPLING_PACKAGES)) dumpling_unit_test_in_verify_ci: failpoint-enable tools/bin/gotestsum @mkdir -p $(TEST_COVERAGE_DIR) - CGO_ENABLED=1 tools/bin/gotestsum --junitfile "$(TEST_COVERAGE_DIR)/dumpling-junit-report.xml" -- -tags leak $(DUMPLING_ARGS) \ + CGO_ENABLED=1 tools/bin/gotestsum --junitfile "$(TEST_COVERAGE_DIR)/dumpling-junit-report.xml" -- $(DUMPLING_ARGS) \ $(RACE_FLAG) -coverprofile="$(TEST_COVERAGE_DIR)/dumpling_cov.unit_test.out" || ( make failpoint-disable && exit 1 ) @make failpoint-disable diff --git a/bindinfo/main_test.go b/bindinfo/main_test.go index 3b69fc67ec943..ede7172be10a6 100644 --- a/bindinfo/main_test.go +++ b/bindinfo/main_test.go @@ -17,12 +17,12 @@ package bindinfo_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/br/pkg/backup/main_test.go b/br/pkg/backup/main_test.go index e47b44847e9f7..49c4832927aa9 100644 --- a/br/pkg/backup/main_test.go +++ b/br/pkg/backup/main_test.go @@ -17,12 +17,12 @@ package backup import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("github.com/klauspost/compress/zstd.(*blockDec).startDecoder"), diff --git a/br/pkg/checksum/main_test.go b/br/pkg/checksum/main_test.go index 801f0a62c4f06..f4c181ccf64c2 100644 --- a/br/pkg/checksum/main_test.go +++ b/br/pkg/checksum/main_test.go @@ -17,7 +17,7 @@ package checksum import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -28,6 +28,6 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m, opts...) } diff --git a/br/pkg/conn/main_test.go b/br/pkg/conn/main_test.go index 512c52a3fc985..9ab9ec0d31297 100644 --- a/br/pkg/conn/main_test.go +++ b/br/pkg/conn/main_test.go @@ -17,7 +17,7 @@ package conn import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -27,6 +27,6 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m, opts...) } diff --git a/br/pkg/lightning/checkpoints/main_test.go b/br/pkg/lightning/checkpoints/main_test.go index a12dce75ecf1b..aa707ae68ea51 100644 --- a/br/pkg/lightning/checkpoints/main_test.go +++ b/br/pkg/lightning/checkpoints/main_test.go @@ -17,12 +17,12 @@ package checkpoints_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("github.com/klauspost/compress/zstd.(*blockDec).startDecoder"), diff --git a/br/pkg/lightning/common/main_test.go b/br/pkg/lightning/common/main_test.go index 716589aa28a7c..89c3779ccc1b2 100644 --- a/br/pkg/lightning/common/main_test.go +++ b/br/pkg/lightning/common/main_test.go @@ -17,12 +17,12 @@ package common_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), diff --git a/br/pkg/lightning/mydump/main_test.go b/br/pkg/lightning/mydump/main_test.go index 12be83040f259..f2672cd1bbc89 100644 --- a/br/pkg/lightning/mydump/main_test.go +++ b/br/pkg/lightning/mydump/main_test.go @@ -17,12 +17,12 @@ package mydump import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), diff --git a/br/pkg/metautil/main_test.go b/br/pkg/metautil/main_test.go index facb4e32cb101..700d234b0182d 100644 --- a/br/pkg/metautil/main_test.go +++ b/br/pkg/metautil/main_test.go @@ -17,7 +17,7 @@ package metautil import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -26,6 +26,6 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m, opts...) } diff --git a/br/pkg/pdutil/main_test.go b/br/pkg/pdutil/main_test.go index f87dd67ce2d47..86b9c6e1a61ad 100644 --- a/br/pkg/pdutil/main_test.go +++ b/br/pkg/pdutil/main_test.go @@ -17,12 +17,12 @@ package pdutil import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/br/pkg/restore/main_test.go b/br/pkg/restore/main_test.go index 7c729307e6584..43df5b07d486d 100644 --- a/br/pkg/restore/main_test.go +++ b/br/pkg/restore/main_test.go @@ -20,12 +20,12 @@ import ( "testing" "github.com/pingcap/tidb/br/pkg/mock" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("github.com/klauspost/compress/zstd.(*blockDec).startDecoder"), diff --git a/br/pkg/rtree/main_test.go b/br/pkg/rtree/main_test.go index f1dcccc623422..6c415ec6e7593 100644 --- a/br/pkg/rtree/main_test.go +++ b/br/pkg/rtree/main_test.go @@ -17,12 +17,12 @@ package rtree_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/br/pkg/stream/decode_kv_test.go b/br/pkg/stream/decode_kv_test.go index 20302b0cc1207..3607be6c73700 100644 --- a/br/pkg/stream/decode_kv_test.go +++ b/br/pkg/stream/decode_kv_test.go @@ -3,22 +3,13 @@ package stream_test import ( - "bytes" "testing" - . "github.com/pingcap/check" "github.com/pingcap/tidb/br/pkg/stream" + "github.com/stretchr/testify/require" ) -type testDecodeKVSuite struct{} - -func TestT(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&testDecodeKVSuite{}) - -func (s *testDecodeKVSuite) TestDecodeKVEntry(c *C) { +func TestDecodeKVEntry(t *testing.T) { var ( pairs = map[string]string{ "db": "tidb", @@ -37,17 +28,17 @@ func (s *testDecodeKVSuite) TestDecodeKVEntry(c *C) { for ei.Valid() { ei.Next() err := ei.GetError() - c.Assert(err, IsNil) + require.NoError(t, err) key := ei.Key() value := ei.Value() v, exist := pairs[string(key)] - c.Assert(exist, IsTrue) - c.Assert(string(value), Equals, v) + require.True(t, exist) + require.Equal(t, v, string(value)) } } -func (s *testDecodeKVSuite) TestDecodeKVEntryError(c *C) { +func TestDecodeKVEntryError(t *testing.T) { var ( k = []byte("db") v = []byte("tidb") @@ -59,10 +50,10 @@ func (s *testDecodeKVSuite) TestDecodeKVEntryError(c *C) { ei := stream.NewEventIterator(buff) ei.Next() - c.Assert(bytes.Equal(k, ei.Key()), IsTrue) - c.Assert(bytes.Equal(v, ei.Value()), IsTrue) - c.Assert(ei.Valid(), IsTrue) + require.Equal(t, k, ei.Key()) + require.Equal(t, v, ei.Value()) + require.True(t, ei.Valid()) ei.Next() - c.Assert(ei.GetError(), NotNil) + require.Error(t, ei.GetError()) } diff --git a/br/pkg/stream/meta_kv_test.go b/br/pkg/stream/meta_kv_test.go index fa543b4572a13..eaebf64526243 100644 --- a/br/pkg/stream/meta_kv_test.go +++ b/br/pkg/stream/meta_kv_test.go @@ -4,24 +4,21 @@ package stream import ( "bytes" + "testing" - . "github.com/pingcap/check" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/util/codec" + "github.com/stretchr/testify/require" ) -type testMetaKVSuite struct{} - -var _ = Suite(&testMetaKVSuite{}) - func encodeTxnMetaKey(key []byte, field []byte, ts uint64) []byte { k := tablecodec.EncodeMetaKey(key, field) txnKey := codec.EncodeBytes(nil, k) return codec.EncodeUintDesc(txnKey, ts) } -func (s *testMetaKVSuite) TestRawMetaKeyForDB(c *C) { +func TestRawMetaKeyForDB(t *testing.T) { var ( dbID int64 = 1 ts uint64 = 400036290571534337 @@ -30,18 +27,18 @@ func (s *testMetaKVSuite) TestRawMetaKeyForDB(c *C) { txnKey := encodeTxnMetaKey(mDbs, meta.DBkey(dbID), ts) - rawMetakey, err := ParseTxnMetaKeyFrom(txnKey) - c.Assert(err, IsNil) + rawMetaKey, err := ParseTxnMetaKeyFrom(txnKey) + require.NoError(t, err) - parseDbID, err := meta.ParseDBKey(rawMetakey.Field) - c.Assert(err, IsNil) - c.Assert(parseDbID, Equals, dbID) + parseDbID, err := meta.ParseDBKey(rawMetaKey.Field) + require.NoError(t, err) + require.Equal(t, dbID, parseDbID) - newKey := rawMetakey.EncodeMetaKey() - c.Assert(bytes.Equal(txnKey, newKey), IsTrue) + newKey := rawMetaKey.EncodeMetaKey() + require.Equal(t, string(txnKey), string(newKey)) } -func (s *testMetaKVSuite) TestRawMetaKeyForTable(c *C) { +func TestRawMetaKeyForTable(t *testing.T) { var ( dbID int64 = 1 tableID int64 = 57 @@ -50,42 +47,41 @@ func (s *testMetaKVSuite) TestRawMetaKeyForTable(c *C) { txnKey := encodeTxnMetaKey(meta.DBkey(dbID), meta.TableKey(tableID), ts) rawMetakey, err := ParseTxnMetaKeyFrom(txnKey) - c.Assert(err, IsNil) + require.NoError(t, err) parseDBID, err := meta.ParseDBKey(rawMetakey.Key) - c.Assert(err, IsNil) - c.Assert(parseDBID, Equals, dbID) + require.NoError(t, err) + require.Equal(t, dbID, parseDBID) parseTableID, err := meta.ParseTableKey(rawMetakey.Field) - c.Assert(err, IsNil) - c.Assert(parseTableID, Equals, tableID) + require.NoError(t, err) + require.Equal(t, tableID, parseTableID) newKey := rawMetakey.EncodeMetaKey() - c.Assert(bytes.Equal(txnKey, newKey), IsTrue) + require.True(t, bytes.Equal(txnKey, newKey)) } -func (s *testMetaKVSuite) TestWriteType(c *C) { - t := 'P' - wt, err := WriteTypeFrom(byte(t)) - c.Assert(err, IsNil) - c.Assert(wt, Equals, WriteTypePut) +func TestWriteType(t *testing.T) { + wt, err := WriteTypeFrom(byte('P')) + require.NoError(t, err) + require.Equal(t, WriteTypePut, wt) } -func (s *testMetaKVSuite) TestWriteCFValueNoShortValue(c *C) { +func TestWriteCFValueNoShortValue(t *testing.T) { buff := make([]byte, 0, 9) buff = append(buff, byte('P')) buff = codec.EncodeUvarint(buff, 400036290571534337) v := new(RawWriteCFValue) err := v.ParseFrom(buff) - c.Assert(err, IsNil) - c.Assert(v.HasShortValue(), IsFalse) + require.NoError(t, err) + require.False(t, v.HasShortValue()) encodedBuff := v.EncodeTo() - c.Assert(bytes.Equal(buff, encodedBuff), IsTrue) + require.True(t, bytes.Equal(buff, encodedBuff)) } -func (s *testMetaKVSuite) TestWriteCFValueWithShortValue(c *C) { +func TestWriteCFValueWithShortValue(t *testing.T) { var ts uint64 = 400036290571534337 shortValue := []byte("pingCAP") @@ -98,12 +94,12 @@ func (s *testMetaKVSuite) TestWriteCFValueWithShortValue(c *C) { v := new(RawWriteCFValue) err := v.ParseFrom(buff) - c.Assert(err, IsNil) - c.Assert(v.HasShortValue(), IsTrue) - c.Assert(bytes.Equal(v.GetShortValue(), shortValue), IsTrue) - c.Assert(v.hasGCFence, IsFalse) - c.Assert(v.hasOverlappedRollback, IsFalse) + require.NoError(t, err) + require.True(t, v.HasShortValue()) + require.True(t, bytes.Equal(v.GetShortValue(), shortValue)) + require.False(t, v.hasGCFence) + require.False(t, v.hasOverlappedRollback) data := v.EncodeTo() - c.Assert(bytes.Equal(data, buff), IsTrue) + require.True(t, bytes.Equal(data, buff)) } diff --git a/br/pkg/stream/rewrite_meta_rawkv_test.go b/br/pkg/stream/rewrite_meta_rawkv_test.go index e742d52b841b7..4467a456710c7 100644 --- a/br/pkg/stream/rewrite_meta_rawkv_test.go +++ b/br/pkg/stream/rewrite_meta_rawkv_test.go @@ -4,18 +4,15 @@ package stream_test import ( "encoding/json" + "testing" - . "github.com/pingcap/check" "github.com/pingcap/log" "github.com/pingcap/tidb/br/pkg/logutil" "github.com/pingcap/tidb/parser/model" + "github.com/stretchr/testify/require" "go.uber.org/zap" ) -type testRewriteMetaRawKvSuite struct{} - -var _ = Suite(&testRewriteMetaRawKvSuite{}) - func ProduceValue(tableName string, dbID int64) ([]byte, error) { tableInfo := model.TableInfo{ ID: dbID, @@ -25,7 +22,7 @@ func ProduceValue(tableName string, dbID int64) ([]byte, error) { return json.Marshal(tableInfo) } -func (s *testRewriteMetaRawKvSuite) TestRewriteValueForTable(c *C) { +func TestRewriteValueForTable(t *testing.T) { var ( tableName = "person" tableID = 57 @@ -33,10 +30,10 @@ func (s *testRewriteMetaRawKvSuite) TestRewriteValueForTable(c *C) { ) v, err := ProduceValue(tableName, int64(tableID)) - c.Assert(err, Equals, nil) + require.NoError(t, err) log.Info("old-value", zap.Int("value-len", len(v)), zap.ByteString("old-value", v), logutil.Key("old-value", v)) v, err = ProduceValue(tableName, int64(newTableID)) - c.Assert(err, Equals, nil) + require.NoError(t, err) log.Info("new-value", zap.Int("value-len", len(v)), zap.ByteString("new-value", v), logutil.Key("new-value", v)) } diff --git a/br/pkg/summary/main_test.go b/br/pkg/summary/main_test.go index bcf86e1381363..48d22e0e5ea11 100644 --- a/br/pkg/summary/main_test.go +++ b/br/pkg/summary/main_test.go @@ -17,12 +17,12 @@ package summary import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/br/pkg/trace/main_test.go b/br/pkg/trace/main_test.go index bf7e3161113fb..3447b03df11db 100644 --- a/br/pkg/trace/main_test.go +++ b/br/pkg/trace/main_test.go @@ -17,12 +17,12 @@ package trace import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/br/pkg/utils/main_test.go b/br/pkg/utils/main_test.go index 990afd5522d4b..b575947bf44f8 100644 --- a/br/pkg/utils/main_test.go +++ b/br/pkg/utils/main_test.go @@ -17,7 +17,7 @@ package utils import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -26,6 +26,6 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m, opts...) } diff --git a/cmd/ddltest/main_test.go b/cmd/ddltest/main_test.go index 9a23dc93c9a52..6016cb9c8d12a 100644 --- a/cmd/ddltest/main_test.go +++ b/cmd/ddltest/main_test.go @@ -20,13 +20,13 @@ import ( "testing" zaplog "github.com/pingcap/log" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/pingcap/tidb/util/logutil" - "github.com/pingcap/tidb/util/testbridge" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() err := logutil.InitLogger(&logutil.LogConfig{Config: zaplog.Config{Level: *logLevel}}) if err != nil { fmt.Fprint(os.Stderr, err.Error()) diff --git a/config/main_test.go b/config/main_test.go index f057995fb6137..363fd39d78304 100644 --- a/config/main_test.go +++ b/config/main_test.go @@ -17,12 +17,12 @@ package config import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/ddl/failtest/main_test.go b/ddl/failtest/main_test.go index f3ce7d61fe808..f363a41b544e8 100644 --- a/ddl/failtest/main_test.go +++ b/ddl/failtest/main_test.go @@ -20,12 +20,12 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() config.UpdateGlobal(func(conf *config.Config) { conf.TiKVClient.AsyncCommit.SafeWindow = 0 diff --git a/ddl/label/main_test.go b/ddl/label/main_test.go index 570afb3bb4e05..b077fcc255bcc 100644 --- a/ddl/label/main_test.go +++ b/ddl/label/main_test.go @@ -17,12 +17,12 @@ package label import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/ddl/main_test.go b/ddl/main_test.go index 16e547954ea50..2db5d9668ca6d 100644 --- a/ddl/main_test.go +++ b/ddl/main_test.go @@ -26,13 +26,13 @@ import ( "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/domain/infosync" "github.com/pingcap/tidb/meta/autoid" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/tikv/client-go/v2/tikv" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() tikv.EnableFailpoints() domain.SchemaOutOfDateRetryInterval.Store(50 * time.Millisecond) diff --git a/ddl/util/main_test.go b/ddl/util/main_test.go index eef260a706759..a28cdcb4b5bfc 100644 --- a/ddl/util/main_test.go +++ b/ddl/util/main_test.go @@ -17,12 +17,12 @@ package util import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/distsql/main_test.go b/distsql/main_test.go index 052afbf350c46..1d8765d866f59 100644 --- a/distsql/main_test.go +++ b/distsql/main_test.go @@ -17,12 +17,12 @@ package distsql import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/domain/globalconfigsync/globalconfig_test.go b/domain/globalconfigsync/globalconfig_test.go index 7cc40acfd992e..db7d9f7c2de42 100644 --- a/domain/globalconfigsync/globalconfig_test.go +++ b/domain/globalconfigsync/globalconfig_test.go @@ -24,7 +24,7 @@ import ( "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/store/mockstore" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/stretchr/testify/require" pd "github.com/tikv/pd/client" "go.etcd.io/etcd/tests/v3/integration" @@ -32,7 +32,7 @@ import ( ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/domain/infosync/info_test.go b/domain/infosync/info_test.go index 07b01bc03a7f9..2fa68c2906af6 100644 --- a/domain/infosync/info_test.go +++ b/domain/infosync/info_test.go @@ -29,15 +29,15 @@ import ( "github.com/pingcap/tidb/ddl/placement" "github.com/pingcap/tidb/ddl/util" "github.com/pingcap/tidb/parser/model" + "github.com/pingcap/tidb/testkit/testsetup" util2 "github.com/pingcap/tidb/util" - "github.com/pingcap/tidb/util/testbridge" "github.com/stretchr/testify/require" "go.etcd.io/etcd/tests/v3/integration" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/domain/main_test.go b/domain/main_test.go index 1896313761270..fc4bc11227206 100644 --- a/domain/main_test.go +++ b/domain/main_test.go @@ -17,12 +17,12 @@ package domain_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/errno/main_test.go b/errno/main_test.go index f0064431962cc..c50dc3cdbaa0e 100644 --- a/errno/main_test.go +++ b/errno/main_test.go @@ -18,10 +18,10 @@ import ( "os" "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() os.Exit(m.Run()) } diff --git a/executor/aggfuncs/main_test.go b/executor/aggfuncs/main_test.go index de63aaa396469..9092f6a465d77 100644 --- a/executor/aggfuncs/main_test.go +++ b/executor/aggfuncs/main_test.go @@ -17,12 +17,12 @@ package aggfuncs import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/executor/main_test.go b/executor/main_test.go index 0ce6dae10889e..98421e5962e72 100644 --- a/executor/main_test.go +++ b/executor/main_test.go @@ -23,7 +23,7 @@ import ( "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/testkit/testdata" "github.com/pingcap/tidb/testkit/testmain" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/tikv/client-go/v2/tikv" "go.uber.org/goleak" ) @@ -35,7 +35,7 @@ var executorSuiteData testdata.TestData var pointGetSuiteData testdata.TestData func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() testDataMap.LoadTestSuiteData("testdata", "agg_suite") testDataMap.LoadTestSuiteData("testdata", "executor_suite") testDataMap.LoadTestSuiteData("testdata", "prepare_suite") diff --git a/executor/oomtest/oom_test.go b/executor/oomtest/oom_test.go index 866f43f567587..85a10ed82326e 100644 --- a/executor/oomtest/oom_test.go +++ b/executor/oomtest/oom_test.go @@ -26,7 +26,7 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/testkit" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/stretchr/testify/require" "go.uber.org/goleak" "go.uber.org/zap" @@ -34,7 +34,7 @@ import ( ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() registerHook() domain.RunAutoAnalyze = false config.UpdateGlobal(func(conf *config.Config) { diff --git a/executor/seqtest/main_test.go b/executor/seqtest/main_test.go index 73f1353e98fc6..5a1e3d84813f4 100644 --- a/executor/seqtest/main_test.go +++ b/executor/seqtest/main_test.go @@ -17,13 +17,13 @@ package executor import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/tikv/client-go/v2/config" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() config.UpdateGlobal(func(conf *config.Config) { conf.TiKVClient.AsyncCommit.SafeWindow = 0 conf.TiKVClient.AsyncCommit.AllowedClockDrift = 0 diff --git a/expression/aggregation/main_test.go b/expression/aggregation/main_test.go index 40ea88a12c516..e144e769b2e6d 100644 --- a/expression/aggregation/main_test.go +++ b/expression/aggregation/main_test.go @@ -17,12 +17,12 @@ package aggregation import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/expression/main_test.go b/expression/main_test.go index 3c785e9a95f07..16fdf0574eb75 100644 --- a/expression/main_test.go +++ b/expression/main_test.go @@ -21,8 +21,8 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/testkit/testdata" "github.com/pingcap/tidb/testkit/testmain" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/pingcap/tidb/util/mock" - "github.com/pingcap/tidb/util/testbridge" "github.com/pingcap/tidb/util/timeutil" "github.com/stretchr/testify/require" "github.com/tikv/client-go/v2/tikv" @@ -32,7 +32,7 @@ import ( var testDataMap = make(testdata.BookKeeper) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() testmain.ShortCircuitForBench(m) config.UpdateGlobal(func(conf *config.Config) { diff --git a/go.mod b/go.mod index 970002e27a0e4..c3b455bc601e4 100644 --- a/go.mod +++ b/go.mod @@ -44,7 +44,6 @@ require ( github.com/opentracing/opentracing-go v1.2.0 github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 github.com/pingcap/badger v1.5.1-0.20220314162537-ab58fbf40580 - github.com/pingcap/check v0.0.0-20211026125417-57bd13f7b5f0 github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3 github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059 diff --git a/infoschema/main_test.go b/infoschema/main_test.go index 7ca5612d5f13d..9330b22b360ca 100644 --- a/infoschema/main_test.go +++ b/infoschema/main_test.go @@ -17,12 +17,12 @@ package infoschema import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/infoschema/perfschema/main_test.go b/infoschema/perfschema/main_test.go index acfdc80447866..43012b2c4dbde 100644 --- a/infoschema/perfschema/main_test.go +++ b/infoschema/perfschema/main_test.go @@ -17,12 +17,12 @@ package perfschema import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/kv/main_test.go b/kv/main_test.go index 9a4d9e0891574..e0b772c63768d 100644 --- a/kv/main_test.go +++ b/kv/main_test.go @@ -17,12 +17,12 @@ package kv import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), diff --git a/meta/autoid/main_test.go b/meta/autoid/main_test.go index 509401d81b105..c5dad759b65b1 100644 --- a/meta/autoid/main_test.go +++ b/meta/autoid/main_test.go @@ -17,12 +17,12 @@ package autoid_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/meta/main_test.go b/meta/main_test.go index 2b58b89851eae..1cfa29527043e 100644 --- a/meta/main_test.go +++ b/meta/main_test.go @@ -17,12 +17,12 @@ package meta import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), diff --git a/metrics/main_test.go b/metrics/main_test.go index d207a172d0eda..998921fbc3192 100644 --- a/metrics/main_test.go +++ b/metrics/main_test.go @@ -17,12 +17,12 @@ package metrics import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/owner/main_test.go b/owner/main_test.go index 1aff62becfd64..501cae73e4c5b 100644 --- a/owner/main_test.go +++ b/owner/main_test.go @@ -17,12 +17,12 @@ package owner import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/planner/cascades/main_test.go b/planner/cascades/main_test.go index ba181fa64df76..c135838a2a1fb 100644 --- a/planner/cascades/main_test.go +++ b/planner/cascades/main_test.go @@ -21,7 +21,7 @@ import ( "testing" "github.com/pingcap/tidb/testkit/testdata" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -30,7 +30,7 @@ var stringerSuiteData testdata.TestData var transformationRulesSuiteData testdata.TestData func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() flag.Parse() diff --git a/planner/core/main_test.go b/planner/core/main_test.go index 882db15a4a053..916d1943aa1f4 100644 --- a/planner/core/main_test.go +++ b/planner/core/main_test.go @@ -20,7 +20,7 @@ import ( "github.com/pingcap/tidb/testkit/testdata" "github.com/pingcap/tidb/testkit/testmain" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -29,7 +29,7 @@ var indexMergeSuiteData testdata.TestData var planSuiteUnexportedData testdata.TestData func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() flag.Parse() diff --git a/planner/funcdep/main_test.go b/planner/funcdep/main_test.go index 0a3b3828fda20..4387fad57e5b8 100644 --- a/planner/funcdep/main_test.go +++ b/planner/funcdep/main_test.go @@ -17,12 +17,12 @@ package funcdep import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/planner/implementation/main_test.go b/planner/implementation/main_test.go index 167a9c51880ac..f2ba762c1d88d 100644 --- a/planner/implementation/main_test.go +++ b/planner/implementation/main_test.go @@ -17,12 +17,12 @@ package implementation import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/planner/memo/main_test.go b/planner/memo/main_test.go index 404c80d45df11..bb84aa0af9800 100644 --- a/planner/memo/main_test.go +++ b/planner/memo/main_test.go @@ -17,12 +17,12 @@ package memo import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/planner/util/main_test.go b/planner/util/main_test.go index d78129a7fa1b3..1cb1f40846368 100644 --- a/planner/util/main_test.go +++ b/planner/util/main_test.go @@ -17,12 +17,12 @@ package util import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/plugin/conn_ip_example/main_test.go b/plugin/conn_ip_example/main_test.go index fb7995913f593..4847a1b134245 100644 --- a/plugin/conn_ip_example/main_test.go +++ b/plugin/conn_ip_example/main_test.go @@ -17,12 +17,12 @@ package main import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/plugin/main_test.go b/plugin/main_test.go index b6091ff667b40..6d55a790de4de 100644 --- a/plugin/main_test.go +++ b/plugin/main_test.go @@ -17,12 +17,12 @@ package plugin import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), diff --git a/privilege/privileges/main_test.go b/privilege/privileges/main_test.go index d530daf09bd56..efe96d5784161 100644 --- a/privilege/privileges/main_test.go +++ b/privilege/privileges/main_test.go @@ -18,7 +18,7 @@ import ( "testing" "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -30,7 +30,7 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("net/http.(*persistConn).writeLoop"), goleak.IgnoreTopFunction("internal/poll.runtime_pollWait"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() session.SetSchemaLease(0) session.DisableStats4Test() diff --git a/server/main_test.go b/server/main_test.go index 1d79b3caf2070..bff47f0dd7706 100644 --- a/server/main_test.go +++ b/server/main_test.go @@ -24,14 +24,14 @@ import ( "github.com/pingcap/tidb/metrics" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/store/mockstore/unistore" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" topsqlstate "github.com/pingcap/tidb/util/topsql/state" "github.com/tikv/client-go/v2/tikv" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() RunInGoTest = true // flag for NewServer to known it is running in test environment // Enable TopSQL for all test, and check the resource tag for each RPC request. diff --git a/session/main_test.go b/session/main_test.go index 92f96c57db956..674bb521f94a9 100644 --- a/session/main_test.go +++ b/session/main_test.go @@ -21,16 +21,15 @@ import ( "testing" "time" - "github.com/pingcap/check" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/testkit/testdata" "github.com/pingcap/tidb/testkit/testmain" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/sqlexec" - "github.com/pingcap/tidb/util/testbridge" "github.com/stretchr/testify/require" "github.com/tikv/client-go/v2/tikv" "go.uber.org/atomic" @@ -39,10 +38,15 @@ import ( var testDataMap = make(testdata.BookKeeper, 1) +var WithTiKV = flag.Bool("with-tikv", false, "workaroundGoCheckFlags: with-tikv") +var _ = flag.String("pd-addrs", "", "workaroundGoCheckFlags: pd-addrs") +var _ = flag.String("check.exclude", "", "workaroundGoCheckFlags: check.exclude") +var _ = flag.String("check.f", "", "workaroundGoCheckFlags: check.f") + func TestMain(m *testing.M) { testmain.ShortCircuitForBench(m) - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() flag.Parse() testDataMap.LoadTestSuiteData("testdata", "clustered_index_suite") @@ -76,10 +80,6 @@ func TestMain(m *testing.M) { goleak.VerifyTestMain(testmain.WrapTestingM(m, callback), opts...) } -func TestT(t *testing.T) { - check.TestingT(t) -} - func GetClusteredIndexSuiteData() testdata.TestData { return testDataMap["clustered_index_suite"] } diff --git a/session/schema_test.go b/session/schema_test.go index 171a8435298bb..559b50de81bc3 100644 --- a/session/schema_test.go +++ b/session/schema_test.go @@ -38,7 +38,7 @@ import ( ) func skipIfWithRealTiKV(t *testing.T) { - if *withTiKV { + if *session.WithTiKV { t.Skip("Schema tests has nothing to do with real tikv scenario") } } diff --git a/session/session_legacy_test.go b/session/session_legacy_test.go deleted file mode 100644 index a758dc026d167..0000000000000 --- a/session/session_legacy_test.go +++ /dev/null @@ -1,768 +0,0 @@ -// Copyright 2015 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 session_test - -import ( - "context" - "flag" - "fmt" - "net" - "strconv" - "time" - - . "github.com/pingcap/check" - "github.com/pingcap/errors" - "github.com/pingcap/tidb/config" - "github.com/pingcap/tidb/domain" - "github.com/pingcap/tidb/kv" - "github.com/pingcap/tidb/parser/auth" - "github.com/pingcap/tidb/parser/mysql" - "github.com/pingcap/tidb/parser/terror" - "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/sessionctx" - "github.com/pingcap/tidb/sessionctx/variable" - "github.com/pingcap/tidb/store/driver" - "github.com/pingcap/tidb/store/mockstore" - "github.com/pingcap/tidb/types" - "github.com/pingcap/tidb/util/testkit" - "github.com/pingcap/tidb/util/testleak" - "github.com/tikv/client-go/v2/testutils" - clientv3 "go.etcd.io/etcd/client/v3" - "google.golang.org/grpc" -) - -var withTiKV = flag.Bool("with-tikv", false, "run tests with TiKV cluster started. (not use the mock server)") - -var _ = flag.String("pd-addrs", "127.0.0.1:2379", "workaroundGoCheckFlags: pd-addrs") - -var _ = Suite(&testSessionSuite{}) - -type testSessionSuiteBase struct { - cluster testutils.Cluster - store kv.Storage - dom *domain.Domain -} - -type testSessionSuite struct { - testSessionSuiteBase -} - -func clearTiKVStorage(store kv.Storage) error { - txn, err := store.Begin() - if err != nil { - return errors.Trace(err) - } - iter, err := txn.Iter(nil, nil) - if err != nil { - return errors.Trace(err) - } - for iter.Valid() { - if err := txn.Delete(iter.Key()); err != nil { - return errors.Trace(err) - } - if err := iter.Next(); err != nil { - return errors.Trace(err) - } - } - return txn.Commit(context.Background()) -} - -func clearEtcdStorage(ebd kv.EtcdBackend) error { - endpoints, err := ebd.EtcdAddrs() - if err != nil { - return err - } - cli, err := clientv3.New(clientv3.Config{ - Endpoints: endpoints, - AutoSyncInterval: 30 * time.Second, - DialTimeout: 5 * time.Second, - DialOptions: []grpc.DialOption{ - grpc.WithBackoffMaxDelay(time.Second * 3), - }, - TLS: ebd.TLSConfig(), - }) - if err != nil { - return errors.Trace(err) - } - defer cli.Close() - - resp, err := cli.Get(context.Background(), "/tidb", clientv3.WithPrefix()) - if err != nil { - return errors.Trace(err) - } - for _, kv := range resp.Kvs { - if kv.Lease != 0 { - if _, err := cli.Revoke(context.Background(), clientv3.LeaseID(kv.Lease)); err != nil { - return errors.Trace(err) - } - } - } - _, err = cli.Delete(context.Background(), "/tidb", clientv3.WithPrefix()) - if err != nil { - return errors.Trace(err) - } - return nil -} - -func (s *testSessionSuiteBase) SetUpSuite(c *C) { - testleak.BeforeTest() - - if *withTiKV { - var d driver.TiKVDriver - config.UpdateGlobal(func(conf *config.Config) { - conf.TxnLocalLatches.Enabled = false - }) - store, err := d.Open("tikv://127.0.0.1:2379?disableGC=true") - c.Assert(err, IsNil) - err = clearTiKVStorage(store) - c.Assert(err, IsNil) - err = clearEtcdStorage(store.(kv.EtcdBackend)) - c.Assert(err, IsNil) - session.ResetStoreForWithTiKVTest(store) - s.store = store - } else { - store, err := mockstore.NewMockStore( - mockstore.WithClusterInspector(func(c testutils.Cluster) { - mockstore.BootstrapWithSingleStore(c) - s.cluster = c - }), - ) - c.Assert(err, IsNil) - s.store = store - session.DisableStats4Test() - } - - var err error - s.dom, err = session.BootstrapSession(s.store) - c.Assert(err, IsNil) -} - -func (s *testSessionSuiteBase) TearDownSuite(c *C) { - s.dom.Close() - s.store.Close() - testleak.AfterTest(c)() -} - -func (s *testSessionSuiteBase) TearDownTest(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - r := tk.MustQuery("show full tables") - for _, tb := range r.Rows() { - tableName := tb[0] - tableType := tb[1] - switch tableType { - case "VIEW": - tk.MustExec(fmt.Sprintf("drop view %v", tableName)) - case "BASE TABLE": - tk.MustExec(fmt.Sprintf("drop table %v", tableName)) - default: - panic(fmt.Sprintf("Unexpected table '%s' with type '%s'.", tableName, tableType)) - } - } -} - -func (s *testSessionSuite) TestQueryString(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - - tk.MustExec("create table mutil1 (a int);create table multi2 (a int)") - queryStr := tk.Se.Value(sessionctx.QueryString) - c.Assert(queryStr, Equals, "create table multi2 (a int)") - - // Test execution of DDL through the "ExecutePreparedStmt" interface. - _, err := tk.Se.Execute(context.Background(), "use test;") - c.Assert(err, IsNil) - _, err = tk.Se.Execute(context.Background(), "CREATE TABLE t (id bigint PRIMARY KEY, age int)") - c.Assert(err, IsNil) - _, err = tk.Se.Execute(context.Background(), "show create table t") - c.Assert(err, IsNil) - id, _, _, err := tk.Se.PrepareStmt("CREATE TABLE t2(id bigint PRIMARY KEY, age int)") - c.Assert(err, IsNil) - params := []types.Datum{} - _, err = tk.Se.ExecutePreparedStmt(context.Background(), id, params) - c.Assert(err, IsNil) - qs := tk.Se.Value(sessionctx.QueryString) - c.Assert(qs.(string), Equals, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)") - - // Test execution of DDL through the "Execute" interface. - _, err = tk.Se.Execute(context.Background(), "use test;") - c.Assert(err, IsNil) - _, err = tk.Se.Execute(context.Background(), "drop table t2") - c.Assert(err, IsNil) - _, err = tk.Se.Execute(context.Background(), "prepare stmt from 'CREATE TABLE t2(id bigint PRIMARY KEY, age int)'") - c.Assert(err, IsNil) - _, err = tk.Se.Execute(context.Background(), "execute stmt") - c.Assert(err, IsNil) - qs = tk.Se.Value(sessionctx.QueryString) - c.Assert(qs.(string), Equals, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)") -} - -func (s *testSessionSuite) TestAffectedRows(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(id TEXT)") - tk.MustExec(`INSERT INTO t VALUES ("a");`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 1) - tk.MustExec(`INSERT INTO t VALUES ("b");`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 1) - tk.MustExec(`UPDATE t set id = 'c' where id = 'a';`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 1) - tk.MustExec(`UPDATE t set id = 'a' where id = 'a';`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 0) - tk.MustQuery(`SELECT * from t`).Check(testkit.Rows("c", "b")) - c.Assert(int(tk.Se.AffectedRows()), Equals, 0) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int, data int)") - tk.MustExec(`INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) - tk.MustExec(`UPDATE t set id = 1 where data = 0;`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 1) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int, c1 timestamp);") - tk.MustExec(`insert t(id) values(1);`) - tk.MustExec(`UPDATE t set id = 1 where id = 1;`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 0) - - // With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, - // 2 if an existing row is updated, and 0 if an existing row is set to its current values. - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (c1 int PRIMARY KEY, c2 int);") - tk.MustExec(`insert t values(1, 1);`) - tk.MustExec(`insert into t values (1, 1) on duplicate key update c2=2;`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 2) - tk.MustExec(`insert into t values (1, 1) on duplicate key update c2=2;`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 0) - tk.MustExec("drop table if exists test") - createSQL := `CREATE TABLE test ( - id VARCHAR(36) PRIMARY KEY NOT NULL, - factor INTEGER NOT NULL DEFAULT 2);` - tk.MustExec(createSQL) - insertSQL := `INSERT INTO test(id) VALUES('id') ON DUPLICATE KEY UPDATE factor=factor+3;` - tk.MustExec(insertSQL) - c.Assert(int(tk.Se.AffectedRows()), Equals, 1) - tk.MustExec(insertSQL) - c.Assert(int(tk.Se.AffectedRows()), Equals, 2) - tk.MustExec(insertSQL) - c.Assert(int(tk.Se.AffectedRows()), Equals, 2) - - tk.Se.SetClientCapability(mysql.ClientFoundRows) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int, data int)") - tk.MustExec(`INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) - tk.MustExec(`UPDATE t set id = 1 where data = 0;`) - c.Assert(int(tk.Se.AffectedRows()), Equals, 2) -} - -// TestRowLock . See http://dev.mysql.com/doc/refman/5.7/en/commit.html. -func (s *testSessionSuite) TestRowLock(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - tk1 := testkit.NewTestKitWithInit(c, s.store) - tk2 := testkit.NewTestKitWithInit(c, s.store) - - tk.MustExec("drop table if exists t") - txn, err := tk.Se.Txn(true) - c.Assert(kv.ErrInvalidTxn.Equal(err), IsTrue) - c.Assert(txn.Valid(), IsFalse) - tk.MustExec("create table t (c1 int, c2 int, c3 int)") - tk.MustExec("insert t values (11, 2, 3)") - tk.MustExec("insert t values (12, 2, 3)") - tk.MustExec("insert t values (13, 2, 3)") - - tk1.MustExec("set @@tidb_disable_txn_auto_retry = 0") - tk1.MustExec("begin") - tk1.MustExec("update t set c2=21 where c1=11") - - tk2.MustExec("begin") - tk2.MustExec("update t set c2=211 where c1=11") - tk2.MustExec("commit") - - // tk1 will retry and the final value is 21 - tk1.MustExec("commit") - - // Check the result is correct - tk.MustQuery("select c2 from t where c1=11").Check(testkit.Rows("21")) - - tk1.MustExec("begin") - tk1.MustExec("update t set c2=21 where c1=11") - - tk2.MustExec("begin") - tk2.MustExec("update t set c2=22 where c1=12") - tk2.MustExec("commit") - - tk1.MustExec("commit") -} - -// TestAutocommit . See https://dev.mysql.com/doc/internals/en/status-flags.html -func (s *testSessionSuite) TestAutocommit(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - - tk.MustExec("drop table if exists t;") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Greater, 0) - tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Greater, 0) - tk.MustExec("insert t values ()") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Greater, 0) - tk.MustExec("begin") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Greater, 0) - tk.MustExec("insert t values ()") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Greater, 0) - tk.MustExec("drop table if exists t") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Greater, 0) - - tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Greater, 0) - tk.MustExec("set autocommit=0") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Equals, 0) - tk.MustExec("insert t values ()") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Equals, 0) - tk.MustExec("commit") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Equals, 0) - tk.MustExec("drop table if exists t") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Equals, 0) - tk.MustExec("set autocommit='On'") - c.Assert(int(tk.Se.Status()&mysql.ServerStatusAutocommit), Greater, 0) - - // When autocommit is 0, transaction start ts should be the first *valid* - // statement, rather than *any* statement. - tk.MustExec("create table t (id int)") - tk.MustExec("set @@autocommit = 0") - tk.MustExec("rollback") - tk.MustExec("set @@autocommit = 0") - tk1 := testkit.NewTestKitWithInit(c, s.store) - tk1.MustExec("insert into t select 1") - tk.MustQuery("select * from t").Check(testkit.Rows("1")) - - // TODO: MySQL compatibility for setting global variable. - // tk.MustExec("begin") - // tk.MustExec("insert into t values (42)") - // tk.MustExec("set @@global.autocommit = 1") - // tk.MustExec("rollback") - // tk.MustQuery("select count(*) from t where id = 42").Check(testkit.Rows("0")) - // Even the transaction is rollbacked, the set statement succeed. - // tk.MustQuery("select @@global.autocommit").Rows("1") -} - -// TestTxnLazyInitialize tests that when autocommit = 0, not all statement starts -// a new transaction. -func (s *testSessionSuite) TestTxnLazyInitialize(c *C) { - testTxnLazyInitialize(s, c, false) - testTxnLazyInitialize(s, c, true) -} - -func testTxnLazyInitialize(s *testSessionSuite, c *C, isPessimistic bool) { - tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int)") - if isPessimistic { - tk.MustExec("set tidb_txn_mode = 'pessimistic'") - } - - tk.MustExec("set @@autocommit = 0") - _, err := tk.Se.Txn(true) - c.Assert(kv.ErrInvalidTxn.Equal(err), IsTrue) - txn, err := tk.Se.Txn(false) - c.Assert(err, IsNil) - c.Assert(txn.Valid(), IsFalse) - tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) - tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) - - // Those statement should not start a new transaction automacally. - tk.MustQuery("select 1") - tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) - - tk.MustExec("set @@tidb_general_log = 0") - tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) - - tk.MustQuery("explain select * from t") - tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) - - // Begin statement should start a new transaction. - tk.MustExec("begin") - txn, err = tk.Se.Txn(false) - c.Assert(err, IsNil) - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("rollback") - - tk.MustExec("select * from t") - txn, err = tk.Se.Txn(false) - c.Assert(err, IsNil) - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("rollback") - - tk.MustExec("insert into t values (1)") - txn, err = tk.Se.Txn(false) - c.Assert(err, IsNil) - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("rollback") -} - -func (s *testSessionSuite) TestGlobalVarAccessor(c *C) { - varName := "max_allowed_packet" - varValue := strconv.FormatUint(variable.DefMaxAllowedPacket, 10) // This is the default value for max_allowed_packet - - // The value of max_allowed_packet should be a multiple of 1024, - // so the setting of varValue1 and varValue2 would be truncated to varValue0 - varValue0 := "4194304" - varValue1 := "4194305" - varValue2 := "4194306" - - tk := testkit.NewTestKitWithInit(c, s.store) - se := tk.Se.(variable.GlobalVarAccessor) - // Get globalSysVar twice and get the same value - v, err := se.GetGlobalSysVar(varName) - c.Assert(err, IsNil) - c.Assert(v, Equals, varValue) - v, err = se.GetGlobalSysVar(varName) - c.Assert(err, IsNil) - c.Assert(v, Equals, varValue) - // Set global var to another value - err = se.SetGlobalSysVar(varName, varValue1) - c.Assert(err, IsNil) - v, err = se.GetGlobalSysVar(varName) - c.Assert(err, IsNil) - c.Assert(v, Equals, varValue0) - c.Assert(tk.Se.CommitTxn(context.TODO()), IsNil) - - tk1 := testkit.NewTestKitWithInit(c, s.store) - se1 := tk1.Se.(variable.GlobalVarAccessor) - v, err = se1.GetGlobalSysVar(varName) - c.Assert(err, IsNil) - c.Assert(v, Equals, varValue0) - err = se1.SetGlobalSysVar(varName, varValue2) - c.Assert(err, IsNil) - v, err = se1.GetGlobalSysVar(varName) - c.Assert(err, IsNil) - c.Assert(v, Equals, varValue0) - c.Assert(tk1.Se.CommitTxn(context.TODO()), IsNil) - - // Make sure the change is visible to any client that accesses that global variable. - v, err = se.GetGlobalSysVar(varName) - c.Assert(err, IsNil) - c.Assert(v, Equals, varValue0) - - // For issue 10955, make sure the new session load `max_execution_time` into sessionVars. - tk1.MustExec("set @@global.max_execution_time = 100") - tk2 := testkit.NewTestKitWithInit(c, s.store) - c.Assert(tk2.Se.GetSessionVars().MaxExecutionTime, Equals, uint64(100)) - tk1.MustExec("set @@global.max_execution_time = 0") - - result := tk.MustQuery("show global variables where variable_name='sql_select_limit';") - result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) - result = tk.MustQuery("show session variables where variable_name='sql_select_limit';") - result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) - tk.MustExec("set session sql_select_limit=100000000000;") - result = tk.MustQuery("show global variables where variable_name='sql_select_limit';") - result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) - result = tk.MustQuery("show session variables where variable_name='sql_select_limit';") - result.Check(testkit.Rows("sql_select_limit 100000000000")) - tk.MustExec("set @@global.sql_select_limit = 1") - result = tk.MustQuery("show global variables where variable_name='sql_select_limit';") - result.Check(testkit.Rows("sql_select_limit 1")) - tk.MustExec("set @@global.sql_select_limit = default") - result = tk.MustQuery("show global variables where variable_name='sql_select_limit';") - result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) - - result = tk.MustQuery("select @@global.autocommit;") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select @@autocommit;") - result.Check(testkit.Rows("1")) - tk.MustExec("set @@global.autocommit = 0;") - result = tk.MustQuery("select @@global.autocommit;") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("select @@autocommit;") - result.Check(testkit.Rows("1")) - tk.MustExec("set @@global.autocommit=1") - - _, err = tk.Exec("set global time_zone = 'timezone'") - c.Assert(err, NotNil) - c.Assert(terror.ErrorEqual(err, variable.ErrUnknownTimeZone), IsTrue) -} - -func (s *testSessionSuite) TestUpgradeSysvars(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - se := tk.Se.(variable.GlobalVarAccessor) - - // Set the global var to a non canonical form of the value - // i.e. implying that it was set from an earlier version of TiDB. - - tk.MustExec(`REPLACE INTO mysql.global_variables (variable_name, variable_value) VALUES ('tidb_enable_noop_functions', '0')`) - domain.GetDomain(tk.Se).NotifyUpdateSysVarCache() // update cache - v, err := se.GetGlobalSysVar("tidb_enable_noop_functions") - c.Assert(err, IsNil) - c.Assert(v, Equals, "OFF") - - // Set the global var to "" which is the invalid version of this from TiDB 4.0.16 - // the err is quashed by the GetGlobalSysVar, and the default value is restored. - // This helps callers of GetGlobalSysVar(), which can't individually be expected - // to handle upgrade/downgrade issues correctly. - - tk.MustExec(`REPLACE INTO mysql.global_variables (variable_name, variable_value) VALUES ('rpl_semi_sync_slave_enabled', '')`) - domain.GetDomain(tk.Se).NotifyUpdateSysVarCache() // update cache - v, err = se.GetGlobalSysVar("rpl_semi_sync_slave_enabled") - c.Assert(err, IsNil) - c.Assert(v, Equals, "OFF") // the default value is restored. - result := tk.MustQuery("SHOW VARIABLES LIKE 'rpl_semi_sync_slave_enabled'") - result.Check(testkit.Rows("rpl_semi_sync_slave_enabled OFF")) - - // Ensure variable out of range is converted to in range after upgrade. - // This further helps for https://github.com/pingcap/tidb/pull/28842 - - tk.MustExec(`REPLACE INTO mysql.global_variables (variable_name, variable_value) VALUES ('tidb_executor_concurrency', '999')`) - domain.GetDomain(tk.Se).NotifyUpdateSysVarCache() // update cache - v, err = se.GetGlobalSysVar("tidb_executor_concurrency") - c.Assert(err, IsNil) - c.Assert(v, Equals, "256") // the max value is restored. - - // Handle the case of a completely bogus value from an earlier version of TiDB. - // This could be the case if an ENUM sysvar removes a value. - - tk.MustExec(`REPLACE INTO mysql.global_variables (variable_name, variable_value) VALUES ('tidb_enable_noop_functions', 'SOMEVAL')`) - domain.GetDomain(tk.Se).NotifyUpdateSysVarCache() // update cache - v, err = se.GetGlobalSysVar("tidb_enable_noop_functions") - c.Assert(err, IsNil) - c.Assert(v, Equals, "OFF") // the default value is restored. -} - -func (s *testSessionSuite) TestSetInstanceSysvarBySetGlobalSysVar(c *C) { - varName := "tidb_general_log" - defaultValue := "OFF" // This is the default value for tidb_general_log - - tk := testkit.NewTestKitWithInit(c, s.store) - se := tk.Se.(variable.GlobalVarAccessor) - - // Get globalSysVar twice and get the same default value - v, err := se.GetGlobalSysVar(varName) - c.Assert(err, IsNil) - c.Assert(v, Equals, defaultValue) - v, err = se.GetGlobalSysVar(varName) - c.Assert(err, IsNil) - c.Assert(v, Equals, defaultValue) - - // session.GetGlobalSysVar would not get the value which session.SetGlobalSysVar writes, - // because SetGlobalSysVar calls SetGlobalFromHook, which uses TiDBGeneralLog's SetGlobal, - // but GetGlobalSysVar could not access TiDBGeneralLog's GetGlobal. - - // set to "1" - err = se.SetGlobalSysVar(varName, "ON") - c.Assert(err, IsNil) - v, err = se.GetGlobalSysVar(varName) - tk.MustQuery("select @@global.tidb_general_log").Check(testkit.Rows("1")) - c.Assert(err, IsNil) - c.Assert(v, Equals, defaultValue) - - // set back to "0" - err = se.SetGlobalSysVar(varName, defaultValue) - c.Assert(err, IsNil) - v, err = se.GetGlobalSysVar(varName) - tk.MustQuery("select @@global.tidb_general_log").Check(testkit.Rows("0")) - c.Assert(err, IsNil) - c.Assert(v, Equals, defaultValue) -} - -func (s *testSessionSuite) TestMatchIdentity(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("CREATE USER `useridentity`@`%`") - tk.MustExec("CREATE USER `useridentity`@`localhost`") - tk.MustExec("CREATE USER `useridentity`@`192.168.1.1`") - tk.MustExec("CREATE USER `useridentity`@`example.com`") - - // The MySQL matching rule is most specific to least specific. - // So if I log in from 192.168.1.1 I should match that entry always. - identity, err := tk.Se.MatchIdentity("useridentity", "192.168.1.1") - c.Assert(err, IsNil) - c.Assert(identity.Username, Equals, "useridentity") - c.Assert(identity.Hostname, Equals, "192.168.1.1") - - // If I log in from localhost, I should match localhost - identity, err = tk.Se.MatchIdentity("useridentity", "localhost") - c.Assert(err, IsNil) - c.Assert(identity.Username, Equals, "useridentity") - c.Assert(identity.Hostname, Equals, "localhost") - - // If I log in from 192.168.1.2 I should match wildcard. - identity, err = tk.Se.MatchIdentity("useridentity", "192.168.1.2") - c.Assert(err, IsNil) - c.Assert(identity.Username, Equals, "useridentity") - c.Assert(identity.Hostname, Equals, "%") - - identity, err = tk.Se.MatchIdentity("useridentity", "127.0.0.1") - c.Assert(err, IsNil) - c.Assert(identity.Username, Equals, "useridentity") - c.Assert(identity.Hostname, Equals, "localhost") - - // This uses the lookup of example.com to get an IP address. - // We then login with that IP address, but expect it to match the example.com - // entry in the privileges table (by reverse lookup). - ips, err := net.LookupHost("example.com") - c.Assert(err, IsNil) - identity, err = tk.Se.MatchIdentity("useridentity", ips[0]) - c.Assert(err, IsNil) - c.Assert(identity.Username, Equals, "useridentity") - // FIXME: we *should* match example.com instead - // as long as skip-name-resolve is not set (DEFAULT) - c.Assert(identity.Hostname, Equals, "%") -} - -func (s *testSessionSuite) TestGetSysVariables(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - - // Test ScopeSession - tk.MustExec("select @@warning_count") - tk.MustExec("select @@session.warning_count") - tk.MustExec("select @@local.warning_count") - _, err := tk.Exec("select @@global.warning_count") - c.Assert(terror.ErrorEqual(err, variable.ErrIncorrectScope), IsTrue, Commentf("err %v", err)) - - // Test ScopeGlobal - tk.MustExec("select @@max_connections") - tk.MustExec("select @@global.max_connections") - _, err = tk.Exec("select @@session.max_connections") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[variable:1238]Variable 'max_connections' is a GLOBAL variable") - _, err = tk.Exec("select @@local.max_connections") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[variable:1238]Variable 'max_connections' is a GLOBAL variable") - - // Test ScopeNone - tk.MustExec("select @@performance_schema_max_mutex_classes") - tk.MustExec("select @@global.performance_schema_max_mutex_classes") - // For issue 19524, test - tk.MustExec("select @@session.performance_schema_max_mutex_classes") - tk.MustExec("select @@local.performance_schema_max_mutex_classes") - - _, err = tk.Exec("select @@global.last_insert_id") - c.Assert(err, NotNil) - c.Assert(err.Error(), Equals, "[variable:1238]Variable 'last_insert_id' is a SESSION variable") -} - -// TestInTrans . See https://dev.mysql.com/doc/internals/en/status-flags.html -func (s *testSessionSuite) TestInTrans(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") - tk.MustExec("insert t values ()") - tk.MustExec("begin") - txn, err := tk.Se.Txn(true) - c.Assert(err, IsNil) - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("insert t values ()") - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("drop table if exists t;") - c.Assert(txn.Valid(), IsFalse) - tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") - c.Assert(txn.Valid(), IsFalse) - tk.MustExec("insert t values ()") - c.Assert(txn.Valid(), IsFalse) - tk.MustExec("commit") - tk.MustExec("insert t values ()") - - tk.MustExec("set autocommit=0") - tk.MustExec("begin") - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("insert t values ()") - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("commit") - c.Assert(txn.Valid(), IsFalse) - tk.MustExec("insert t values ()") - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("commit") - c.Assert(txn.Valid(), IsFalse) - - tk.MustExec("set autocommit=1") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") - tk.MustExec("begin") - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("insert t values ()") - c.Assert(txn.Valid(), IsTrue) - tk.MustExec("rollback") - c.Assert(txn.Valid(), IsFalse) -} - -func (s *testSessionSuite) TestSession(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("ROLLBACK;") - tk.Se.Close() -} - -func (s *testSessionSuite) TestSessionAuth(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "Any not exist username with zero password!", Hostname: "anyhost"}, []byte(""), []byte("")), IsFalse) -} - -func (s *testSessionSuite) TestLastInsertID(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - // insert - tk.MustExec("create table t (c1 int not null auto_increment, c2 int, PRIMARY KEY (c1))") - tk.MustExec("insert into t set c2 = 11") - tk.MustQuery("select last_insert_id()").Check(testkit.Rows("1")) - - tk.MustExec("insert into t (c2) values (22), (33), (44)") - tk.MustQuery("select last_insert_id()").Check(testkit.Rows("2")) - - tk.MustExec("insert into t (c1, c2) values (10, 55)") - tk.MustQuery("select last_insert_id()").Check(testkit.Rows("2")) - - // replace - tk.MustExec("replace t (c2) values(66)") - tk.MustQuery("select * from t").Check(testkit.Rows("1 11", "2 22", "3 33", "4 44", "10 55", "11 66")) - tk.MustQuery("select last_insert_id()").Check(testkit.Rows("11")) - - // update - tk.MustExec("update t set c1=last_insert_id(c1 + 100)") - tk.MustQuery("select * from t").Check(testkit.Rows("101 11", "102 22", "103 33", "104 44", "110 55", "111 66")) - tk.MustQuery("select last_insert_id()").Check(testkit.Rows("111")) - tk.MustExec("insert into t (c2) values (77)") - tk.MustQuery("select last_insert_id()").Check(testkit.Rows("112")) - - // drop - tk.MustExec("drop table t") - tk.MustQuery("select last_insert_id()").Check(testkit.Rows("112")) - - tk.MustExec("create table t (c2 int, c3 int, c1 int not null auto_increment, PRIMARY KEY (c1))") - tk.MustExec("insert into t set c2 = 30") - - // insert values - lastInsertID := tk.Se.LastInsertID() - tk.MustExec("prepare stmt1 from 'insert into t (c2) values (?)'") - tk.MustExec("set @v1=10") - tk.MustExec("set @v2=20") - tk.MustExec("execute stmt1 using @v1") - tk.MustExec("execute stmt1 using @v2") - tk.MustExec("deallocate prepare stmt1") - currLastInsertID := tk.Se.GetSessionVars().StmtCtx.PrevLastInsertID - tk.MustQuery("select c1 from t where c2 = 20").Check(testkit.Rows(fmt.Sprint(currLastInsertID))) - c.Assert(lastInsertID+2, Equals, currLastInsertID) -} - -func (s *testSessionSuite) TestBinaryReadOnly(c *C) { - tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("create table t (i int key)") - id, _, _, err := tk.Se.PrepareStmt("select i from t where i = ?") - c.Assert(err, IsNil) - id2, _, _, err := tk.Se.PrepareStmt("insert into t values (?)") - c.Assert(err, IsNil) - tk.MustExec("set autocommit = 0") - tk.MustExec("set tidb_disable_txn_auto_retry = 0") - _, err = tk.Se.ExecutePreparedStmt(context.Background(), id, []types.Datum{types.NewDatum(1)}) - c.Assert(err, IsNil) - c.Assert(session.GetHistory(tk.Se).Count(), Equals, 0) - tk.MustExec("insert into t values (1)") - c.Assert(session.GetHistory(tk.Se).Count(), Equals, 1) - _, err = tk.Se.ExecutePreparedStmt(context.Background(), id2, []types.Datum{types.NewDatum(2)}) - c.Assert(err, IsNil) - c.Assert(session.GetHistory(tk.Se).Count(), Equals, 2) - tk.MustExec("commit") -} diff --git a/sessionctx/binloginfo/main_test.go b/sessionctx/binloginfo/main_test.go index 51a97bee98a96..a6d097b6a99c5 100644 --- a/sessionctx/binloginfo/main_test.go +++ b/sessionctx/binloginfo/main_test.go @@ -17,12 +17,12 @@ package binloginfo import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/sessionctx/main_test.go b/sessionctx/main_test.go index 91943d5726ca9..ecaf2dcebe95a 100644 --- a/sessionctx/main_test.go +++ b/sessionctx/main_test.go @@ -17,12 +17,12 @@ package sessionctx import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/sessionctx/stmtctx/main_test.go b/sessionctx/stmtctx/main_test.go index 444f2610ba5a8..d9ea6f6a898e7 100644 --- a/sessionctx/stmtctx/main_test.go +++ b/sessionctx/stmtctx/main_test.go @@ -17,12 +17,12 @@ package stmtctx import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/sessionctx/variable/main_test.go b/sessionctx/variable/main_test.go index 3d1f5cabbee70..977294bdc70d9 100644 --- a/sessionctx/variable/main_test.go +++ b/sessionctx/variable/main_test.go @@ -17,12 +17,12 @@ package variable import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/sessiontxn/staleread/processor_test.go b/sessiontxn/staleread/processor_test.go index 44fca02cd70a8..0e399d3b78760 100644 --- a/sessiontxn/staleread/processor_test.go +++ b/sessiontxn/staleread/processor_test.go @@ -28,7 +28,7 @@ import ( "github.com/pingcap/tidb/sessiontxn/staleread" "github.com/pingcap/tidb/table/temptable" "github.com/pingcap/tidb/testkit" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/stretchr/testify/require" "github.com/tikv/client-go/v2/oracle" "go.uber.org/goleak" @@ -39,7 +39,7 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m, opts...) } diff --git a/sessiontxn/txn_context_test.go b/sessiontxn/txn_context_test.go index 0aec44f8a6ee1..562005ed782a4 100644 --- a/sessiontxn/txn_context_test.go +++ b/sessiontxn/txn_context_test.go @@ -26,13 +26,13 @@ import ( "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessiontxn" "github.com/pingcap/tidb/testkit" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/stretchr/testify/require" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/statistics/handle/main_test.go b/statistics/handle/main_test.go index 1822c3b52eb27..b6bf59c2e27c3 100644 --- a/statistics/handle/main_test.go +++ b/statistics/handle/main_test.go @@ -17,7 +17,7 @@ package handle import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -27,6 +27,6 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m, opts...) } diff --git a/statistics/main_test.go b/statistics/main_test.go index 3fd1cfda4d8ef..b790b0e63076a 100644 --- a/statistics/main_test.go +++ b/statistics/main_test.go @@ -23,8 +23,8 @@ import ( "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/testkit/testdata" "github.com/pingcap/tidb/testkit/testmain" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/pingcap/tidb/types" - "github.com/pingcap/tidb/util/testbridge" "github.com/stretchr/testify/require" "go.uber.org/goleak" ) @@ -32,7 +32,7 @@ import ( var testDataMap = make(testdata.BookKeeper, 3) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() if !flag.Parsed() { flag.Parse() diff --git a/store/copr/main_test.go b/store/copr/main_test.go index 934a55c6354fd..9e9e2a02410e4 100644 --- a/store/copr/main_test.go +++ b/store/copr/main_test.go @@ -18,7 +18,7 @@ import ( "testing" "time" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -34,7 +34,7 @@ func (m *main) Run() int { } func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/pingcap/goleveldb/leveldb.(*DB).mpoolDrain"), goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), diff --git a/store/driver/error/error_test.go b/store/driver/error/error_test.go index 9af54d70dfa96..a8c2c6ddc9152 100644 --- a/store/driver/error/error_test.go +++ b/store/driver/error/error_test.go @@ -19,14 +19,14 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/parser/terror" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/stretchr/testify/assert" tikverr "github.com/tikv/client-go/v2/error" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/driver/main_test.go b/store/driver/main_test.go index 6927510cbc898..7da33743e5b2f 100644 --- a/store/driver/main_test.go +++ b/store/driver/main_test.go @@ -25,7 +25,7 @@ import ( "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/store/copr" "github.com/pingcap/tidb/store/mockstore/unistore" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/stretchr/testify/require" "github.com/tikv/client-go/v2/tikv" "go.uber.org/goleak" @@ -37,7 +37,7 @@ var ( ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() tikv.EnableFailpoints() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), diff --git a/store/driver/txn/main_test.go b/store/driver/txn/main_test.go index 8d93e31420d40..ad777bb5af019 100644 --- a/store/driver/txn/main_test.go +++ b/store/driver/txn/main_test.go @@ -17,12 +17,12 @@ package txn import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/gcworker/main_test.go b/store/gcworker/main_test.go index f6532f7b0a855..4451f16783160 100644 --- a/store/gcworker/main_test.go +++ b/store/gcworker/main_test.go @@ -19,13 +19,13 @@ import ( "time" "github.com/pingcap/tidb/testkit/testmain" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/tikv/client-go/v2/tikv" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() tikv.EnableFailpoints() opts := []goleak.Option{ goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/helper/main_test.go b/store/helper/main_test.go index 75cdb1d50f8ac..9c95a1fdbd9cf 100644 --- a/store/helper/main_test.go +++ b/store/helper/main_test.go @@ -17,12 +17,12 @@ package helper import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/main_test.go b/store/main_test.go index 0579ea229d2a8..3c4d13ad381b8 100644 --- a/store/main_test.go +++ b/store/main_test.go @@ -17,12 +17,12 @@ package store import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/mockstore/main_test.go b/store/mockstore/main_test.go index 32ba74a6a98d2..daf0e059f2572 100644 --- a/store/mockstore/main_test.go +++ b/store/mockstore/main_test.go @@ -19,12 +19,12 @@ import ( "time" "github.com/pingcap/tidb/testkit/testmain" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() callback := func(i int) int { // wait for leveldb to close, leveldb will be closed in one second time.Sleep(time.Second) diff --git a/store/mockstore/mockcopr/main_test.go b/store/mockstore/mockcopr/main_test.go index 1aef4b1027883..fa7a0a1e97df9 100644 --- a/store/mockstore/mockcopr/main_test.go +++ b/store/mockstore/mockcopr/main_test.go @@ -19,12 +19,12 @@ import ( "time" "github.com/pingcap/tidb/testkit/testmain" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/mockstore/unistore/cophandler/main_test.go b/store/mockstore/unistore/cophandler/main_test.go index 420cf1f476f89..0cd6c13b8cba0 100644 --- a/store/mockstore/unistore/cophandler/main_test.go +++ b/store/mockstore/unistore/cophandler/main_test.go @@ -17,12 +17,12 @@ package cophandler import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/mockstore/unistore/lockstore/main_test.go b/store/mockstore/unistore/lockstore/main_test.go index fdef458e79cb9..bc28743788d54 100644 --- a/store/mockstore/unistore/lockstore/main_test.go +++ b/store/mockstore/unistore/lockstore/main_test.go @@ -17,12 +17,12 @@ package lockstore import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/mockstore/unistore/main_test.go b/store/mockstore/unistore/main_test.go index adc65105d06c2..b69415466734e 100644 --- a/store/mockstore/unistore/main_test.go +++ b/store/mockstore/unistore/main_test.go @@ -17,12 +17,12 @@ package unistore import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/mockstore/unistore/tikv/main_test.go b/store/mockstore/unistore/tikv/main_test.go index 94e4abe33e2e4..07b836d7fd874 100644 --- a/store/mockstore/unistore/tikv/main_test.go +++ b/store/mockstore/unistore/tikv/main_test.go @@ -17,12 +17,12 @@ package tikv import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/store/mockstore/unistore/util/lockwaiter/main_test.go b/store/mockstore/unistore/util/lockwaiter/main_test.go index 19d1b574a56b3..470e33909b5b0 100644 --- a/store/mockstore/unistore/util/lockwaiter/main_test.go +++ b/store/mockstore/unistore/util/lockwaiter/main_test.go @@ -17,12 +17,12 @@ package lockwaiter import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/structure/main_test.go b/structure/main_test.go index d4617b9cbfccb..12e8019bf3e47 100644 --- a/structure/main_test.go +++ b/structure/main_test.go @@ -17,12 +17,12 @@ package structure import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/table/main_test.go b/table/main_test.go index d12ef390d9741..79f1eaa5a9a71 100644 --- a/table/main_test.go +++ b/table/main_test.go @@ -17,12 +17,12 @@ package table import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/table/tables/main_test.go b/table/tables/main_test.go index eb29416fb0b00..63a616eeb69b0 100644 --- a/table/tables/main_test.go +++ b/table/tables/main_test.go @@ -17,12 +17,12 @@ package tables_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/table/temptable/main_test.go b/table/temptable/main_test.go index 48b91db00c838..96c09feaeaec9 100644 --- a/table/temptable/main_test.go +++ b/table/temptable/main_test.go @@ -26,9 +26,9 @@ import ( "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/table" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/mock" - "github.com/pingcap/tidb/util/testbridge" "github.com/stretchr/testify/require" "go.uber.org/goleak" ) @@ -39,7 +39,7 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m, opts...) } diff --git a/tablecodec/main_test.go b/tablecodec/main_test.go index 6b0a750cfa958..ded843a887c9c 100644 --- a/tablecodec/main_test.go +++ b/tablecodec/main_test.go @@ -17,12 +17,12 @@ package tablecodec import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/tablecodec/rowindexcodec/main_test.go b/tablecodec/rowindexcodec/main_test.go index 745b45d45a929..fed13e1f46a21 100644 --- a/tablecodec/rowindexcodec/main_test.go +++ b/tablecodec/rowindexcodec/main_test.go @@ -17,12 +17,12 @@ package rowindexcodec import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/telemetry/cte_test/cte_test.go b/telemetry/cte_test/cte_test.go index 4a07a3e9255ee..9ad9e40674a35 100644 --- a/telemetry/cte_test/cte_test.go +++ b/telemetry/cte_test/cte_test.go @@ -26,14 +26,14 @@ import ( "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/telemetry" "github.com/pingcap/tidb/testkit" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/stretchr/testify/require" "go.etcd.io/etcd/tests/v3/integration" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/telemetry/main_test.go b/telemetry/main_test.go index 9e9d8fd2c4389..c3d817ebeabee 100644 --- a/telemetry/main_test.go +++ b/telemetry/main_test.go @@ -17,7 +17,7 @@ package telemetry import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -27,7 +27,7 @@ var ( ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/testbridge/bridge.go b/testkit/testsetup/bridge.go similarity index 66% rename from util/testbridge/bridge.go rename to testkit/testsetup/bridge.go index c761848fbce66..f2096fa8c947e 100644 --- a/util/testbridge/bridge.go +++ b/testkit/testsetup/bridge.go @@ -14,10 +14,9 @@ //go:build !codes -package testbridge +package testsetup import ( - "flag" "fmt" "os" @@ -28,24 +27,9 @@ import ( // SetupForCommonTest runs before all the tests. func SetupForCommonTest() { - workaroundGoCheckFlags() applyOSLogLevel() } -// workaroundGoCheckFlags registers flags of go-check for pkg does not import go-check -// to workaround the go-check flags passed in Makefile. -// -// TODO: Remove this function when the migration from go-check to testify[1] is done. -// [1] https://github.com/pingcap/tidb/issues/26022 -func workaroundGoCheckFlags() { - if flag.Lookup("check.timeout") == nil { - _ = flag.Duration("check.timeout", 0, "workaroundGoCheckFlags: check.timeout") - } - if flag.Lookup("check.p") == nil { - _ = flag.Bool("check.p", false, "workaroundGoCheckFlags: check.p") - } -} - func applyOSLogLevel() { osLoglevel := os.Getenv("log_level") if len(osLoglevel) > 0 { diff --git a/tests/globalkilltest/main_test.go b/tests/globalkilltest/main_test.go index 71ee2c7e95780..ef5e3eb152f82 100644 --- a/tests/globalkilltest/main_test.go +++ b/tests/globalkilltest/main_test.go @@ -18,10 +18,10 @@ import ( "os" "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() os.Exit(m.Run()) } diff --git a/tests/graceshutdown/main_test.go b/tests/graceshutdown/main_test.go index dc131695874f7..b480886282c4b 100644 --- a/tests/graceshutdown/main_test.go +++ b/tests/graceshutdown/main_test.go @@ -17,12 +17,12 @@ package graceshutdown import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("syscall.syscall6"), diff --git a/tests/readonlytest/main_test.go b/tests/readonlytest/main_test.go index 3bfa7bb5ec28c..7cdc72764ebb9 100644 --- a/tests/readonlytest/main_test.go +++ b/tests/readonlytest/main_test.go @@ -17,12 +17,12 @@ package readonlytest import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/tests/realtikvtest/sessiontest/session_test.go b/tests/realtikvtest/sessiontest/session_test.go index 271d6032bda11..b60bca147e62f 100644 --- a/tests/realtikvtest/sessiontest/session_test.go +++ b/tests/realtikvtest/sessiontest/session_test.go @@ -17,7 +17,9 @@ package sessiontest import ( "context" "fmt" + "net" "sort" + "strconv" "strings" "sync" "testing" @@ -25,6 +27,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/config" + "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/parser/auth" @@ -3058,3 +3061,652 @@ func TestLastMessage(t *testing.T) { tk.MustExec(`INSERT INTO t SELECT * FROM t1 ON DUPLICATE KEY UPDATE c2=a2;`) tk.CheckLastMessage("Records: 6 Duplicates: 3 Warnings: 0") } + +func TestQueryString(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + tk.MustExec("create table mutil1 (a int);create table multi2 (a int)") + queryStr := tk.Session().Value(sessionctx.QueryString) + require.Equal(t, "create table multi2 (a int)", queryStr) + + // Test execution of DDL through the "ExecutePreparedStmt" interface. + tk.MustExec("use test") + tk.MustExec("CREATE TABLE t (id bigint PRIMARY KEY, age int)") + tk.MustExec("show create table t") + id, _, _, err := tk.Session().PrepareStmt("CREATE TABLE t2(id bigint PRIMARY KEY, age int)") + require.NoError(t, err) + var params []types.Datum + _, err = tk.Session().ExecutePreparedStmt(context.Background(), id, params) + require.NoError(t, err) + qs := tk.Session().Value(sessionctx.QueryString) + require.Equal(t, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)", qs.(string)) + + // Test execution of DDL through the "Execute" interface. + tk.MustExec("use test") + tk.MustExec("drop table t2") + tk.MustExec("prepare stmt from 'CREATE TABLE t2(id bigint PRIMARY KEY, age int)'") + tk.MustExec("execute stmt") + qs = tk.Session().Value(sessionctx.QueryString) + require.Equal(t, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)", qs.(string)) +} + +func TestAffectedRows(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(id TEXT)") + tk.MustExec(`INSERT INTO t VALUES ("a");`) + require.Equal(t, 1, int(tk.Session().AffectedRows())) + tk.MustExec(`INSERT INTO t VALUES ("b");`) + require.Equal(t, 1, int(tk.Session().AffectedRows())) + tk.MustExec(`UPDATE t set id = 'c' where id = 'a';`) + require.Equal(t, 1, int(tk.Session().AffectedRows())) + tk.MustExec(`UPDATE t set id = 'a' where id = 'a';`) + require.Equal(t, 0, int(tk.Session().AffectedRows())) + tk.MustQuery(`SELECT * from t`).Check(testkit.Rows("c", "b")) + require.Equal(t, 0, int(tk.Session().AffectedRows())) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id int, data int)") + tk.MustExec(`INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) + tk.MustExec(`UPDATE t set id = 1 where data = 0;`) + require.Equal(t, 1, int(tk.Session().AffectedRows())) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id int, c1 timestamp);") + tk.MustExec(`insert t(id) values(1);`) + tk.MustExec(`UPDATE t set id = 1 where id = 1;`) + require.Equal(t, 0, int(tk.Session().AffectedRows())) + + // With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, + // 2 if an existing row is updated, and 0 if an existing row is set to its current values. + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (c1 int PRIMARY KEY, c2 int);") + tk.MustExec(`insert t values(1, 1);`) + tk.MustExec(`insert into t values (1, 1) on duplicate key update c2=2;`) + require.Equal(t, 2, int(tk.Session().AffectedRows())) + tk.MustExec(`insert into t values (1, 1) on duplicate key update c2=2;`) + require.Equal(t, 0, int(tk.Session().AffectedRows())) + tk.MustExec("drop table if exists test") + createSQL := `CREATE TABLE test ( + id VARCHAR(36) PRIMARY KEY NOT NULL, + factor INTEGER NOT NULL DEFAULT 2);` + tk.MustExec(createSQL) + insertSQL := `INSERT INTO test(id) VALUES('id') ON DUPLICATE KEY UPDATE factor=factor+3;` + tk.MustExec(insertSQL) + require.Equal(t, 1, int(tk.Session().AffectedRows())) + tk.MustExec(insertSQL) + require.Equal(t, 2, int(tk.Session().AffectedRows())) + tk.MustExec(insertSQL) + require.Equal(t, 2, int(tk.Session().AffectedRows())) + + tk.Session().SetClientCapability(mysql.ClientFoundRows) + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id int, data int)") + tk.MustExec(`INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) + tk.MustExec(`UPDATE t set id = 1 where data = 0;`) + require.Equal(t, 2, int(tk.Session().AffectedRows())) +} + +// TestRowLock . See http://dev.mysql.com/doc/refman/5.7/en/commit.html. +func TestRowLock(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk1 := testkit.NewTestKit(t, store) + tk1.MustExec("use test") + tk2 := testkit.NewTestKit(t, store) + tk2.MustExec("use test") + + tk.MustExec("drop table if exists t") + txn, err := tk.Session().Txn(true) + require.True(t, kv.ErrInvalidTxn.Equal(err)) + require.False(t, txn.Valid()) + tk.MustExec("create table t (c1 int, c2 int, c3 int)") + tk.MustExec("insert t values (11, 2, 3)") + tk.MustExec("insert t values (12, 2, 3)") + tk.MustExec("insert t values (13, 2, 3)") + + tk1.MustExec("set @@tidb_disable_txn_auto_retry = 0") + tk1.MustExec("begin") + tk1.MustExec("update t set c2=21 where c1=11") + + tk2.MustExec("begin") + tk2.MustExec("update t set c2=211 where c1=11") + tk2.MustExec("commit") + + // tk1 will retry and the final value is 21 + tk1.MustExec("commit") + + // Check the result is correct + tk.MustQuery("select c2 from t where c1=11").Check(testkit.Rows("21")) + + tk1.MustExec("begin") + tk1.MustExec("update t set c2=21 where c1=11") + + tk2.MustExec("begin") + tk2.MustExec("update t set c2=22 where c1=12") + tk2.MustExec("commit") + + tk1.MustExec("commit") +} + +// TestAutocommit . See https://dev.mysql.com/doc/internals/en/status-flags.html +func TestAutocommit(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + tk.MustExec("drop table if exists t;") + require.Greater(t, int(tk.Session().Status()&mysql.ServerStatusAutocommit), 0) + tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") + require.Greater(t, int(tk.Session().Status()&mysql.ServerStatusAutocommit), 0) + tk.MustExec("insert t values ()") + require.Greater(t, int(tk.Session().Status()&mysql.ServerStatusAutocommit), 0) + tk.MustExec("begin") + require.Greater(t, int(tk.Session().Status()&mysql.ServerStatusAutocommit), 0) + tk.MustExec("insert t values ()") + require.Greater(t, int(tk.Session().Status()&mysql.ServerStatusAutocommit), 0) + tk.MustExec("drop table if exists t") + require.Greater(t, int(tk.Session().Status()&mysql.ServerStatusAutocommit), 0) + + tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") + require.Greater(t, int(tk.Session().Status()&mysql.ServerStatusAutocommit), 0) + tk.MustExec("set autocommit=0") + require.Equal(t, 0, int(tk.Session().Status()&mysql.ServerStatusAutocommit)) + tk.MustExec("insert t values ()") + require.Equal(t, 0, int(tk.Session().Status()&mysql.ServerStatusAutocommit)) + tk.MustExec("commit") + require.Equal(t, 0, int(tk.Session().Status()&mysql.ServerStatusAutocommit)) + tk.MustExec("drop table if exists t") + require.Equal(t, 0, int(tk.Session().Status()&mysql.ServerStatusAutocommit)) + tk.MustExec("set autocommit='On'") + require.Greater(t, int(tk.Session().Status()&mysql.ServerStatusAutocommit), 0) + + // When autocommit is 0, transaction start ts should be the first *valid* + // statement, rather than *any* statement. + tk.MustExec("create table t (id int)") + tk.MustExec("set @@autocommit = 0") + tk.MustExec("rollback") + tk.MustExec("set @@autocommit = 0") + + tk1 := testkit.NewTestKit(t, store) + tk1.MustExec("use test") + tk1.MustExec("insert into t select 1") + + tk.MustQuery("select * from t").Check(testkit.Rows("1")) + + // TODO: MySQL compatibility for setting global variable. + // tk.MustExec("begin") + // tk.MustExec("insert into t values (42)") + // tk.MustExec("set @@global.autocommit = 1") + // tk.MustExec("rollback") + // tk.MustQuery("select count(*) from t where id = 42").Check(testkit.Rows("0")) + // Even the transaction is rollbacked, the set statement succeed. + // tk.MustQuery("select @@global.autocommit").Rows("1") +} + +// TestTxnLazyInitialize tests that when autocommit = 0, not all statement starts +// a new transaction. +func TestTxnLazyInitialize(t *testing.T) { + testTxnLazyInitialize(t, false) + testTxnLazyInitialize(t, true) +} + +func testTxnLazyInitialize(t *testing.T, isPessimistic bool) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id int)") + if isPessimistic { + tk.MustExec("set tidb_txn_mode = 'pessimistic'") + } + + tk.MustExec("set @@autocommit = 0") + _, err := tk.Session().Txn(true) + require.True(t, kv.ErrInvalidTxn.Equal(err)) + txn, err := tk.Session().Txn(false) + require.NoError(t, err) + require.False(t, txn.Valid()) + tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) + tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) + + // Those statements should not start a new transaction automatically. + tk.MustQuery("select 1") + tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) + + tk.MustExec("set @@tidb_general_log = 0") + tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) + + tk.MustQuery("explain select * from t") + tk.MustQuery("select @@tidb_current_ts").Check(testkit.Rows("0")) + + // Begin statement should start a new transaction. + tk.MustExec("begin") + txn, err = tk.Session().Txn(false) + require.NoError(t, err) + require.True(t, txn.Valid()) + tk.MustExec("rollback") + + tk.MustExec("select * from t") + txn, err = tk.Session().Txn(false) + require.NoError(t, err) + require.True(t, txn.Valid()) + tk.MustExec("rollback") + + tk.MustExec("insert into t values (1)") + txn, err = tk.Session().Txn(false) + require.NoError(t, err) + require.True(t, txn.Valid()) + tk.MustExec("rollback") +} + +func TestGlobalVarAccessor(t *testing.T) { + varName := "max_allowed_packet" + varValue := strconv.FormatUint(variable.DefMaxAllowedPacket, 10) // This is the default value for max_allowed_packet + + // The value of max_allowed_packet should be a multiple of 1024, + // so the setting of varValue1 and varValue2 would be truncated to varValue0 + varValue0 := "4194304" + varValue1 := "4194305" + varValue2 := "4194306" + + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + se := tk.Session().(variable.GlobalVarAccessor) + // Get globalSysVar twice and get the same value + v, err := se.GetGlobalSysVar(varName) + require.NoError(t, err) + require.Equal(t, varValue, v) + v, err = se.GetGlobalSysVar(varName) + require.NoError(t, err) + require.Equal(t, varValue, v) + // Set global var to another value + err = se.SetGlobalSysVar(varName, varValue1) + require.NoError(t, err) + v, err = se.GetGlobalSysVar(varName) + require.NoError(t, err) + require.Equal(t, varValue0, v) + require.NoError(t, tk.Session().CommitTxn(context.TODO())) + + tk1 := testkit.NewTestKit(t, store) + tk1.MustExec("use test") + se1 := tk1.Session().(variable.GlobalVarAccessor) + v, err = se1.GetGlobalSysVar(varName) + require.NoError(t, err) + require.Equal(t, varValue0, v) + err = se1.SetGlobalSysVar(varName, varValue2) + require.NoError(t, err) + v, err = se1.GetGlobalSysVar(varName) + require.NoError(t, err) + require.Equal(t, varValue0, v) + require.NoError(t, tk1.Session().CommitTxn(context.TODO())) + + // Make sure the change is visible to any client that accesses that global variable. + v, err = se.GetGlobalSysVar(varName) + require.NoError(t, err) + require.Equal(t, varValue0, v) + + // For issue 10955, make sure the new session load `max_execution_time` into sessionVars. + tk1.MustExec("set @@global.max_execution_time = 100") + tk2 := testkit.NewTestKit(t, store) + tk2.MustExec("use test") + require.Equal(t, uint64(100), tk2.Session().GetSessionVars().MaxExecutionTime) + tk1.MustExec("set @@global.max_execution_time = 0") + + result := tk.MustQuery("show global variables where variable_name='sql_select_limit';") + result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) + result = tk.MustQuery("show session variables where variable_name='sql_select_limit';") + result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) + tk.MustExec("set session sql_select_limit=100000000000;") + result = tk.MustQuery("show global variables where variable_name='sql_select_limit';") + result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) + result = tk.MustQuery("show session variables where variable_name='sql_select_limit';") + result.Check(testkit.Rows("sql_select_limit 100000000000")) + tk.MustExec("set @@global.sql_select_limit = 1") + result = tk.MustQuery("show global variables where variable_name='sql_select_limit';") + result.Check(testkit.Rows("sql_select_limit 1")) + tk.MustExec("set @@global.sql_select_limit = default") + result = tk.MustQuery("show global variables where variable_name='sql_select_limit';") + result.Check(testkit.Rows("sql_select_limit 18446744073709551615")) + + result = tk.MustQuery("select @@global.autocommit;") + result.Check(testkit.Rows("1")) + result = tk.MustQuery("select @@autocommit;") + result.Check(testkit.Rows("1")) + tk.MustExec("set @@global.autocommit = 0;") + result = tk.MustQuery("select @@global.autocommit;") + result.Check(testkit.Rows("0")) + result = tk.MustQuery("select @@autocommit;") + result.Check(testkit.Rows("1")) + tk.MustExec("set @@global.autocommit=1") + + err = tk.ExecToErr("set global time_zone = 'timezone'") + require.Error(t, err) + require.True(t, terror.ErrorEqual(err, variable.ErrUnknownTimeZone)) +} + +func TestUpgradeSysvars(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + se := tk.Session().(variable.GlobalVarAccessor) + + // Set the global var to a non-canonical form of the value + // i.e. implying that it was set from an earlier version of TiDB. + + tk.MustExec(`REPLACE INTO mysql.global_variables (variable_name, variable_value) VALUES ('tidb_enable_noop_functions', '0')`) + domain.GetDomain(tk.Session()).NotifyUpdateSysVarCache() // update cache + v, err := se.GetGlobalSysVar("tidb_enable_noop_functions") + require.NoError(t, err) + require.Equal(t, "OFF", v) + + // Set the global var to "" which is the invalid version of this from TiDB 4.0.16 + // the err is quashed by the GetGlobalSysVar, and the default value is restored. + // This helps callers of GetGlobalSysVar(), which can't individually be expected + // to handle upgrade/downgrade issues correctly. + + tk.MustExec(`REPLACE INTO mysql.global_variables (variable_name, variable_value) VALUES ('rpl_semi_sync_slave_enabled', '')`) + domain.GetDomain(tk.Session()).NotifyUpdateSysVarCache() // update cache + v, err = se.GetGlobalSysVar("rpl_semi_sync_slave_enabled") + require.NoError(t, err) + require.Equal(t, "OFF", v) // the default value is restored. + result := tk.MustQuery("SHOW VARIABLES LIKE 'rpl_semi_sync_slave_enabled'") + result.Check(testkit.Rows("rpl_semi_sync_slave_enabled OFF")) + + // Ensure variable out of range is converted to in range after upgrade. + // This further helps for https://github.com/pingcap/tidb/pull/28842 + + tk.MustExec(`REPLACE INTO mysql.global_variables (variable_name, variable_value) VALUES ('tidb_executor_concurrency', '999')`) + domain.GetDomain(tk.Session()).NotifyUpdateSysVarCache() // update cache + v, err = se.GetGlobalSysVar("tidb_executor_concurrency") + require.NoError(t, err) + require.Equal(t, "256", v) // the max value is restored. + + // Handle the case of a completely bogus value from an earlier version of TiDB. + // This could be the case if an ENUM sysvar removes a value. + + tk.MustExec(`REPLACE INTO mysql.global_variables (variable_name, variable_value) VALUES ('tidb_enable_noop_functions', 'SOMEVAL')`) + domain.GetDomain(tk.Session()).NotifyUpdateSysVarCache() // update cache + v, err = se.GetGlobalSysVar("tidb_enable_noop_functions") + require.NoError(t, err) + require.Equal(t, "OFF", v) // the default value is restored. +} + +func TestSetInstanceSysvarBySetGlobalSysVar(t *testing.T) { + varName := "tidb_general_log" + defaultValue := "OFF" // This is the default value for tidb_general_log + + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + se := tk.Session().(variable.GlobalVarAccessor) + + // Get globalSysVar twice and get the same default value + v, err := se.GetGlobalSysVar(varName) + require.NoError(t, err) + require.Equal(t, defaultValue, v) + v, err = se.GetGlobalSysVar(varName) + require.NoError(t, err) + require.Equal(t, defaultValue, v) + + // session.GetGlobalSysVar would not get the value which session.SetGlobalSysVar writes, + // because SetGlobalSysVar calls SetGlobalFromHook, which uses TiDBGeneralLog's SetGlobal, + // but GetGlobalSysVar could not access TiDBGeneralLog's GetGlobal. + + // set to "1" + err = se.SetGlobalSysVar(varName, "ON") + require.NoError(t, err) + v, err = se.GetGlobalSysVar(varName) + tk.MustQuery("select @@global.tidb_general_log").Check(testkit.Rows("1")) + require.NoError(t, err) + require.Equal(t, defaultValue, v) + + // set back to "0" + err = se.SetGlobalSysVar(varName, defaultValue) + require.NoError(t, err) + v, err = se.GetGlobalSysVar(varName) + tk.MustQuery("select @@global.tidb_general_log").Check(testkit.Rows("0")) + require.NoError(t, err) + require.Equal(t, defaultValue, v) +} + +func TestMatchIdentity(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("CREATE USER `useridentity`@`%`") + tk.MustExec("CREATE USER `useridentity`@`localhost`") + tk.MustExec("CREATE USER `useridentity`@`192.168.1.1`") + tk.MustExec("CREATE USER `useridentity`@`example.com`") + + // The MySQL matching rule is most specific to least specific. + // So if I log in from 192.168.1.1 I should match that entry always. + identity, err := tk.Session().MatchIdentity("useridentity", "192.168.1.1") + require.NoError(t, err) + require.Equal(t, "useridentity", identity.Username) + require.Equal(t, "192.168.1.1", identity.Hostname) + + // If I log in from localhost, I should match localhost + identity, err = tk.Session().MatchIdentity("useridentity", "localhost") + require.NoError(t, err) + require.Equal(t, "useridentity", identity.Username) + require.Equal(t, "localhost", identity.Hostname) + + // If I log in from 192.168.1.2 I should match wildcard. + identity, err = tk.Session().MatchIdentity("useridentity", "192.168.1.2") + require.NoError(t, err) + require.Equal(t, "useridentity", identity.Username) + require.Equal(t, "%", identity.Hostname) + + identity, err = tk.Session().MatchIdentity("useridentity", "127.0.0.1") + require.NoError(t, err) + require.Equal(t, "useridentity", identity.Username) + require.Equal(t, "localhost", identity.Hostname) + + // This uses the lookup of example.com to get an IP address. + // We then login with that IP address, but expect it to match the example.com + // entry in the privileges table (by reverse lookup). + ips, err := net.LookupHost("example.com") + require.NoError(t, err) + identity, err = tk.Session().MatchIdentity("useridentity", ips[0]) + require.NoError(t, err) + require.Equal(t, "useridentity", identity.Username) + // FIXME: we *should* match example.com instead + // as long as skip-name-resolve is not set (DEFAULT) + require.Equal(t, "%", identity.Hostname) +} + +func TestGetSysVariables(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + // Test ScopeSession + tk.MustExec("select @@warning_count") + tk.MustExec("select @@session.warning_count") + tk.MustExec("select @@local.warning_count") + err := tk.ExecToErr("select @@global.warning_count") + require.True(t, terror.ErrorEqual(err, variable.ErrIncorrectScope), fmt.Sprintf("err %v", err)) + + // Test ScopeGlobal + tk.MustExec("select @@max_connections") + tk.MustExec("select @@global.max_connections") + tk.MustGetErrMsg("select @@session.max_connections", "[variable:1238]Variable 'max_connections' is a GLOBAL variable") + tk.MustGetErrMsg("select @@local.max_connections", "[variable:1238]Variable 'max_connections' is a GLOBAL variable") + + // Test ScopeNone + tk.MustExec("select @@performance_schema_max_mutex_classes") + tk.MustExec("select @@global.performance_schema_max_mutex_classes") + // For issue 19524, test + tk.MustExec("select @@session.performance_schema_max_mutex_classes") + tk.MustExec("select @@local.performance_schema_max_mutex_classes") + tk.MustGetErrMsg("select @@global.last_insert_id", "[variable:1238]Variable 'last_insert_id' is a SESSION variable") +} + +// TestInTrans . See https://dev.mysql.com/doc/internals/en/status-flags.html +func TestInTrans(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") + tk.MustExec("insert t values ()") + tk.MustExec("begin") + txn, err := tk.Session().Txn(true) + require.NoError(t, err) + require.True(t, txn.Valid()) + tk.MustExec("insert t values ()") + require.True(t, txn.Valid()) + tk.MustExec("drop table if exists t;") + require.False(t, txn.Valid()) + tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") + require.False(t, txn.Valid()) + tk.MustExec("insert t values ()") + require.False(t, txn.Valid()) + tk.MustExec("commit") + tk.MustExec("insert t values ()") + + tk.MustExec("set autocommit=0") + tk.MustExec("begin") + require.True(t, txn.Valid()) + tk.MustExec("insert t values ()") + require.True(t, txn.Valid()) + tk.MustExec("commit") + require.False(t, txn.Valid()) + tk.MustExec("insert t values ()") + require.True(t, txn.Valid()) + tk.MustExec("commit") + require.False(t, txn.Valid()) + + tk.MustExec("set autocommit=1") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL)") + tk.MustExec("begin") + require.True(t, txn.Valid()) + tk.MustExec("insert t values ()") + require.True(t, txn.Valid()) + tk.MustExec("rollback") + require.False(t, txn.Valid()) +} + +func TestSession(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("ROLLBACK;") + tk.Session().Close() +} + +func TestSessionAuth(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + require.False(t, tk.Session().Auth(&auth.UserIdentity{Username: "Any not exist username with zero password!", Hostname: "anyhost"}, []byte(""), []byte(""))) +} + +func TestLastInsertID(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + // insert + tk.MustExec("create table t (c1 int not null auto_increment, c2 int, PRIMARY KEY (c1))") + tk.MustExec("insert into t set c2 = 11") + tk.MustQuery("select last_insert_id()").Check(testkit.Rows("1")) + + tk.MustExec("insert into t (c2) values (22), (33), (44)") + tk.MustQuery("select last_insert_id()").Check(testkit.Rows("2")) + + tk.MustExec("insert into t (c1, c2) values (10, 55)") + tk.MustQuery("select last_insert_id()").Check(testkit.Rows("2")) + + // replace + tk.MustExec("replace t (c2) values(66)") + tk.MustQuery("select * from t").Check(testkit.Rows("1 11", "2 22", "3 33", "4 44", "10 55", "11 66")) + tk.MustQuery("select last_insert_id()").Check(testkit.Rows("11")) + + // update + tk.MustExec("update t set c1=last_insert_id(c1 + 100)") + tk.MustQuery("select * from t").Check(testkit.Rows("101 11", "102 22", "103 33", "104 44", "110 55", "111 66")) + tk.MustQuery("select last_insert_id()").Check(testkit.Rows("111")) + tk.MustExec("insert into t (c2) values (77)") + tk.MustQuery("select last_insert_id()").Check(testkit.Rows("112")) + + // drop + tk.MustExec("drop table t") + tk.MustQuery("select last_insert_id()").Check(testkit.Rows("112")) + + tk.MustExec("create table t (c2 int, c3 int, c1 int not null auto_increment, PRIMARY KEY (c1))") + tk.MustExec("insert into t set c2 = 30") + + // insert values + lastInsertID := tk.Session().LastInsertID() + tk.MustExec("prepare stmt1 from 'insert into t (c2) values (?)'") + tk.MustExec("set @v1=10") + tk.MustExec("set @v2=20") + tk.MustExec("execute stmt1 using @v1") + tk.MustExec("execute stmt1 using @v2") + tk.MustExec("deallocate prepare stmt1") + currLastInsertID := tk.Session().GetSessionVars().StmtCtx.PrevLastInsertID + tk.MustQuery("select c1 from t where c2 = 20").Check(testkit.Rows(fmt.Sprint(currLastInsertID))) + require.Equal(t, currLastInsertID, lastInsertID+2) +} + +func TestBinaryReadOnly(t *testing.T) { + store, clean := realtikvtest.CreateMockStoreAndSetup(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t (i int key)") + id, _, _, err := tk.Session().PrepareStmt("select i from t where i = ?") + require.NoError(t, err) + id2, _, _, err := tk.Session().PrepareStmt("insert into t values (?)") + require.NoError(t, err) + tk.MustExec("set autocommit = 0") + tk.MustExec("set tidb_disable_txn_auto_retry = 0") + _, err = tk.Session().ExecutePreparedStmt(context.Background(), id, []types.Datum{types.NewDatum(1)}) + require.NoError(t, err) + require.Equal(t, 0, session.GetHistory(tk.Session()).Count()) + tk.MustExec("insert into t values (1)") + require.Equal(t, 1, session.GetHistory(tk.Session()).Count()) + _, err = tk.Session().ExecutePreparedStmt(context.Background(), id2, []types.Datum{types.NewDatum(2)}) + require.NoError(t, err) + require.Equal(t, 2, session.GetHistory(tk.Session()).Count()) + tk.MustExec("commit") +} diff --git a/tests/realtikvtest/testkit.go b/tests/realtikvtest/testkit.go index 6d15f5f43f2ec..501fafcd3e6ec 100644 --- a/tests/realtikvtest/testkit.go +++ b/tests/realtikvtest/testkit.go @@ -30,7 +30,7 @@ import ( "github.com/pingcap/tidb/store/driver" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/testkit/testmain" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/stretchr/testify/require" "github.com/tikv/client-go/v2/tikv" "github.com/tikv/client-go/v2/txnkv/transaction" @@ -44,7 +44,7 @@ var WithRealTiKV = flag.Bool("with-real-tikv", false, "whether tests run with re // RunTestMain run common setups for all real tikv tests. func RunTestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() flag.Parse() session.SetSchemaLease(20 * time.Millisecond) config.UpdateGlobal(func(conf *config.Config) { diff --git a/tidb-server/main_test.go b/tidb-server/main_test.go index 0f5719a17aee9..6989aa588f3ec 100644 --- a/tidb-server/main_test.go +++ b/tidb-server/main_test.go @@ -21,7 +21,7 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/sessionctx/variable" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/stretchr/testify/require" "go.uber.org/goleak" ) @@ -29,7 +29,7 @@ import ( var isCoverageServer string func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/types/json/main_test.go b/types/json/main_test.go index 8e138439a36e4..329bed3e3f0fc 100644 --- a/types/json/main_test.go +++ b/types/json/main_test.go @@ -17,14 +17,14 @@ package json import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) const benchStr = `{"a":[1,"2",{"aa":"bb"},4,null],"b":true,"c":null}` func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/types/main_test.go b/types/main_test.go index f4ffa66d3ce02..af337f93a762d 100644 --- a/types/main_test.go +++ b/types/main_test.go @@ -17,12 +17,12 @@ package types import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/types/parser_driver/main_test.go b/types/parser_driver/main_test.go index c1d814061a877..b8cc6966f9694 100644 --- a/types/parser_driver/main_test.go +++ b/types/parser_driver/main_test.go @@ -17,12 +17,12 @@ package driver import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/admin/main_test.go b/util/admin/main_test.go index 96e8d45550145..fb4afa20523c7 100644 --- a/util/admin/main_test.go +++ b/util/admin/main_test.go @@ -18,12 +18,12 @@ import ( "testing" "github.com/pingcap/tidb/config" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() config.UpdateGlobal(func(conf *config.Config) { conf.TiKVClient.AsyncCommit.SafeWindow = 0 diff --git a/util/arena/main_test.go b/util/arena/main_test.go index f542c1076cadf..390edd50990ba 100644 --- a/util/arena/main_test.go +++ b/util/arena/main_test.go @@ -17,12 +17,12 @@ package arena import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/benchdaily/main_test.go b/util/benchdaily/main_test.go index 65f48e8a9a8e2..10165494dc815 100644 --- a/util/benchdaily/main_test.go +++ b/util/benchdaily/main_test.go @@ -17,12 +17,12 @@ package benchdaily import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/bitmap/main_test.go b/util/bitmap/main_test.go index 41ee9ebe20409..a58b6819e2873 100644 --- a/util/bitmap/main_test.go +++ b/util/bitmap/main_test.go @@ -17,12 +17,12 @@ package bitmap import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/checksum/main_test.go b/util/checksum/main_test.go index 85e9a5383a402..7442fafc9e13f 100644 --- a/util/checksum/main_test.go +++ b/util/checksum/main_test.go @@ -17,12 +17,12 @@ package checksum import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/chunk/main_test.go b/util/chunk/main_test.go index d3f9eddaa54eb..12a2ac966e298 100644 --- a/util/chunk/main_test.go +++ b/util/chunk/main_test.go @@ -19,12 +19,12 @@ import ( "testing" "github.com/pingcap/tidb/config" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() path, _ := os.MkdirTemp("", "oom-use-tmp-storage") config.UpdateGlobal(func(conf *config.Config) { diff --git a/util/codec/main_test.go b/util/codec/main_test.go index a0bb53eeb7058..7bafb6674a521 100644 --- a/util/codec/main_test.go +++ b/util/codec/main_test.go @@ -17,12 +17,12 @@ package codec import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/collate/main_test.go b/util/collate/main_test.go index 99cdcba6663ee..f49fa70eb7f11 100644 --- a/util/collate/main_test.go +++ b/util/collate/main_test.go @@ -17,12 +17,12 @@ package collate import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/cpuprofile/cpuprofile_test.go b/util/cpuprofile/cpuprofile_test.go index 2b7b3ca14a068..2d400264ede10 100644 --- a/util/cpuprofile/cpuprofile_test.go +++ b/util/cpuprofile/cpuprofile_test.go @@ -27,14 +27,14 @@ import ( "time" "github.com/google/pprof/profile" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/pingcap/tidb/util/cpuprofile/testutil" - "github.com/pingcap/tidb/util/testbridge" "github.com/stretchr/testify/require" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() // To speed up testing DefProfileDuration = time.Millisecond * 200 goleak.VerifyTestMain(m) diff --git a/util/cteutil/main_test.go b/util/cteutil/main_test.go index 7f7f29a6d7267..4b770405cae1e 100644 --- a/util/cteutil/main_test.go +++ b/util/cteutil/main_test.go @@ -17,12 +17,12 @@ package cteutil import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/dbterror/main_test.go b/util/dbterror/main_test.go index e69c6873018e5..133187cfb5e26 100644 --- a/util/dbterror/main_test.go +++ b/util/dbterror/main_test.go @@ -17,12 +17,12 @@ package dbterror import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), } diff --git a/util/deadlockhistory/main_test.go b/util/deadlockhistory/main_test.go index 694c2828824ae..1b35bf1b98a07 100644 --- a/util/deadlockhistory/main_test.go +++ b/util/deadlockhistory/main_test.go @@ -17,12 +17,12 @@ package deadlockhistory import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/disjointset/main_test.go b/util/disjointset/main_test.go index 8f598e2ba3a74..db19bce1ec4df 100644 --- a/util/disjointset/main_test.go +++ b/util/disjointset/main_test.go @@ -17,12 +17,12 @@ package disjointset import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/disk/main_test.go b/util/disk/main_test.go index d5418c0b67dfd..e81052c6da6f1 100644 --- a/util/disk/main_test.go +++ b/util/disk/main_test.go @@ -17,12 +17,12 @@ package disk import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/encrypt/main_test.go b/util/encrypt/main_test.go index e6df43ef9941d..a63aa4ef2a8e0 100644 --- a/util/encrypt/main_test.go +++ b/util/encrypt/main_test.go @@ -17,12 +17,12 @@ package encrypt import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m) } diff --git a/util/execdetails/main_test.go b/util/execdetails/main_test.go index 3f053c81c12a1..0f3742206df1d 100644 --- a/util/execdetails/main_test.go +++ b/util/execdetails/main_test.go @@ -16,12 +16,12 @@ package execdetails import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/expensivequery/expensivequerey_test.go b/util/expensivequery/expensivequerey_test.go index e19afa14aaea4..c1a8464fdfeb9 100644 --- a/util/expensivequery/expensivequerey_test.go +++ b/util/expensivequery/expensivequerey_test.go @@ -19,15 +19,15 @@ import ( "time" "github.com/pingcap/tidb/sessionctx/stmtctx" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/memory" - "github.com/pingcap/tidb/util/testbridge" "github.com/stretchr/testify/assert" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/fastrand/main_test.go b/util/fastrand/main_test.go index 02b8d749ef9cc..697272281901a 100644 --- a/util/fastrand/main_test.go +++ b/util/fastrand/main_test.go @@ -17,12 +17,12 @@ package fastrand import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/format/main_test.go b/util/format/main_test.go index 69232c0dcaed6..4007921cfa497 100644 --- a/util/format/main_test.go +++ b/util/format/main_test.go @@ -17,12 +17,12 @@ package format import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/generatedexpr/main_test.go b/util/generatedexpr/main_test.go index b676469f0c7d3..c85821e5d5acb 100644 --- a/util/generatedexpr/main_test.go +++ b/util/generatedexpr/main_test.go @@ -17,12 +17,12 @@ package generatedexpr import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/hack/main_test.go b/util/hack/main_test.go index 4dec7ea811549..5570c024cb379 100644 --- a/util/hack/main_test.go +++ b/util/hack/main_test.go @@ -17,12 +17,12 @@ package hack import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/keydecoder/main_test.go b/util/keydecoder/main_test.go index b8e332fdcf621..a74939704281a 100644 --- a/util/keydecoder/main_test.go +++ b/util/keydecoder/main_test.go @@ -17,7 +17,7 @@ package keydecoder import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -27,6 +27,6 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m, opts...) } diff --git a/util/kvcache/main_test.go b/util/kvcache/main_test.go index 9c15628f325a0..d3f68d489b77b 100644 --- a/util/kvcache/main_test.go +++ b/util/kvcache/main_test.go @@ -17,12 +17,12 @@ package kvcache import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/localpool/main_test.go b/util/localpool/main_test.go index 8079a5c2275ea..8b7091159ab47 100644 --- a/util/localpool/main_test.go +++ b/util/localpool/main_test.go @@ -17,12 +17,12 @@ package localpool import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/logutil/main_test.go b/util/logutil/main_test.go index e05b4ac7442cf..b1ac511fec3c1 100644 --- a/util/logutil/main_test.go +++ b/util/logutil/main_test.go @@ -17,7 +17,7 @@ package logutil import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -34,7 +34,7 @@ var ( ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("gopkg.in/natefinch/lumberjack%2ev2.(*Logger).millRun"), diff --git a/util/main_test.go b/util/main_test.go index ecbbfe5de8c24..b1edabb604dac 100644 --- a/util/main_test.go +++ b/util/main_test.go @@ -17,12 +17,12 @@ package util_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/mathutil/main_test.go b/util/mathutil/main_test.go index 047b2e9d2e512..b1c570b85cd91 100644 --- a/util/mathutil/main_test.go +++ b/util/mathutil/main_test.go @@ -17,12 +17,12 @@ package mathutil import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/memory/main_test.go b/util/memory/main_test.go index 34ce2a33e66c3..a61c6337fcfe3 100644 --- a/util/memory/main_test.go +++ b/util/memory/main_test.go @@ -17,12 +17,12 @@ package memory import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m) } diff --git a/util/mock/main_test.go b/util/mock/main_test.go index 505930599f568..c528f577fbf1d 100644 --- a/util/mock/main_test.go +++ b/util/mock/main_test.go @@ -17,12 +17,12 @@ package mock import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/mvmap/main_test.go b/util/mvmap/main_test.go index e927047488840..0590cab17ab01 100644 --- a/util/mvmap/main_test.go +++ b/util/mvmap/main_test.go @@ -17,12 +17,12 @@ package mvmap import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/paging/main_test.go b/util/paging/main_test.go index 8078d665698f4..b8424e59afb54 100644 --- a/util/paging/main_test.go +++ b/util/paging/main_test.go @@ -17,12 +17,12 @@ package paging import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/parser/main_test.go b/util/parser/main_test.go index e119b5d60f7ac..8c093f222d473 100644 --- a/util/parser/main_test.go +++ b/util/parser/main_test.go @@ -17,12 +17,12 @@ package parser_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/plancodec/main_test.go b/util/plancodec/main_test.go index 400045a070f32..57968838e5bb4 100644 --- a/util/plancodec/main_test.go +++ b/util/plancodec/main_test.go @@ -17,12 +17,12 @@ package plancodec import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/printer/main_test.go b/util/printer/main_test.go index ec6c0a4b8f015..cce8fecb9634c 100644 --- a/util/printer/main_test.go +++ b/util/printer/main_test.go @@ -17,12 +17,12 @@ package printer import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/profile/main_test.go b/util/profile/main_test.go index 05cd6e959d8fa..b5342257c5d52 100644 --- a/util/profile/main_test.go +++ b/util/profile/main_test.go @@ -17,12 +17,12 @@ package profile import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/ranger/main_test.go b/util/ranger/main_test.go index f1eb37f9eceb2..7e4131603ab90 100644 --- a/util/ranger/main_test.go +++ b/util/ranger/main_test.go @@ -21,7 +21,7 @@ import ( "testing" "github.com/pingcap/tidb/testkit/testdata" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -29,7 +29,7 @@ var testDataMap = make(testdata.BookKeeper, 1) var rangerSuiteData testdata.TestData func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() flag.Parse() diff --git a/util/resourcegrouptag/main_test.go b/util/resourcegrouptag/main_test.go index aea5d31275209..b25e8fb3b03ba 100644 --- a/util/resourcegrouptag/main_test.go +++ b/util/resourcegrouptag/main_test.go @@ -17,12 +17,12 @@ package resourcegrouptag import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/rowDecoder/main_test.go b/util/rowDecoder/main_test.go index fa20c63dbba8f..13212c366f0c9 100644 --- a/util/rowDecoder/main_test.go +++ b/util/rowDecoder/main_test.go @@ -17,7 +17,7 @@ package decoder_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) @@ -27,6 +27,6 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() goleak.VerifyTestMain(m, opts...) } diff --git a/util/rowcodec/main_test.go b/util/rowcodec/main_test.go index c8486191b17dd..080df00080558 100644 --- a/util/rowcodec/main_test.go +++ b/util/rowcodec/main_test.go @@ -18,14 +18,14 @@ import ( "testing" "github.com/pingcap/tidb/sessionctx/stmtctx" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/codec" - "github.com/pingcap/tidb/util/testbridge" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/selection/main_test.go b/util/selection/main_test.go index b1cfe4d5c5d08..632b4b65314eb 100644 --- a/util/selection/main_test.go +++ b/util/selection/main_test.go @@ -17,12 +17,12 @@ package selection import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/sem/main_test.go b/util/sem/main_test.go index 56b9685682edd..5c0463b7f0389 100644 --- a/util/sem/main_test.go +++ b/util/sem/main_test.go @@ -17,12 +17,12 @@ package sem import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/set/main_test.go b/util/set/main_test.go index 1e7c879d12ef1..0590afd81c18b 100644 --- a/util/set/main_test.go +++ b/util/set/main_test.go @@ -17,12 +17,12 @@ package set import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/slice/main_test.go b/util/slice/main_test.go index 659858c664a25..f2980a2c76419 100644 --- a/util/slice/main_test.go +++ b/util/slice/main_test.go @@ -17,12 +17,12 @@ package slice import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/sqlexec/main_test.go b/util/sqlexec/main_test.go index c7bdfccfe86dc..b664b25c6ce13 100644 --- a/util/sqlexec/main_test.go +++ b/util/sqlexec/main_test.go @@ -17,12 +17,12 @@ package sqlexec import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/stmtsummary/main_test.go b/util/stmtsummary/main_test.go index 546356ff06008..2168d68c30be2 100644 --- a/util/stmtsummary/main_test.go +++ b/util/stmtsummary/main_test.go @@ -17,12 +17,12 @@ package stmtsummary import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/stringutil/main_test.go b/util/stringutil/main_test.go index 1499960ed2e09..0c97626a477ec 100644 --- a/util/stringutil/main_test.go +++ b/util/stringutil/main_test.go @@ -17,12 +17,12 @@ package stringutil import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/sys/linux/main_test.go b/util/sys/linux/main_test.go index f74b932ff5096..41c46f7144ad8 100644 --- a/util/sys/linux/main_test.go +++ b/util/sys/linux/main_test.go @@ -17,12 +17,12 @@ package linux_test import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/sys/storage/main_test.go b/util/sys/storage/main_test.go index 98a3ba5aa37f9..f24011eb47f70 100644 --- a/util/sys/storage/main_test.go +++ b/util/sys/storage/main_test.go @@ -17,12 +17,12 @@ package storage import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/systimemon/main_test.go b/util/systimemon/main_test.go index 852a016dee4b6..7d9f1efd961c4 100644 --- a/util/systimemon/main_test.go +++ b/util/systimemon/main_test.go @@ -17,12 +17,12 @@ package systimemon import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), diff --git a/util/testkit/fake.go b/util/testkit/fake.go deleted file mode 100644 index 2c676427b7c6f..0000000000000 --- a/util/testkit/fake.go +++ /dev/null @@ -1,17 +0,0 @@ -// 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. - -//go:build codes - -package testkit diff --git a/util/testkit/testkit.go b/util/testkit/testkit.go deleted file mode 100644 index 6ab557f0da040..0000000000000 --- a/util/testkit/testkit.go +++ /dev/null @@ -1,411 +0,0 @@ -// Copyright 2015 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. - -//go:build !codes - -package testkit - -import ( - "bytes" - "context" - "fmt" - "sort" - "strings" - "sync/atomic" - - "github.com/pingcap/check" - "github.com/pingcap/errors" - "github.com/pingcap/tidb/domain" - "github.com/pingcap/tidb/kv" - "github.com/pingcap/tidb/parser/model" - "github.com/pingcap/tidb/parser/terror" - "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/types" - "github.com/pingcap/tidb/util/sqlexec" -) - -// TestKit is a utility to run sql test. -type TestKit struct { - c *check.C - store kv.Storage - Se session.Session -} - -// Result is the result returned by MustQuery. -type Result struct { - rows [][]string - comment check.CommentInterface - c *check.C -} - -// Check asserts the result equals the expected results. -func (res *Result) Check(expected [][]interface{}) { - resBuff := bytes.NewBufferString("") - for _, row := range res.rows { - fmt.Fprintf(resBuff, "%s\n", row) - } - needBuff := bytes.NewBufferString("") - for _, row := range expected { - fmt.Fprintf(needBuff, "%s\n", row) - } - res.c.Assert(resBuff.String(), check.Equals, needBuff.String(), res.comment) -} - -// CheckAt asserts the result of selected columns equals the expected results. -func (res *Result) CheckAt(cols []int, expected [][]interface{}) { - for _, e := range expected { - res.c.Assert(len(cols), check.Equals, len(e)) - } - - rows := make([][]string, 0, len(expected)) - for i := range res.rows { - row := make([]string, 0, len(cols)) - for _, r := range cols { - row = append(row, res.rows[i][r]) - } - rows = append(rows, row) - } - got := fmt.Sprintf("%s", rows) - need := fmt.Sprintf("%s", expected) - res.c.Assert(got, check.Equals, need, res.comment) -} - -// Rows returns the result data. -func (res *Result) Rows() [][]interface{} { - ifacesSlice := make([][]interface{}, len(res.rows)) - for i := range res.rows { - ifaces := make([]interface{}, len(res.rows[i])) - for j := range res.rows[i] { - ifaces[j] = res.rows[i][j] - } - ifacesSlice[i] = ifaces - } - return ifacesSlice -} - -// Sort sorts and return the result. -func (res *Result) Sort() *Result { - sort.Slice(res.rows, func(i, j int) bool { - a := res.rows[i] - b := res.rows[j] - for i := range a { - if a[i] < b[i] { - return true - } else if a[i] > b[i] { - return false - } - } - return false - }) - return res -} - -// NewTestKit returns a new *TestKit. -func NewTestKit(c *check.C, store kv.Storage) *TestKit { - return &TestKit{ - c: c, - store: store, - } -} - -// NewTestKitWithInit returns a new *TestKit and creates a session. -func NewTestKitWithInit(c *check.C, store kv.Storage) *TestKit { - tk := NewTestKit(c, store) - // Use test and prepare a session. - tk.MustExec("use test") - return tk -} - -var connectionID uint64 - -// GetConnectionID get the connection ID for tk.Se -func (tk *TestKit) GetConnectionID() { - if tk.Se != nil { - id := atomic.AddUint64(&connectionID, 1) - tk.Se.SetConnectionID(id) - } -} - -// Exec executes a sql statement using the prepared stmt API -func (tk *TestKit) Exec(sql string, args ...interface{}) (sqlexec.RecordSet, error) { - var err error - if tk.Se == nil { - tk.Se, err = session.CreateSession4Test(tk.store) - tk.c.Assert(err, check.IsNil) - tk.GetConnectionID() - } - ctx := context.Background() - if len(args) == 0 { - sc := tk.Se.GetSessionVars().StmtCtx - prevWarns := sc.GetWarnings() - stmts, err := tk.Se.Parse(ctx, sql) - if err != nil { - return nil, errors.Trace(err) - } - warns := sc.GetWarnings() - parserWarns := warns[len(prevWarns):] - var rs0 sqlexec.RecordSet - for i, stmt := range stmts { - rs, err := tk.Se.ExecuteStmt(ctx, stmt) - if i == 0 { - rs0 = rs - } - if err != nil { - tk.Se.GetSessionVars().StmtCtx.AppendError(err) - return nil, errors.Trace(err) - } - } - if len(parserWarns) > 0 { - tk.Se.GetSessionVars().StmtCtx.AppendWarnings(parserWarns) - } - return rs0, nil - } - stmtID, _, _, err := tk.Se.PrepareStmt(sql) - if err != nil { - return nil, errors.Trace(err) - } - params := make([]types.Datum, len(args)) - for i := 0; i < len(params); i++ { - params[i] = types.NewDatum(args[i]) - } - rs, err := tk.Se.ExecutePreparedStmt(ctx, stmtID, params) - if err != nil { - return nil, errors.Trace(err) - } - err = tk.Se.DropPreparedStmt(stmtID) - if err != nil { - return nil, errors.Trace(err) - } - return rs, nil -} - -// CheckExecResult checks the affected rows and the insert id after executing MustExec. -func (tk *TestKit) CheckExecResult(affectedRows, insertID int64) { - tk.c.Assert(affectedRows, check.Equals, int64(tk.Se.AffectedRows())) - tk.c.Assert(insertID, check.Equals, int64(tk.Se.LastInsertID())) -} - -// CheckLastMessage checks last message after executing MustExec -func (tk *TestKit) CheckLastMessage(msg string) { - tk.c.Assert(tk.Se.LastMessage(), check.Equals, msg) -} - -// MustExec executes a sql statement and asserts nil error. -func (tk *TestKit) MustExec(sql string, args ...interface{}) { - res, err := tk.Exec(sql, args...) - tk.c.Assert(err, check.IsNil, check.Commentf("sql:%s, %v, error stack %v", sql, args, errors.ErrorStack(err))) - if res != nil { - tk.c.Assert(res.Close(), check.IsNil) - } -} - -// HasPlan checks if the result execution plan contains specific plan. -func (tk *TestKit) HasPlan(sql string, plan string, args ...interface{}) bool { - rs := tk.MustQuery("explain "+sql, args...) - for i := range rs.rows { - if strings.Contains(rs.rows[i][0], plan) { - return true - } - } - return false -} - -func containGlobal(rs *Result) bool { - partitionNameCol := 2 - for i := range rs.rows { - if strings.Contains(rs.rows[i][partitionNameCol], "global") { - return true - } - } - return false -} - -// MustNoGlobalStats checks if there is no global stats. -func (tk *TestKit) MustNoGlobalStats(table string) bool { - if containGlobal(tk.MustQuery("show stats_meta where table_name like '" + table + "'")) { - return false - } - if containGlobal(tk.MustQuery("show stats_buckets where table_name like '" + table + "'")) { - return false - } - if containGlobal(tk.MustQuery("show stats_histograms where table_name like '" + table + "'")) { - return false - } - return true -} - -// MustPartition checks if the result execution plan must read specific partitions. -func (tk *TestKit) MustPartition(sql string, partitions string, args ...interface{}) *Result { - rs := tk.MustQuery("explain "+sql, args...) - ok := len(partitions) == 0 - for i := range rs.rows { - if len(partitions) == 0 && strings.Contains(rs.rows[i][3], "partition:") { - ok = false - } - if len(partitions) != 0 && strings.Compare(rs.rows[i][3], "partition:"+partitions) == 0 { - ok = true - } - } - tk.c.Assert(ok, check.IsTrue) - return tk.MustQuery(sql, args...) -} - -// UsedPartitions returns the partition names that will be used or all/dual. -func (tk *TestKit) UsedPartitions(sql string, args ...interface{}) *Result { - rs := tk.MustQuery("explain "+sql, args...) - var usedPartitions [][]string - for i := range rs.rows { - index := strings.Index(rs.rows[i][3], "partition:") - if index != -1 { - p := rs.rows[i][3][index+len("partition:"):] - partitions := strings.Split(strings.SplitN(p, " ", 2)[0], ",") - usedPartitions = append(usedPartitions, partitions) - } - } - return &Result{rows: usedPartitions, c: tk.c, comment: check.Commentf("sql:%s, args:%v", sql, args)} -} - -// MustUseIndex checks if the result execution plan contains specific index(es). -func (tk *TestKit) MustUseIndex(sql string, index string, args ...interface{}) bool { - rs := tk.MustQuery("explain "+sql, args...) - for i := range rs.rows { - if strings.Contains(rs.rows[i][3], "index:"+index) { - return true - } - } - return false -} - -// MustIndexLookup checks whether the plan for the sql is IndexLookUp. -func (tk *TestKit) MustIndexLookup(sql string, args ...interface{}) *Result { - tk.c.Assert(tk.HasPlan(sql, "IndexLookUp", args...), check.IsTrue) - return tk.MustQuery(sql, args...) -} - -// MustTableDual checks whether the plan for the sql is TableDual. -func (tk *TestKit) MustTableDual(sql string, args ...interface{}) *Result { - tk.c.Assert(tk.HasPlan(sql, "TableDual", args...), check.IsTrue) - return tk.MustQuery(sql, args...) -} - -// MustPointGet checks whether the plan for the sql is Point_Get. -func (tk *TestKit) MustPointGet(sql string, args ...interface{}) *Result { - rs := tk.MustQuery("explain "+sql, args...) - tk.c.Assert(len(rs.rows), check.Equals, 1) - tk.c.Assert(strings.Contains(rs.rows[0][0], "Point_Get"), check.IsTrue, check.Commentf("plan %v", rs.rows[0][0])) - return tk.MustQuery(sql, args...) -} - -// MustQuery query the statements and returns result rows. -// If expected result is set it asserts the query result equals expected result. -func (tk *TestKit) MustQuery(sql string, args ...interface{}) *Result { - comment := check.Commentf("sql:%s, args:%v", sql, args) - rs, err := tk.Exec(sql, args...) - tk.c.Assert(err, check.IsNil, comment) - tk.c.Assert(rs, check.NotNil, comment) - return tk.ResultSetToResult(rs, comment) -} - -// MayQuery query the statements and returns result rows if result set is returned. -// If expected result is set it asserts the query result equals expected result. -func (tk *TestKit) MayQuery(sql string, args ...interface{}) *Result { - comment := check.Commentf("sql:%s, args:%v", sql, args) - rs, err := tk.Exec(sql, args...) - tk.c.Assert(err, check.IsNil, comment) - if rs == nil { - var emptyStringAoA [][]string - return &Result{rows: emptyStringAoA, c: tk.c, comment: comment} - } - return tk.ResultSetToResult(rs, comment) -} - -// QueryToErr executes a sql statement and discard results. -func (tk *TestKit) QueryToErr(sql string, args ...interface{}) error { - comment := check.Commentf("sql:%s, args:%v", sql, args) - res, err := tk.Exec(sql, args...) - tk.c.Assert(err, check.IsNil, comment) - tk.c.Assert(res, check.NotNil, comment) - _, resErr := session.GetRows4Test(context.Background(), tk.Se, res) - tk.c.Assert(res.Close(), check.IsNil) - return resErr -} - -// ExecToErr executes a sql statement and discard results. -func (tk *TestKit) ExecToErr(sql string, args ...interface{}) error { - res, err := tk.Exec(sql, args...) - if res != nil { - tk.c.Assert(res.Close(), check.IsNil) - } - return err -} - -// MustGetErrMsg executes a sql statement and assert it's error message. -func (tk *TestKit) MustGetErrMsg(sql string, errStr string) { - err := tk.ExecToErr(sql) - tk.c.Assert(err, check.NotNil) - tk.c.Assert(err.Error(), check.Equals, errStr) -} - -// MustGetErrCode executes a sql statement and assert it's error code. -func (tk *TestKit) MustGetErrCode(sql string, errCode int) { - _, err := tk.Exec(sql) - tk.c.Assert(err, check.NotNil) - originErr := errors.Cause(err) - tErr, ok := originErr.(*terror.Error) - tk.c.Assert(ok, check.IsTrue, check.Commentf("expect type 'terror.Error', but obtain '%T': %v", originErr, originErr)) - sqlErr := terror.ToSQLError(tErr) - tk.c.Assert(int(sqlErr.Code), check.Equals, errCode, check.Commentf("Assertion failed, origin err:\n %v", sqlErr)) -} - -// ResultSetToResult converts sqlexec.RecordSet to testkit.Result. -// It is used to check results of execute statement in binary mode. -func (tk *TestKit) ResultSetToResult(rs sqlexec.RecordSet, comment check.CommentInterface) *Result { - return tk.ResultSetToResultWithCtx(context.Background(), rs, comment) -} - -// ResultSetToResultWithCtx converts sqlexec.RecordSet to testkit.Result. -func (tk *TestKit) ResultSetToResultWithCtx(ctx context.Context, rs sqlexec.RecordSet, comment check.CommentInterface) *Result { - sRows, err := session.ResultSetToStringSlice(ctx, tk.Se, rs) - tk.c.Check(err, check.IsNil, comment) - return &Result{rows: sRows, c: tk.c, comment: comment} -} - -// Rows is similar to RowsWithSep, use white space as separator string. -func Rows(args ...string) [][]interface{} { - return RowsWithSep(" ", args...) -} - -// GetTableID gets table ID by name. -func (tk *TestKit) GetTableID(tableName string) int64 { - dom := domain.GetDomain(tk.Se) - is := dom.InfoSchema() - tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr(tableName)) - tk.c.Assert(err, check.IsNil) - return tbl.Meta().ID -} - -// RowsWithSep is a convenient function to wrap args to a slice of []interface. -// The arg represents a row, split by sep. -func RowsWithSep(sep string, args ...string) [][]interface{} { - rows := make([][]interface{}, len(args)) - for i, v := range args { - strs := strings.Split(v, sep) - row := make([]interface{}, len(strs)) - for j, s := range strs { - row[j] = s - } - rows[i] = row - } - return rows -} diff --git a/util/testkit/testkit_test.go b/util/testkit/testkit_test.go deleted file mode 100644 index 21aceedf868c5..0000000000000 --- a/util/testkit/testkit_test.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2017 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 testkit - -import ( - "testing" - - "github.com/pingcap/check" -) - -var _ = check.Suite(&testKitSuite{}) - -func TestT(t *testing.T) { - check.TestingT(t) -} - -type testKitSuite struct { -} - -func (s testKitSuite) TestSort(c *check.C) { - result := &Result{ - rows: [][]string{{"1", "1", "", ""}, {"2", "2", "2", "3"}}, - c: c, - comment: check.Commentf(""), - } - result.Sort().Check(Rows("1 1 ", "2 2 2 3")) -} diff --git a/util/testleak/add-leaktest.sh b/util/testleak/add-leaktest.sh deleted file mode 100755 index 1ebb778655fb8..0000000000000 --- a/util/testleak/add-leaktest.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh -# Copyright 2019 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. -# -# Usage: add-leaktest.sh pkg/*_test.go - -set -eu - -sed -i'~' -e ' - /^func (s \*test.*Suite) Test.*(c \*C) {/ { - n - /testleak.AfterTest/! i\ - defer testleak.AfterTest(c)() - } -' $@ - -for i in $@; do - if ! cmp -s $i $i~ ; then - goimports -w $i - fi -echo $i - rm -f $i~ -done diff --git a/util/testleak/check-leaktest.sh b/util/testleak/check-leaktest.sh deleted file mode 100755 index add7f923cd0cb..0000000000000 --- a/util/testleak/check-leaktest.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/sh -# Copyright 2019 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. -# -# Usage: check-leaktest.sh -# It needs to run under the github.com/pingcap/tidb directory. - -set -e - -pkgs=$(git grep 'Suite' |grep -vE "Godeps|tags" |awk -F: '{print $1}' | xargs -n1 dirname | sort |uniq) -echo $pkgs -for pkg in ${pkgs}; do - if [ -z "$(ls ${pkg}/*_test.go 2>/dev/null)" ]; then - continue - fi - awk -F'[(]' ' -/func \(s .*Suite\) Test.*C\) {/ { - test = $1"("$2 - next -} - -/defer testleak.AfterTest/ { - test = 0 - next -} - -{ - if (test && (FILENAME != "./tidb_test.go")) { - printf "%s: %s: missing defer testleak.AfterTest\n", FILENAME, test - test = 0 - code = 1 - } -} - -END { - exit code -} - -' ${pkg}/*_test.go -done diff --git a/util/testleak/fake.go b/util/testleak/fake.go deleted file mode 100644 index 22f74ad482129..0000000000000 --- a/util/testleak/fake.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2017 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. -//go:build !leak - -package testleak - -import ( - "testing" - - "github.com/pingcap/check" -) - -// BeforeTest is a dummy implementation when build tag 'leak' is not set. -func BeforeTest() { -} - -// AfterTest is a dummy implementation when build tag 'leak' is not set. -func AfterTest(c *check.C) func() { - return func() { - } -} - -// AfterTestT is used after all the test cases is finished. -func AfterTestT(t *testing.T) func() { - return func() { - } -} diff --git a/util/testleak/leaktest.go b/util/testleak/leaktest.go deleted file mode 100644 index 7c0c4a57fe225..0000000000000 --- a/util/testleak/leaktest.go +++ /dev/null @@ -1,182 +0,0 @@ -// 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, -// 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. - -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build leak - -package testleak - -import ( - "runtime" - "sort" - "strings" - "testing" - "time" - - "github.com/pingcap/check" -) - -func interestingGoroutines() (gs []string) { - buf := make([]byte, 2<<20) - buf = buf[:runtime.Stack(buf, true)] - ignoreList := []string{ - "created by github.com/pingcap/tidb.init", - "testing.RunTests", - "check.(*resultTracker).start", - "check.(*suiteRunner).runFunc", - "check.(*suiteRunner).parallelRun", - "localstore.(*dbStore).scheduler", - "ddl.(*ddl).start", - "ddl.(*delRange).startEmulator", - "domain.NewDomain", - "testing.(*T).Run", - "domain.(*Domain).LoadPrivilegeLoop", - "domain.(*Domain).UpdateTableStatsLoop", - "testing.Main(", - "runtime.goexit", - "created by runtime.gc", - "interestingGoroutines", - "runtime.MHeap_Scavenger", - "created by os/signal.init", - "gopkg.in/natefinch/lumberjack%2ev2.(*Logger).millRun", - // these go routines are async terminated, so they may still alive after test end, thus cause - // false positive leak failures - "google.golang.org/grpc.(*addrConn).resetTransport", - "google.golang.org/grpc.(*ccBalancerWrapper).watcher", - "github.com/pingcap/goleveldb/leveldb/util.(*BufferPool).drain", - "github.com/pingcap/goleveldb/leveldb.(*DB).compactionError", - "github.com/pingcap/goleveldb/leveldb.(*DB).mpoolDrain", - "go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop", - "go.etcd.io/etcd/v3/pkg/logutil.(*MergeLogger).outputLoop", - "oracles.(*pdOracle).updateTS", - "tikv.(*KVStore).runSafePointChecker", - "tikv.(*RegionCache).asyncCheckAndResolveLoop", - "github.com/pingcap/badger", - "github.com/ngaut/unistore/tikv.(*MVCCStore).runUpdateSafePointLoop", - "github.com/tikv/client-go/v2/txnkv/transaction.keepAlive", // See https://github.com/tikv/client-go/issues/174 - } - shouldIgnore := func(stack string) bool { - if stack == "" { - return true - } - for _, ident := range ignoreList { - if strings.Contains(strings.ToLower(stack), strings.ToLower(ident)) { - return true - } - } - return false - } - for _, g := range strings.Split(string(buf), "\n\n") { - sl := strings.SplitN(g, "\n", 2) - if len(sl) != 2 { - continue - } - stack := strings.TrimSpace(sl[1]) - if shouldIgnore(stack) { - continue - } - gs = append(gs, stack) - } - sort.Strings(gs) - return -} - -var beforeTestGoroutines = map[string]bool{} -var testGoroutinesInited bool - -// BeforeTest gets the current goroutines. -// It's used for check.Suite.SetUpSuite() function. -// Now it's only used in the tidb_test.go. -// Note: it's not accurate, consider the following function: -// func loop() { -// for { -// select { -// case <-ticker.C: -// DoSomething() -// } -// } -// } -// If this loop step into DoSomething() during BeforeTest(), the stack for this goroutine will contain DoSomething(). -// Then if this loop jumps out of DoSomething during AfterTest(), the stack for this goroutine will not contain DoSomething(). -// Resulting in false-positive leak reports. -func BeforeTest() { - for _, g := range interestingGoroutines() { - beforeTestGoroutines[g] = true - } - testGoroutinesInited = true -} - -const defaultCheckCnt = 50 - -func checkLeakAfterTest(errorFunc func(cnt int, g string)) func() { - // After `BeforeTest`, `beforeTestGoroutines` may still be empty, in this case, - // we shouldn't init it again. - if !testGoroutinesInited && len(beforeTestGoroutines) == 0 { - for _, g := range interestingGoroutines() { - beforeTestGoroutines[g] = true - } - } - - cnt := defaultCheckCnt - return func() { - defer func() { - beforeTestGoroutines = map[string]bool{} - testGoroutinesInited = false - }() - - var leaked []string - for i := 0; i < cnt; i++ { - leaked = leaked[:0] - for _, g := range interestingGoroutines() { - if !beforeTestGoroutines[g] { - leaked = append(leaked, g) - } - } - // Bad stuff found, but goroutines might just still be - // shutting down, so give it some time. - if len(leaked) != 0 { - time.Sleep(50 * time.Millisecond) - continue - } - - return - } - for _, g := range leaked { - errorFunc(cnt, g) - } - } -} - -// AfterTest gets the current goroutines and runs the returned function to -// get the goroutines at that time to contrast whether any goroutines leaked. -// Usage: defer testleak.AfterTest(c)() -// It can call with BeforeTest() at the beginning of check.Suite.TearDownSuite() or -// call alone at the beginning of each test. -func AfterTest(c *check.C) func() { - errorFunc := func(cnt int, g string) { - c.Errorf("Test %s check-count %d appears to have leaked: %v", c.TestName(), cnt, g) - } - return checkLeakAfterTest(errorFunc) -} - -// AfterTestT is used after all the test cases is finished. -func AfterTestT(t *testing.T) func() { - errorFunc := func(cnt int, g string) { - t.Errorf("Test %s check-count %d appears to have leaked: %v", t.Name(), cnt, g) - } - return checkLeakAfterTest(errorFunc) -} diff --git a/util/texttree/main_test.go b/util/texttree/main_test.go index 1d151ccf15a7f..7c600e422f8fc 100644 --- a/util/texttree/main_test.go +++ b/util/texttree/main_test.go @@ -17,12 +17,12 @@ package texttree import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/timeutil/main_test.go b/util/timeutil/main_test.go index 956115bf19d26..78c88bee38734 100644 --- a/util/timeutil/main_test.go +++ b/util/timeutil/main_test.go @@ -17,12 +17,12 @@ package timeutil import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/topsql/collector/main_test.go b/util/topsql/collector/main_test.go index e468d0e0add26..fff68f39a3ab0 100644 --- a/util/topsql/collector/main_test.go +++ b/util/topsql/collector/main_test.go @@ -19,16 +19,16 @@ import ( "testing" "time" + "github.com/pingcap/tidb/testkit/testsetup" "github.com/pingcap/tidb/util/cpuprofile" "github.com/pingcap/tidb/util/cpuprofile/testutil" - "github.com/pingcap/tidb/util/testbridge" topsqlstate "github.com/pingcap/tidb/util/topsql/state" "github.com/stretchr/testify/require" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/topsql/main_test.go b/util/topsql/main_test.go index f8c550b98775e..459565bff263c 100644 --- a/util/topsql/main_test.go +++ b/util/topsql/main_test.go @@ -17,12 +17,12 @@ package topsql import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/topsql/reporter/main_test.go b/util/topsql/reporter/main_test.go index 3df1502203f86..217cca60068c6 100644 --- a/util/topsql/reporter/main_test.go +++ b/util/topsql/reporter/main_test.go @@ -17,12 +17,12 @@ package reporter import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/topsql/stmtstats/main_test.go b/util/topsql/stmtstats/main_test.go index 4285d133b824a..9b0ca5ed84560 100644 --- a/util/topsql/stmtstats/main_test.go +++ b/util/topsql/stmtstats/main_test.go @@ -17,12 +17,12 @@ package stmtstats import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/tracing/main_test.go b/util/tracing/main_test.go index bcfcec7bee10b..9a76d536ffeb7 100644 --- a/util/tracing/main_test.go +++ b/util/tracing/main_test.go @@ -17,12 +17,12 @@ package tracing import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), diff --git a/util/vitess/main_test.go b/util/vitess/main_test.go index 3096513f8a19c..78ec592071a0f 100644 --- a/util/vitess/main_test.go +++ b/util/vitess/main_test.go @@ -17,12 +17,12 @@ package vitess import ( "testing" - "github.com/pingcap/tidb/util/testbridge" + "github.com/pingcap/tidb/testkit/testsetup" "go.uber.org/goleak" ) func TestMain(m *testing.M) { - testbridge.SetupForCommonTest() + testsetup.SetupForCommonTest() opts := []goleak.Option{ goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"),