diff --git a/bindinfo/BUILD.bazel b/bindinfo/BUILD.bazel index a95b8f4fd0421..07ab6b5e0a098 100644 --- a/bindinfo/BUILD.bazel +++ b/bindinfo/BUILD.bazel @@ -67,10 +67,11 @@ go_test( "//parser/auth", "//parser/model", "//parser/terror", - "//session/txninfo", + "//server", "//sessionctx/variable", "//testkit", "//testkit/testsetup", + "//testkit/testutil", "//util", "//util/hack", "//util/parser", diff --git a/bindinfo/bind_test.go b/bindinfo/bind_test.go index ee8a1959bab2a..f1bb895e747a9 100644 --- a/bindinfo/bind_test.go +++ b/bindinfo/bind_test.go @@ -28,6 +28,7 @@ import ( "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/parser/terror" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/stretchr/testify/require" ) @@ -47,7 +48,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t1:idx_b", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res := tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "idx_b(b)"), res.Rows()) tk.MustExec("execute stmt1;") @@ -59,7 +60,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t1:idx_c", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "idx_c(c)"), res.Rows()) @@ -67,7 +68,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt2;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "HashJoin"), res.Rows()) tk.MustExec("execute stmt2;") @@ -78,7 +79,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt2;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "IndexJoin"), res.Rows()) @@ -87,7 +88,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t1:idx_b", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "idx_b(b)"), res.Rows()) tk.MustExec("execute stmt3;") @@ -99,7 +100,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t1:idx_c", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "idx_c(c)"), res.Rows()) @@ -107,7 +108,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt4;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "HashJoin"), res.Rows()) tk.MustExec("execute stmt4;") @@ -118,7 +119,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt4;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "IndexJoin"), res.Rows()) @@ -127,7 +128,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t2:idx_b", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "idx_b(b)"), res.Rows()) tk.MustExec("execute stmt5;") @@ -139,7 +140,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t2:idx_b", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "idx_b(b)"), res.Rows()) @@ -150,7 +151,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t2:idx_c", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "idx_c(c)"), res.Rows()) @@ -159,7 +160,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t2:idx_b", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "idx_b(b)"), res.Rows()) tk.MustExec("execute stmt6;") @@ -171,7 +172,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t2:idx_c", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "idx_c(c)"), res.Rows()) @@ -185,7 +186,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt1;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "HashJoin")) tk.MustExec("execute stmt1;") @@ -195,7 +196,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt2;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "MergeJoin")) tk.MustExec("execute stmt2;") @@ -206,7 +207,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt1;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "MergeJoin")) @@ -217,7 +218,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt1;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "IndexReader")) tk.MustExec("execute stmt1;") @@ -227,7 +228,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt1;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.False(t, tk.HasPlan4ExplainFor(res, "IndexReader")) tk.MustExec("execute stmt1;") @@ -238,7 +239,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt1;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.False(t, tk.HasPlan4ExplainFor(res, "IndexReader")) tk.MustExec("execute stmt1;") @@ -248,7 +249,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt2;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "IndexReader")) tk.MustExec("execute stmt2;") @@ -259,7 +260,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { tk.MustExec("execute stmt1;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.HasPlan4ExplainFor(res, "IndexReader")) @@ -274,7 +275,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t:ia", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "ia(a)"), res.Rows()) tk.MustExec("execute stmt1;") @@ -287,7 +288,7 @@ func TestPrepareCacheWithBinding(t *testing.T) { require.Equal(t, "t:ib", tk.Session().GetSessionVars().StmtCtx.IndexNames[0]) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.True(t, tk.MustUseIndex4ExplainFor(res, "ib(b)"), res.Rows()) } diff --git a/bindinfo/session_handle_test.go b/bindinfo/session_handle_test.go index 73bb271c2106f..a60f8ff41cd12 100644 --- a/bindinfo/session_handle_test.go +++ b/bindinfo/session_handle_test.go @@ -16,7 +16,6 @@ package bindinfo_test import ( "context" - "crypto/tls" "strconv" "testing" "time" @@ -26,9 +25,8 @@ import ( "github.com/pingcap/tidb/metrics" "github.com/pingcap/tidb/parser" "github.com/pingcap/tidb/parser/auth" - "github.com/pingcap/tidb/session/txninfo" + "github.com/pingcap/tidb/server" "github.com/pingcap/tidb/testkit" - "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/stmtsummary" dto "github.com/prometheus/client_model/go" "github.com/stretchr/testify/require" @@ -361,56 +359,15 @@ func TestDefaultDB(t *testing.T) { tk.MustQuery("show session bindings").Check(testkit.Rows()) } -type mockSessionManager struct { - PS []*util.ProcessInfo -} - -func (msm *mockSessionManager) ShowTxnList() []*txninfo.TxnInfo { - panic("unimplemented!") -} - -func (msm *mockSessionManager) ShowProcessList() map[uint64]*util.ProcessInfo { - ret := make(map[uint64]*util.ProcessInfo) - for _, item := range msm.PS { - ret[item.ID] = item - } - return ret -} - -func (msm *mockSessionManager) GetProcessInfo(id uint64) (*util.ProcessInfo, bool) { - for _, item := range msm.PS { - if item.ID == id { - return item, true - } - } - return &util.ProcessInfo{}, false -} - -func (msm *mockSessionManager) Kill(cid uint64, query bool) { -} - -func (msm *mockSessionManager) KillAllConnections() { -} - -func (msm *mockSessionManager) UpdateTLSConfig(cfg *tls.Config) { -} - -func (msm *mockSessionManager) ServerID() uint64 { - return 1 -} - -func (msm *mockSessionManager) StoreInternalSession(se interface{}) {} - -func (msm *mockSessionManager) DeleteInternalSession(se interface{}) {} - -func (msm *mockSessionManager) GetInternalSessionStartTSList() []uint64 { - return nil -} - func TestIssue19836(t *testing.T) { - store := testkit.CreateMockStore(t) + store, dom := testkit.CreateMockStoreAndDomain(t) + sv := server.CreateMockServer(t, store) + sv.SetDomain(dom) + defer sv.Close() + + conn1 := server.CreateMockConn(t, sv) + tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session) - tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("drop table if exists t") tk.MustExec("create table t(a int, key (a));") @@ -419,9 +376,6 @@ func TestIssue19836(t *testing.T) { tk.MustExec("set @a=1;") tk.MustExec("set @b=2;") tk.MustExec("EXECUTE stmt USING @a, @b;") - tk.Session().SetSessionManager(&mockSessionManager{ - PS: []*util.ProcessInfo{tk.Session().ShowProcess()}, - }) explainResult := testkit.Rows( "Limit_8 2.00 0 root time:0s, loops:0 offset:1, count:2 N/A N/A", "└─TableReader_13 3.00 0 root time:0s, loops:0 data:Limit_12 N/A N/A", diff --git a/br/pkg/restore/BUILD.bazel b/br/pkg/restore/BUILD.bazel index a014ed024f182..5f7e7f79bcd87 100644 --- a/br/pkg/restore/BUILD.bazel +++ b/br/pkg/restore/BUILD.bazel @@ -32,6 +32,7 @@ go_library( "//br/pkg/pdutil", "//br/pkg/redact", "//br/pkg/restore/split", + "//br/pkg/restore/tiflashrec", "//br/pkg/rtree", "//br/pkg/storage", "//br/pkg/stream", @@ -115,6 +116,7 @@ go_test( "//br/pkg/metautil", "//br/pkg/mock", "//br/pkg/restore/split", + "//br/pkg/restore/tiflashrec", "//br/pkg/rtree", "//br/pkg/storage", "//br/pkg/stream", diff --git a/br/pkg/restore/tiflashrec/BUILD.bazel b/br/pkg/restore/tiflashrec/BUILD.bazel new file mode 100644 index 0000000000000..2a134c9e28b22 --- /dev/null +++ b/br/pkg/restore/tiflashrec/BUILD.bazel @@ -0,0 +1,29 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "tiflashrec", + srcs = ["tiflash_recorder.go"], + importpath = "github.com/pingcap/tidb/br/pkg/restore/tiflashrec", + visibility = ["//visibility:public"], + deps = [ + "//br/pkg/logutil", + "//br/pkg/utils", + "//infoschema", + "//parser/ast", + "//parser/format", + "//parser/model", + "@com_github_pingcap_log//:log", + "@org_uber_go_zap//:zap", + ], +) + +go_test( + name = "tiflashrec_test", + srcs = ["tiflash_recorder_test.go"], + deps = [ + ":tiflashrec", + "//infoschema", + "//parser/model", + "@com_github_stretchr_testify//require", + ], +) diff --git a/br/pkg/task/BUILD.bazel b/br/pkg/task/BUILD.bazel index 99df7164961c4..04a4bc5a6249b 100644 --- a/br/pkg/task/BUILD.bazel +++ b/br/pkg/task/BUILD.bazel @@ -24,6 +24,7 @@ go_library( "//br/pkg/metautil", "//br/pkg/pdutil", "//br/pkg/restore", + "//br/pkg/restore/tiflashrec", "//br/pkg/rtree", "//br/pkg/storage", "//br/pkg/stream", @@ -34,11 +35,13 @@ go_library( "//br/pkg/version", "//config", "//kv", + "//parser/model", "//parser/mysql", "//sessionctx/stmtctx", "//sessionctx/variable", "//statistics/handle", "//types", + "//util", "//util/mathutil", "//util/sqlexec", "//util/table-filter", diff --git a/ddl/db_test.go b/ddl/db_test.go index f4ac4d9f592f5..82eb5e0c7977d 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -46,6 +46,7 @@ import ( "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/testkit/external" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/dbterror" @@ -1577,7 +1578,7 @@ func TestReportingMinStartTimestamp(t *testing.T) { _, dom := testkit.CreateMockStoreAndDomainWithSchemaLease(t, dbTestLease) infoSyncer := dom.InfoSyncer() - sm := &testkit.MockSessionManager{ + sm := &testutil.MockSessionManager{ PS: make([]*util.ProcessInfo, 0), } infoSyncer.SetSessionManager(sm) diff --git a/executor/BUILD.bazel b/executor/BUILD.bazel index 0e7ab7a56ca44..b73396d1e2760 100644 --- a/executor/BUILD.bazel +++ b/executor/BUILD.bazel @@ -352,6 +352,7 @@ go_test( "//parser", "//parser/ast", "//parser/auth", + "//parser/charset", "//parser/model", "//parser/mysql", "//parser/terror", @@ -361,7 +362,6 @@ go_test( "//planner/util", "//server", "//session", - "//session/txninfo", "//sessionctx", "//sessionctx/binloginfo", "//sessionctx/stmtctx", diff --git a/executor/aggfuncs/BUILD.bazel b/executor/aggfuncs/BUILD.bazel index 92d1cd9eae8bf..1bb3c1133eec9 100644 --- a/executor/aggfuncs/BUILD.bazel +++ b/executor/aggfuncs/BUILD.bazel @@ -34,6 +34,7 @@ go_library( "//expression", "//expression/aggregation", "//parser/ast", + "//parser/charset", "//parser/mysql", "//planner/core", "//planner/util", @@ -92,6 +93,7 @@ go_test( "//expression", "//expression/aggregation", "//parser/ast", + "//parser/charset", "//parser/mysql", "//planner/util", "//sessionctx", diff --git a/executor/brie_test.go b/executor/brie_test.go index b4eca0564da45..fdebebacbb7a5 100644 --- a/executor/brie_test.go +++ b/executor/brie_test.go @@ -28,6 +28,7 @@ import ( "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/planner/core" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/mock" @@ -78,7 +79,7 @@ func TestFetchShowBRIE(t *testing.T) { Info: "", } ps = append(ps, pi) - sm := &mockSessionManager{ + sm := &testutil.MockSessionManager{ PS: ps, } diff --git a/executor/cluster_table_test.go b/executor/cluster_table_test.go index c2970474a1ca5..58dd6b10e8eda 100644 --- a/executor/cluster_table_test.go +++ b/executor/cluster_table_test.go @@ -31,13 +31,14 @@ import ( "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/server" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/stretchr/testify/require" "google.golang.org/grpc" ) func createRPCServer(t *testing.T, dom *domain.Domain) *grpc.Server { - sm := &testkit.MockSessionManager{} + sm := &testutil.MockSessionManager{} sm.PS = append(sm.PS, &util.ProcessInfo{ ID: 1, User: "root", diff --git a/executor/executor_issue_test.go b/executor/executor_issue_test.go index 8e8b4d28f0fa3..35ec5a76e2842 100644 --- a/executor/executor_issue_test.go +++ b/executor/executor_issue_test.go @@ -31,6 +31,7 @@ import ( "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/statistics" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/stretchr/testify/require" ) @@ -475,7 +476,7 @@ func TestIndexJoin31494(t *testing.T) { insertStr += fmt.Sprintf(", (%d, %d, %d)", i, i, i) } tk.MustExec(insertStr) - sm := &testkit.MockSessionManager{ + sm := &testutil.MockSessionManager{ PS: make([]*util.ProcessInfo, 0), } tk.Session().SetSessionManager(sm) diff --git a/executor/executor_pkg_test.go b/executor/executor_pkg_test.go index 0b62a684f6195..7253bdc3ceb42 100644 --- a/executor/executor_pkg_test.go +++ b/executor/executor_pkg_test.go @@ -16,7 +16,6 @@ package executor import ( "context" - "crypto/tls" "runtime" "strconv" "testing" @@ -31,9 +30,9 @@ import ( "github.com/pingcap/tidb/parser/auth" "github.com/pingcap/tidb/parser/mysql" plannerutil "github.com/pingcap/tidb/planner/util" - "github.com/pingcap/tidb/session/txninfo" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/tablecodec" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/chunk" @@ -51,60 +50,6 @@ var ( InspectionRules = inspectionRules ) -// mockSessionManager is a mocked session manager which is used for test. -type mockSessionManager struct { - PS []*util.ProcessInfo - serverID uint64 -} - -func (msm *mockSessionManager) ShowTxnList() []*txninfo.TxnInfo { - panic("unimplemented!") -} - -// ShowProcessList implements the SessionManager.ShowProcessList interface. -func (msm *mockSessionManager) ShowProcessList() map[uint64]*util.ProcessInfo { - ret := make(map[uint64]*util.ProcessInfo) - for _, item := range msm.PS { - ret[item.ID] = item - } - return ret -} - -func (msm *mockSessionManager) GetProcessInfo(id uint64) (*util.ProcessInfo, bool) { - for _, item := range msm.PS { - if item.ID == id { - return item, true - } - } - return &util.ProcessInfo{}, false -} - -// Kill implements the SessionManager.Kill interface. -func (msm *mockSessionManager) Kill(cid uint64, query bool) { -} - -func (msm *mockSessionManager) KillAllConnections() { -} - -func (msm *mockSessionManager) UpdateTLSConfig(cfg *tls.Config) { -} - -func (msm *mockSessionManager) ServerID() uint64 { - return msm.serverID -} - -func (msm *mockSessionManager) SetServerID(serverID uint64) { - msm.serverID = serverID -} - -func (msm *mockSessionManager) StoreInternalSession(se interface{}) {} - -func (msm *mockSessionManager) DeleteInternalSession(se interface{}) {} - -func (msm *mockSessionManager) GetInternalSessionStartTSList() []uint64 { - return nil -} - func TestShowProcessList(t *testing.T) { // Compose schema. names := []string{"Id", "User", "Host", "db", "Command", "Time", "State", "Info"} @@ -124,7 +69,7 @@ func TestShowProcessList(t *testing.T) { Info: "", } ps = append(ps, pi) - sm := &mockSessionManager{ + sm := &testutil.MockSessionManager{ PS: ps, } sctx := mock.NewContext() diff --git a/executor/executor_test.go b/executor/executor_test.go index efa732c92523b..e8dd7541d1d90 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -60,6 +60,7 @@ import ( "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/testkit/testdata" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/dbterror" @@ -3500,7 +3501,7 @@ func TestOOMPanicAction(t *testing.T) { tk.MustExec("use test") tk.MustExec("create table t (a int primary key, b double);") tk.MustExec("insert into t values (1,1)") - sm := &testkit.MockSessionManager{ + sm := &testutil.MockSessionManager{ PS: make([]*util.ProcessInfo, 0), } tk.Session().SetSessionManager(sm) @@ -5930,7 +5931,7 @@ func TestSummaryFailedUpdate(t *testing.T) { tk.MustExec("drop table if exists t") tk.MustExec("create table t(a int, b int as(-a))") tk.MustExec("insert into t(a) values(1), (3), (7)") - sm := &testkit.MockSessionManager{ + sm := &testutil.MockSessionManager{ PS: make([]*util.ProcessInfo, 0), } tk.Session().SetSessionManager(sm) diff --git a/executor/explainfor_test.go b/executor/explainfor_test.go index 77b15c292747a..cf29ceb4684c5 100644 --- a/executor/explainfor_test.go +++ b/executor/explainfor_test.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/stretchr/testify/require" ) @@ -46,8 +47,8 @@ func TestExplainFor(t *testing.T) { tkRoot.MustQuery("select * from t1;") tkRootProcess := tkRoot.Session().ShowProcess() ps := []*util.ProcessInfo{tkRootProcess} - tkRoot.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) - tkUser.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tkRoot.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) + tkUser.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tkRoot.MustQuery(fmt.Sprintf("explain for connection %d", tkRootProcess.ID)).Check(testkit.Rows( "TableReader_5 10000.00 root data:TableFullScan_4", "└─TableFullScan_4 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo", @@ -56,8 +57,8 @@ func TestExplainFor(t *testing.T) { check := func() { tkRootProcess = tkRoot.Session().ShowProcess() ps = []*util.ProcessInfo{tkRootProcess} - tkRoot.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) - tkUser.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tkRoot.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) + tkUser.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rows := tkRoot.MustQuery(fmt.Sprintf("explain for connection %d", tkRootProcess.ID)).Rows() require.Len(t, rows, 2) require.Len(t, rows[0], 9) @@ -88,7 +89,7 @@ func TestExplainFor(t *testing.T) { tkRootProcess.Plan = nil ps = []*util.ProcessInfo{tkRootProcess} - tkRoot.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tkRoot.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tkRoot.MustExec(fmt.Sprintf("explain for connection %d", tkRootProcess.ID)) } @@ -104,8 +105,8 @@ func TestExplainForVerbose(t *testing.T) { tk.MustQuery("select * from t1;") tkRootProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkRootProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) - tk2.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) + tk2.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rs := tk.MustQuery("explain format = 'verbose' select * from t1").Rows() rs2 := tk2.MustQuery(fmt.Sprintf("explain format = 'verbose' for connection %d", tkRootProcess.ID)).Rows() @@ -120,8 +121,8 @@ func TestExplainForVerbose(t *testing.T) { tk.MustQuery("select * from t2;") tkRootProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkRootProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) - tk2.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) + tk2.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rs = tk.MustQuery("explain format = 'verbose' select * from t2").Rows() rs2 = tk2.MustQuery(fmt.Sprintf("explain format = 'verbose' for connection %d", tkRootProcess.ID)).Rows() require.Len(t, rs, len(rs2)) @@ -156,8 +157,8 @@ func TestIssue11124(t *testing.T) { tk.MustQuery("select t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1'") tkRootProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkRootProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) - tk2.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) + tk2.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rs := tk.MustQuery("explain select t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1'").Rows() rs2 := tk2.MustQuery(fmt.Sprintf("explain for connection %d", tkRootProcess.ID)).Rows() @@ -255,7 +256,7 @@ func TestExplainForConnPlanCache(t *testing.T) { // single test tk1.MustExec(executeQuery) - tk2.Session().SetSessionManager(&testkit.MockSessionManager{ + tk2.Session().SetSessionManager(&testutil.MockSessionManager{ PS: []*util.ProcessInfo{tk1.Session().ShowProcess()}, }) tk2.MustQuery(explainQuery).Check(explainResult) @@ -275,7 +276,7 @@ func TestExplainForConnPlanCache(t *testing.T) { wg.Run(func() { for i := 0; i < repeats; i++ { - tk2.Session().SetSessionManager(&testkit.MockSessionManager{ + tk2.Session().SetSessionManager(&testutil.MockSessionManager{ PS: []*util.ProcessInfo{tk1.Session().ShowProcess()}, }) tk2.MustQuery(explainQuery).Check(explainResult) @@ -321,7 +322,7 @@ func TestExplainDotForExplainPlan(t *testing.T) { tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tk.MustQuery(fmt.Sprintf("explain format=\"dot\" for connection %s", connID)).Check(nil) } @@ -337,7 +338,7 @@ func TestExplainDotForQuery(t *testing.T) { tk.MustQuery("select 1") tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) expected := tk2.MustQuery("explain format=\"dot\" select 1").Rows() got := tk.MustQuery(fmt.Sprintf("explain format=\"dot\" for connection %s", connID)).Rows() @@ -458,7 +459,7 @@ func TestPointGetUserVarPlanCache(t *testing.T) { )) tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Check(testkit.Rows( // can use idx_a `Projection_9 1.00 root test.t1.a, test.t1.b, test.t2.a, test.t2.b`, `└─IndexJoin_17 1.00 root inner join, inner:TableReader_13, outer key:test.t2.a, inner key:test.t1.a, equal cond:eq(test.t2.a, test.t1.a)`, @@ -474,7 +475,7 @@ func TestPointGetUserVarPlanCache(t *testing.T) { )) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Check(testkit.Rows( // can use idx_a `Projection_9 1.00 root test.t1.a, test.t1.b, test.t2.a, test.t2.b`, `└─IndexJoin_17 1.00 root inner join, inner:TableReader_13, outer key:test.t2.a, inner key:test.t1.a, equal cond:eq(test.t2.a, test.t1.a)`, @@ -503,7 +504,7 @@ func TestExpressionIndexPreparePlanCache(t *testing.T) { tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res := tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 4) require.Regexp(t, ".*expression_index.*", res.Rows()[2][3]) @@ -535,7 +536,7 @@ func TestIssue28259(t *testing.T) { tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res := tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 3) require.Regexp(t, ".*Selection.*", res.Rows()[0][0]) @@ -547,7 +548,7 @@ func TestIssue28259(t *testing.T) { tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 4) require.Regexp(t, ".*Selection.*", res.Rows()[0][0]) @@ -571,7 +572,7 @@ func TestIssue28259(t *testing.T) { tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows("1")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 5) require.Regexp(t, ".*Selection.*", res.Rows()[1][0]) @@ -585,7 +586,7 @@ func TestIssue28259(t *testing.T) { tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 5) require.Regexp(t, ".*Selection.*", res.Rows()[1][0]) @@ -617,7 +618,7 @@ func TestIssue28259(t *testing.T) { tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows("1")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 6) require.Regexp(t, ".*Selection.*", res.Rows()[1][0]) @@ -632,7 +633,7 @@ func TestIssue28259(t *testing.T) { tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 6) require.Regexp(t, ".*Selection.*", res.Rows()[1][0]) @@ -661,7 +662,7 @@ func TestIssue28259(t *testing.T) { tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows("1")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 5) require.Regexp(t, ".*Selection.*", res.Rows()[1][0]) @@ -676,7 +677,7 @@ func TestIssue28259(t *testing.T) { tk.MustQuery("execute stmt using @a,@b,@c;").Check(testkit.Rows()) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 5) require.Regexp(t, ".*Selection.*", res.Rows()[1][0]) @@ -707,7 +708,7 @@ func TestIssue28259(t *testing.T) { tk.MustQuery("execute stmt using @a,@b;").Check(testkit.Rows()) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 4) require.Regexp(t, ".*Selection.*", res.Rows()[0][0]) @@ -733,7 +734,7 @@ func TestIssue28696(t *testing.T) { tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res := tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 6) require.Regexp(t, ".*Selection.*", res.Rows()[1][0]) @@ -779,7 +780,7 @@ func TestIndexMerge4PlanCache(t *testing.T) { tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res := tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 7) require.Regexp(t, ".*Selection.*", res.Rows()[1][0]) @@ -799,7 +800,7 @@ func TestIndexMerge4PlanCache(t *testing.T) { tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Len(t, res.Rows(), 6) require.Regexp(t, ".*Selection.*", res.Rows()[0][0]) @@ -820,7 +821,7 @@ func TestIndexMerge4PlanCache(t *testing.T) { tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Regexp(t, ".*IndexMerge.*", res.Rows()[1][0]) @@ -829,7 +830,7 @@ func TestIndexMerge4PlanCache(t *testing.T) { tk.MustQuery("execute stmt using @b;").Check(testkit.Rows("3 ddcdsaf 3")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Regexp(t, ".*IndexMerge.*", res.Rows()[1][0]) @@ -997,7 +998,7 @@ func TestSPM4PlanCache(t *testing.T) { tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Regexp(t, ".*TableReader.*", res.Rows()[0][0]) require.Regexp(t, ".*TableFullScan.*", res.Rows()[1][0]) @@ -1015,7 +1016,7 @@ func TestSPM4PlanCache(t *testing.T) { tk.MustQuery("execute stmt;").Check(testkit.Rows()) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) // We can use the new binding. require.Regexp(t, ".*IndexReader.*", res.Rows()[0][0]) diff --git a/executor/index_merge_reader_test.go b/executor/index_merge_reader_test.go index 2e33eef27d12d..7e3f8a9ea651d 100644 --- a/executor/index_merge_reader_test.go +++ b/executor/index_merge_reader_test.go @@ -24,6 +24,7 @@ import ( "time" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/stretchr/testify/require" ) @@ -198,7 +199,7 @@ func TestIndexMergeWithPreparedStmt(t *testing.T) { tk.MustExec("prepare stmt1 from 'select /*+ use_index_merge(t1) */ count(1) from t1 where c1 < ? or c2 < ?';") tk.MustExec("set @a = 10;") tk.MustQuery("execute stmt1 using @a, @a;").Check(testkit.Rows("10")) - tk.Session().SetSessionManager(&testkit.MockSessionManager{ + tk.Session().SetSessionManager(&testutil.MockSessionManager{ PS: []*util.ProcessInfo{tk.Session().ShowProcess()}, }) explainStr := "explain for connection " + strconv.FormatUint(tk.Session().ShowProcess().ID, 10) diff --git a/executor/infoschema_cluster_table_test.go b/executor/infoschema_cluster_table_test.go index 3af92f2654dd3..a9c591b60e89d 100644 --- a/executor/infoschema_cluster_table_test.go +++ b/executor/infoschema_cluster_table_test.go @@ -33,9 +33,9 @@ import ( "github.com/pingcap/tidb/parser/auth" "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/server" - "github.com/pingcap/tidb/session/txninfo" "github.com/pingcap/tidb/store/helper" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/pdapi" "github.com/stretchr/testify/require" @@ -74,16 +74,16 @@ func setUpRPCService(t *testing.T, dom *domain.Domain, addr string) (*grpc.Serve require.NoError(t, err) // Fix issue 9836 - sm := &mockSessionManager{ - processInfoMap: make(map[uint64]*util.ProcessInfo, 1), - serverID: 1, + sm := &testutil.MockSessionManager{ + PS: make([]*util.ProcessInfo, 1), + SerID: 1, } - sm.processInfoMap[1] = &util.ProcessInfo{ + sm.PS = append(sm.PS, &util.ProcessInfo{ ID: 1, User: "root", Host: "127.0.0.1", Command: mysql.ComQuery, - } + }) srv := server.NewRPCServer(config.GetGlobalConfig(), dom, sm) port := lis.Addr().(*net.TCPAddr).Port addr = fmt.Sprintf("127.0.0.1:%d", port) @@ -169,48 +169,6 @@ func (s *infosSchemaClusterTableSuite) setUpMockPDHTTPServer() (*httptest.Server return srv, mockAddr } -type mockSessionManager struct { - processInfoMap map[uint64]*util.ProcessInfo - serverID uint64 -} - -func (sm *mockSessionManager) ShowTxnList() []*txninfo.TxnInfo { - panic("unimplemented!") -} - -func (sm *mockSessionManager) ShowProcessList() map[uint64]*util.ProcessInfo { - return sm.processInfoMap -} - -func (sm *mockSessionManager) GetProcessInfo(id uint64) (*util.ProcessInfo, bool) { - rs, ok := sm.processInfoMap[id] - return rs, ok -} - -func (sm *mockSessionManager) StoreInternalSession(_ interface{}) { -} - -func (sm *mockSessionManager) DeleteInternalSession(_ interface{}) { -} - -func (sm *mockSessionManager) GetInternalSessionStartTSList() []uint64 { - return nil -} - -func (sm *mockSessionManager) Kill(_ uint64, _ bool) {} - -func (sm *mockSessionManager) KillAllConnections() {} - -func (sm *mockSessionManager) UpdateTLSConfig(_ *tls.Config) {} - -func (sm *mockSessionManager) ServerID() uint64 { - return sm.serverID -} - -func (sm *mockSessionManager) SetServerID(serverID uint64) { - sm.serverID = serverID -} - type mockStore struct { helper.Storage host string diff --git a/executor/join_test.go b/executor/join_test.go index 0b9c1aad9f8be..2b6f1a4e90b8c 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -30,6 +30,7 @@ import ( "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/testkit/testdata" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/stretchr/testify/require" ) @@ -65,7 +66,7 @@ func TestJoinInDisk(t *testing.T) { tk.MustExec("SET GLOBAL tidb_mem_oom_action='LOG'") tk.MustExec("use test") - sm := &testkit.MockSessionManager{ + sm := &testutil.MockSessionManager{ PS: make([]*util.ProcessInfo, 0), } tk.Session().SetSessionManager(sm) diff --git a/executor/memtest/BUILD.bazel b/executor/memtest/BUILD.bazel index 394197a057cc5..f5e23de778ab2 100644 --- a/executor/memtest/BUILD.bazel +++ b/executor/memtest/BUILD.bazel @@ -10,7 +10,6 @@ go_test( flaky = True, deps = [ "//config", - "//executor", "//meta/autoid", "//testkit", "@com_github_stretchr_testify//require", diff --git a/executor/merge_join_test.go b/executor/merge_join_test.go index 01d6d37a8cd65..88d83d6d3d767 100644 --- a/executor/merge_join_test.go +++ b/executor/merge_join_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/stretchr/testify/require" ) @@ -247,7 +248,7 @@ func TestShuffleMergeJoinInDisk(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") - sm := &testkit.MockSessionManager{ + sm := &testutil.MockSessionManager{ PS: make([]*util.ProcessInfo, 0), } tk.Session().SetSessionManager(sm) @@ -288,7 +289,7 @@ func TestMergeJoinInDisk(t *testing.T) { tk.MustExec("SET GLOBAL tidb_mem_oom_action='LOG'") tk.MustExec("use test") - sm := &testkit.MockSessionManager{ + sm := &testutil.MockSessionManager{ PS: make([]*util.ProcessInfo, 0), } tk.Session().SetSessionManager(sm) diff --git a/executor/prepared_test.go b/executor/prepared_test.go index f511c2e6f8b72..04db83eaf56c4 100644 --- a/executor/prepared_test.go +++ b/executor/prepared_test.go @@ -15,22 +15,20 @@ package executor_test import ( - "crypto/tls" "fmt" "strconv" "strings" - "sync/atomic" "testing" "github.com/pingcap/tidb/parser/auth" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/parser/mysql" plannercore "github.com/pingcap/tidb/planner/core" - "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/session/txninfo" + "github.com/pingcap/tidb/server" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/testkit/testdata" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/stretchr/testify/require" ) @@ -84,64 +82,19 @@ func TestIgnorePlanCache(t *testing.T) { require.False(t, tk.Session().GetSessionVars().StmtCtx.UseCache) } -type mockSessionManager2 struct { - se session.Session - killed int32 -} - -func (sm *mockSessionManager2) ShowTxnList() []*txninfo.TxnInfo { - panic("unimplemented!") -} - -func (sm *mockSessionManager2) ShowProcessList() map[uint64]*util.ProcessInfo { - pl := make(map[uint64]*util.ProcessInfo) - if pi, ok := sm.GetProcessInfo(0); ok { - pl[pi.ID] = pi - } - return pl -} - -func (sm *mockSessionManager2) GetProcessInfo(_ uint64) (pi *util.ProcessInfo, notNil bool) { - pi = sm.se.ShowProcess() - if pi != nil { - notNil = true - } - return -} - -func (sm *mockSessionManager2) Kill(_ uint64, _ bool) { - atomic.StoreInt32(&sm.killed, 1) - atomic.StoreUint32(&sm.se.GetSessionVars().Killed, 1) -} -func (sm *mockSessionManager2) KillAllConnections() {} -func (sm *mockSessionManager2) UpdateTLSConfig(_ *tls.Config) {} -func (sm *mockSessionManager2) ServerID() uint64 { return 1 } - -func (sm *mockSessionManager2) StoreInternalSession(se interface{}) {} - -func (sm *mockSessionManager2) DeleteInternalSession(se interface{}) {} - -func (sm *mockSessionManager2) GetInternalSessionStartTSList() []uint64 { - return nil -} - func TestPreparedStmtWithHint(t *testing.T) { // see https://github.com/pingcap/tidb/issues/18535 store, dom := testkit.CreateMockStoreAndDomain(t) + sv := server.CreateMockServer(t, store) + sv.SetDomain(dom) + defer sv.Close() - se, err := session.CreateSession4Test(store) - require.NoError(t, err) - tk := testkit.NewTestKit(t, store) - tk.SetSession(se) + conn1 := server.CreateMockConn(t, sv) + tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session) - sm := &mockSessionManager2{ - se: se, - } - se.SetSessionManager(sm) - go dom.ExpensiveQueryHandle().SetSessionManager(sm).Run() + go dom.ExpensiveQueryHandle().SetSessionManager(sv).Run() tk.MustExec("prepare stmt from \"select /*+ max_execution_time(100) */ sleep(10)\"") tk.MustQuery("execute stmt").Check(testkit.Rows("1")) - require.Equal(t, int32(1), atomic.LoadInt32(&sm.killed)) } func TestPreparedNullParam(t *testing.T) { @@ -162,7 +115,7 @@ func TestPreparedNullParam(t *testing.T) { tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Check(testkit.Rows( "TableDual_5 0.00 root rows:0")) } @@ -199,7 +152,7 @@ func TestIssue29850(t *testing.T) { tk.MustQuery(`execute stmt using @w_id, @c_d_id, @c_id`).Check(testkit.Rows()) tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Check(testkit.Rows( // can use PointGet `Projection_7 0.00 root test.customer.c_discount, test.customer.c_last, test.customer.c_credit, test.warehouse.w_tax`, `└─MergeJoin_8 0.00 root inner join, left key:test.customer.c_w_id, right key:test.warehouse.w_id`, @@ -215,7 +168,7 @@ func TestIssue29850(t *testing.T) { tk.MustQuery(`execute stmt using @a1, @a1`).Check(testkit.Rows("1")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Check(testkit.Rows( // cannot use PointGet since it contains a range condition `Selection_7 1.00 root ge(test.t.a, 1), le(test.t.a, 1)`, `└─TableReader_6 1.00 root data:TableRangeScan_5`, @@ -227,7 +180,7 @@ func TestIssue29850(t *testing.T) { tk.MustQuery(`execute stmt using @a1, @a1`).Check(testkit.Rows("1")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Check(testkit.Rows( // cannot use PointGet since it contains a or condition `Selection_7 1.00 root or(eq(test.t.a, 1), eq(test.t.a, 1))`, `└─TableReader_6 1.00 root data:TableRangeScan_5`, @@ -254,7 +207,7 @@ func TestIssue28064(t *testing.T) { tk.MustExec("execute stmt1 using @a, @b, @c;") tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rows := tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)) rows.Check(testkit.Rows("Selection_8 0.00 root eq(test.t28064.a, 123), eq(test.t28064.b, 234), eq(test.t28064.c, 345)", "└─IndexLookUp_7 0.00 root ", @@ -287,7 +240,7 @@ func TestPreparePlanCache4Blacklist(t *testing.T) { tk.MustExec("execute stmt;") tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res := tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)) require.Contains(t, res.Rows()[1][0], "TopN") @@ -302,7 +255,7 @@ func TestPreparePlanCache4Blacklist(t *testing.T) { tk.MustExec("execute stmt;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)) // Plans that have been cached will not be affected by the blacklist. require.Contains(t, res.Rows()[1][0], "TopN") @@ -317,7 +270,7 @@ func TestPreparePlanCache4Blacklist(t *testing.T) { tk.MustExec("execute stmt;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)) require.Equal(t, 3, len(res.Rows())) require.Contains(t, res.Rows()[1][0], "Selection") @@ -335,7 +288,7 @@ func TestPreparePlanCache4Blacklist(t *testing.T) { tk.MustExec("execute stmt;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)) // The expressions can still be pushed down to tikv. require.Equal(t, 3, len(res.Rows())) @@ -379,7 +332,7 @@ func TestPlanCacheClusterIndex(t *testing.T) { tk.MustQuery("execute stmt1 using @v1,@v2").Check(testkit.Rows("3 3 333")) tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rows := tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Rows() require.Equal(t, 0, strings.Index(rows[len(rows)-1][4].(string), `range:("3" "2","3" +inf]`)) // For point get @@ -397,7 +350,7 @@ func TestPlanCacheClusterIndex(t *testing.T) { tk.MustQuery("execute stmt2 using @v1,@v2").Check(testkit.Rows("3 3 333")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rows = tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Rows() require.Equal(t, 0, strings.Index(rows[len(rows)-1][0].(string), `Point_Get`)) // For CBO point get and batch point get @@ -425,7 +378,7 @@ func TestPlanCacheClusterIndex(t *testing.T) { tk.MustQuery(`execute stmt1 using @v2`).Check(testkit.Rows("b 2 2 2")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rows = tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Rows() require.True(t, strings.Contains(rows[3][0].(string), `TableRangeScan`)) @@ -525,7 +478,7 @@ func TestPlanCacheWithDifferentVariableTypes(t *testing.T) { tk.MustQuery(exec.ExecuteSQL) tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) plan := tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)) testdata.OnRecord(func() { output[i].Executes[j].SQL = exec.ExecuteSQL @@ -921,7 +874,7 @@ func TestIssue29101(t *testing.T) { tk.MustQuery(`execute s1 using @a,@b,@c`).Check(testkit.Rows()) tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Check(testkit.Rows( // can use IndexJoin `Projection_6 1.00 root test.customer.c_discount, test.customer.c_last, test.customer.c_credit, test.warehouse.w_tax`, `└─IndexJoin_14 1.00 root inner join, inner:TableReader_10, outer key:test.customer.c_w_id, inner key:test.warehouse.w_id, equal cond:eq(test.customer.c_w_id, test.warehouse.w_id)`, @@ -949,7 +902,7 @@ func TestIssue29101(t *testing.T) { tk.MustExec(`execute s1 using @a,@b,@c,@c,@a,@d`) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Check(testkit.Rows( // can use index-join `StreamAgg_9 1.00 root funcs:count(distinct test.stock.s_i_id)->Column#11`, `└─IndexJoin_14 0.03 root inner join, inner:IndexLookUp_13, outer key:test.order_line.ol_i_id, inner key:test.stock.s_i_id, equal cond:eq(test.order_line.ol_i_id, test.stock.s_i_id)`, @@ -1026,7 +979,7 @@ func TestParameterPushDown(t *testing.T) { tk.MustQuery(tt.SQL) tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) plan := tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)) testdata.OnRecord(func() { @@ -1114,7 +1067,7 @@ func TestPreparePlanCache4DifferentSystemVars(t *testing.T) { tk.MustExec("execute stmt;") tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res := tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Equal(t, 4, len(res.Rows())) require.Contains(t, res.Rows()[0][0], "IndexMerge") @@ -1123,7 +1076,7 @@ func TestPreparePlanCache4DifferentSystemVars(t *testing.T) { tk.MustExec("execute stmt;") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Equal(t, 4, len(res.Rows())) require.Contains(t, res.Rows()[0][0], "IndexMerge") @@ -1141,7 +1094,7 @@ func TestPreparePlanCache4DifferentSystemVars(t *testing.T) { tk.MustQuery("execute stmt;").Sort().Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7", "8", "9")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Contains(t, res.Rows()[1][0], "Apply") require.Contains(t, res.Rows()[1][5], "Concurrency") @@ -1150,7 +1103,7 @@ func TestPreparePlanCache4DifferentSystemVars(t *testing.T) { tk.MustQuery("execute stmt;").Sort().Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7", "8", "9")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Contains(t, res.Rows()[1][0], "Apply") executionInfo := fmt.Sprintf("%v", res.Rows()[1][4]) @@ -1171,7 +1124,7 @@ func TestPreparePlanCache4DifferentSystemVars(t *testing.T) { tk.MustQuery("execute stmt;").Sort().Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7", "8", "9")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Contains(t, res.Rows()[1][0], "Apply") require.Contains(t, res.Rows()[1][5], "cache:ON") @@ -1180,7 +1133,7 @@ func TestPreparePlanCache4DifferentSystemVars(t *testing.T) { tk.MustQuery("execute stmt;").Sort().Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7", "8", "9")) tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) res = tk.MustQuery("explain for connection " + strconv.FormatUint(tkProcess.ID, 10)) require.Contains(t, res.Rows()[1][0], "Apply") executionInfo = fmt.Sprintf("%v", res.Rows()[1][5]) @@ -1239,7 +1192,7 @@ func TestPrepareStmtAfterIsolationReadChange(t *testing.T) { tk.MustQuery("execute stmt") tkProcess := tk.Session().ShowProcess() ps := []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rows := tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Rows() require.Equal(t, "cop[tikv]", rows[len(rows)-1][2]) @@ -1247,7 +1200,7 @@ func TestPrepareStmtAfterIsolationReadChange(t *testing.T) { tk.MustExec("execute stmt") tkProcess = tk.Session().ShowProcess() ps = []*util.ProcessInfo{tkProcess} - tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps}) + tk.Session().SetSessionManager(&testutil.MockSessionManager{PS: ps}) rows = tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Rows() require.Equal(t, rows[len(rows)-1][2], "cop[tiflash]") diff --git a/executor/seqtest/BUILD.bazel b/executor/seqtest/BUILD.bazel index 51bb4d30d1e95..83680d339d84b 100644 --- a/executor/seqtest/BUILD.bazel +++ b/executor/seqtest/BUILD.bazel @@ -25,8 +25,8 @@ go_test( "//parser/mysql", "//parser/terror", "//planner/core", + "//server", "//session", - "//session/txninfo", "//sessionctx/variable", "//statistics/handle", "//store/copr", @@ -35,7 +35,6 @@ go_test( "//testkit", "//testkit/testsetup", "//testkit/testutil", - "//util", "//util/gcutil", "//util/kvcache", "@com_github_pingcap_errors//:errors", diff --git a/executor/seqtest/prepared_test.go b/executor/seqtest/prepared_test.go index f8176bbc9cc95..009dbb76e6a3a 100644 --- a/executor/seqtest/prepared_test.go +++ b/executor/seqtest/prepared_test.go @@ -16,7 +16,6 @@ package executor_test import ( "context" - "crypto/tls" "fmt" "math" "testing" @@ -28,10 +27,9 @@ import ( "github.com/pingcap/tidb/parser/ast" "github.com/pingcap/tidb/parser/mysql" plannercore "github.com/pingcap/tidb/planner/core" + "github.com/pingcap/tidb/server" "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/session/txninfo" "github.com/pingcap/tidb/testkit" - "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/kvcache" dto "github.com/prometheus/client_model/go" "github.com/stretchr/testify/require" @@ -743,68 +741,29 @@ func TestPreparedIssue8644(t *testing.T) { } } -// mockSessionManager is a mocked session manager which is used for test. -type mockSessionManager1 struct { - Se session.Session -} - -func (msm *mockSessionManager1) ShowTxnList() []*txninfo.TxnInfo { - panic("unimplemented!") -} - -// ShowProcessList implements the SessionManager.ShowProcessList interface. -func (msm *mockSessionManager1) ShowProcessList() map[uint64]*util.ProcessInfo { - ret := make(map[uint64]*util.ProcessInfo) - return ret -} - -func (msm *mockSessionManager1) GetProcessInfo(_ uint64) (*util.ProcessInfo, bool) { - pi := msm.Se.ShowProcess() - return pi, true -} - -// Kill implements the SessionManager.Kill interface. -func (msm *mockSessionManager1) Kill(_ uint64, _ bool) {} - -func (msm *mockSessionManager1) KillAllConnections() {} - -func (msm *mockSessionManager1) ServerID() uint64 { - return 1 -} - -func (msm *mockSessionManager1) UpdateTLSConfig(_ *tls.Config) {} - -func (msm *mockSessionManager1) StoreInternalSession(se interface{}) { -} - -func (msm *mockSessionManager1) DeleteInternalSession(se interface{}) { -} - -func (msm *mockSessionManager1) GetInternalSessionStartTSList() []uint64 { - return nil -} - func TestPreparedIssue17419(t *testing.T) { store, dom := testkit.CreateMockStoreAndDomain(t) + sv := server.CreateMockServer(t, store) + sv.SetDomain(dom) + defer sv.Close() + + conn1 := server.CreateMockConn(t, sv) + tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session) ctx := context.Background() - tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("drop table if exists t") tk.MustExec("create table t (a int)") tk.MustExec("insert into t (a) values (1), (2), (3)") - tk1 := testkit.NewTestKit(t, store) + conn2 := server.CreateMockConn(t, sv) + tk1 := testkit.NewTestKitWithSession(t, store, conn2.Context().Session) query := "select * from test.t" stmtID, _, _, err := tk1.Session().PrepareStmt(query) require.NoError(t, err) - sm := &mockSessionManager1{ - Se: tk1.Session(), - } - tk1.Session().SetSessionManager(sm) - dom.ExpensiveQueryHandle().SetSessionManager(sm) + dom.ExpensiveQueryHandle().SetSessionManager(sv) rs, err := tk1.Session().ExecutePreparedStmt(ctx, stmtID, expression.Args2Expressions4Test()) require.NoError(t, err) diff --git a/executor/simple_test.go b/executor/simple_test.go index e3110d47036b4..2cad7698d2522 100644 --- a/executor/simple_test.go +++ b/executor/simple_test.go @@ -15,16 +15,25 @@ package executor_test import ( + "fmt" "strconv" "testing" "github.com/pingcap/tidb/config" + "github.com/pingcap/tidb/server" "github.com/pingcap/tidb/testkit" "github.com/pingcap/tidb/util" ) func TestKillStmt(t *testing.T) { - store := testkit.CreateMockStore(t) + store, dom := testkit.CreateMockStoreAndDomain(t) + sv := server.CreateMockServer(t, store) + sv.SetDomain(dom) + defer sv.Close() + + conn1 := server.CreateMockConn(t, sv) + tk := testkit.NewTestKitWithSession(t, store, conn1.Context().Session) + originCfg := config.GetGlobalConfig() newCfg := *originCfg newCfg.EnableGlobalKill = false @@ -33,13 +42,10 @@ func TestKillStmt(t *testing.T) { config.StoreGlobalConfig(originCfg) }() - tk := testkit.NewTestKit(t, store) + connID := conn1.ID() + tk.MustExec("use test") - sm := &mockSessionManager{ - serverID: 0, - } - tk.Session().SetSessionManager(sm) - tk.MustExec("kill 1") + tk.MustExec(fmt.Sprintf("kill %d", connID)) result := tk.MustQuery("show warnings") result.Check(testkit.Rows("Warning 1105 Invalid operation. Please use 'KILL TIDB [CONNECTION | QUERY] connectionID' instead")) @@ -53,7 +59,6 @@ func TestKillStmt(t *testing.T) { result.Check(testkit.Rows("Warning 1105 Kill failed: Received a 32bits truncated ConnectionID, expect 64bits. Please execute 'KILL [CONNECTION | QUERY] ConnectionID' to send a Kill without truncating ConnectionID.")) // truncated - sm.SetServerID(1) tk.MustExec("kill 101") result = tk.MustQuery("show warnings") result.Check(testkit.Rows("Warning 1105 Kill failed: Received a 32bits truncated ConnectionID, expect 64bits. Please execute 'KILL [CONNECTION | QUERY] ConnectionID' to send a Kill without truncating ConnectionID.")) @@ -64,8 +69,8 @@ func TestKillStmt(t *testing.T) { result.Check(testkit.Rows("Warning 1105 Parse ConnectionID failed: Unexpected connectionID excceeds int64")) // local kill - connID := util.NewGlobalConnID(1, true) - tk.MustExec("kill " + strconv.FormatUint(connID.ID(), 10)) + killConnID := util.NewGlobalConnID(connID, true) + tk.MustExec("kill " + strconv.FormatUint(killConnID.ID(), 10)) result = tk.MustQuery("show warnings") result.Check(testkit.Rows()) diff --git a/executor/sort_test.go b/executor/sort_test.go index 7b82ad15a52c4..99f7015828e96 100644 --- a/executor/sort_test.go +++ b/executor/sort_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/stretchr/testify/require" ) @@ -50,7 +51,7 @@ func testSortInDisk(t *testing.T, removeDir bool) { tk.MustExec("SET GLOBAL tidb_mem_oom_action='LOG'") tk.MustExec("use test") - sm := &testkit.MockSessionManager{ + sm := &testutil.MockSessionManager{ PS: make([]*util.ProcessInfo, 0), } tk.Session().SetSessionManager(sm) diff --git a/infoschema/cluster_tables_test.go b/infoschema/cluster_tables_test.go index b416157e702cb..734b23fbd6470 100644 --- a/infoschema/cluster_tables_test.go +++ b/infoschema/cluster_tables_test.go @@ -43,6 +43,7 @@ import ( "github.com/pingcap/tidb/store/helper" "github.com/pingcap/tidb/store/mockstore/mockstorage" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/pdapi" "github.com/pingcap/tidb/util/resourcegrouptag" @@ -718,8 +719,8 @@ func (s *clusterTablesSuite) setUpRPCService(t *testing.T, addr string) (*grpc.S lis, err := net.Listen("tcp", addr) require.NoError(t, err) // Fix issue 9836 - sm := &mockSessionManager{make(map[uint64]*util.ProcessInfo, 1), nil} - sm.processInfoMap[1] = &util.ProcessInfo{ + sm := &testutil.MockSessionManager{PS: make([]*util.ProcessInfo, 1)} + sm.PS[0] = &util.ProcessInfo{ ID: 1, User: "root", Host: "127.0.0.1", diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index d8b04504844e9..c75c03f1af71b 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -15,7 +15,6 @@ package infoschema_test import ( - "crypto/tls" "fmt" "math" "os" @@ -39,6 +38,7 @@ import ( "github.com/pingcap/tidb/session/txninfo" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/testkit" + "github.com/pingcap/tidb/testkit/testutil" "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/kvcache" "github.com/stretchr/testify/require" @@ -128,14 +128,14 @@ func TestInfoSchemaFieldValue(t *testing.T) { tk1.MustQuery("select distinct(table_schema) from information_schema.tables").Check(testkit.Rows("INFORMATION_SCHEMA")) // Fix issue 9836 - sm := &mockSessionManager{make(map[uint64]*util.ProcessInfo, 1), nil} - sm.processInfoMap[1] = &util.ProcessInfo{ + sm := &testutil.MockSessionManager{PS: make([]*util.ProcessInfo, 0)} + sm.PS = append(sm.PS, &util.ProcessInfo{ ID: 1, User: "root", Host: "127.0.0.1", Command: mysql.ComQuery, StmtCtx: tk.Session().GetSessionVars().StmtCtx, - } + }) tk.Session().SetSessionManager(sm) tk.MustQuery("SELECT user,host,command FROM information_schema.processlist;").Check(testkit.Rows("root 127.0.0.1 Query")) @@ -287,42 +287,6 @@ func TestCurrentTimestampAsDefault(t *testing.T) { tk.MustExec("DROP DATABASE default_time_test") } -type mockSessionManager struct { - processInfoMap map[uint64]*util.ProcessInfo - txnInfo []*txninfo.TxnInfo -} - -func (sm *mockSessionManager) ShowTxnList() []*txninfo.TxnInfo { - return sm.txnInfo -} - -func (sm *mockSessionManager) ShowProcessList() map[uint64]*util.ProcessInfo { - return sm.processInfoMap -} - -func (sm *mockSessionManager) GetProcessInfo(id uint64) (*util.ProcessInfo, bool) { - rs, ok := sm.processInfoMap[id] - return rs, ok -} - -func (sm *mockSessionManager) Kill(_ uint64, _ bool) {} - -func (sm *mockSessionManager) KillAllConnections() {} - -func (sm *mockSessionManager) UpdateTLSConfig(_ *tls.Config) {} - -func (sm *mockSessionManager) ServerID() uint64 { return 1 } - -func (sm *mockSessionManager) StoreInternalSession(se interface{}) { -} - -func (sm *mockSessionManager) DeleteInternalSession(se interface{}) { -} - -func (sm *mockSessionManager) GetInternalSessionStartTSList() []uint64 { - return nil -} - func TestSomeTables(t *testing.T) { store := testkit.CreateMockStore(t) @@ -330,8 +294,8 @@ func TestSomeTables(t *testing.T) { require.NoError(t, err) tk := testkit.NewTestKit(t, store) tk.SetSession(se) - sm := &mockSessionManager{make(map[uint64]*util.ProcessInfo, 2), nil} - sm.processInfoMap[1] = &util.ProcessInfo{ + sm := &testutil.MockSessionManager{PS: make([]*util.ProcessInfo, 0)} + sm.PS = append(sm.PS, &util.ProcessInfo{ ID: 1, User: "user-1", Host: "localhost", @@ -342,8 +306,8 @@ func TestSomeTables(t *testing.T) { State: 1, Info: "do something", StmtCtx: tk.Session().GetSessionVars().StmtCtx, - } - sm.processInfoMap[2] = &util.ProcessInfo{ + }) + sm.PS = append(sm.PS, &util.ProcessInfo{ ID: 2, User: "user-2", Host: "localhost", @@ -354,8 +318,8 @@ func TestSomeTables(t *testing.T) { State: 2, Info: strings.Repeat("x", 101), StmtCtx: tk.Session().GetSessionVars().StmtCtx, - } - sm.processInfoMap[3] = &util.ProcessInfo{ + }) + sm.PS = append(sm.PS, &util.ProcessInfo{ ID: 3, User: "user-3", Host: "127.0.0.1", @@ -366,7 +330,7 @@ func TestSomeTables(t *testing.T) { State: 1, Info: "check port", StmtCtx: tk.Session().GetSessionVars().StmtCtx, - } + }) tk.Session().SetSessionManager(sm) tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Sort().Check( testkit.Rows( @@ -387,8 +351,8 @@ func TestSomeTables(t *testing.T) { fmt.Sprintf("3 user-3 127.0.0.1:12345 test Init DB 9223372036 %s %s", "in transaction", "check port"), )) - sm = &mockSessionManager{make(map[uint64]*util.ProcessInfo, 2), nil} - sm.processInfoMap[1] = &util.ProcessInfo{ + sm = &testutil.MockSessionManager{PS: make([]*util.ProcessInfo, 0)} + sm.PS = append(sm.PS, &util.ProcessInfo{ ID: 1, User: "user-1", Host: "localhost", @@ -396,8 +360,8 @@ func TestSomeTables(t *testing.T) { Command: byte(1), Digest: "abc1", State: 1, - } - sm.processInfoMap[2] = &util.ProcessInfo{ + }) + sm.PS = append(sm.PS, &util.ProcessInfo{ ID: 2, User: "user-2", Host: "localhost", @@ -406,7 +370,7 @@ func TestSomeTables(t *testing.T) { State: 2, Info: strings.Repeat("x", 101), CurTxnStartTS: 410090409861578752, - } + }) tk.Session().SetSessionManager(sm) tk.Session().GetSessionVars().TimeZone = time.UTC tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Check( @@ -1418,8 +1382,8 @@ func TestTiDBTrx(t *testing.T) { // by digest. tk.MustExec("update test_tidb_trx set i = i + 1") _, digest := parser.NormalizeDigest("update test_tidb_trx set i = i + 1") - sm := &mockSessionManager{nil, make([]*txninfo.TxnInfo, 2)} - sm.txnInfo[0] = &txninfo.TxnInfo{ + sm := &testutil.MockSessionManager{TxnInfo: make([]*txninfo.TxnInfo, 2)} + sm.TxnInfo[0] = &txninfo.TxnInfo{ StartTS: 424768545227014155, CurrentSQLDigest: digest.String(), State: txninfo.TxnIdle, @@ -1430,7 +1394,7 @@ func TestTiDBTrx(t *testing.T) { CurrentDB: "test", } blockTime2 := time.Date(2021, 05, 20, 13, 18, 30, 123456000, time.Local) - sm.txnInfo[1] = &txninfo.TxnInfo{ + sm.TxnInfo[1] = &txninfo.TxnInfo{ StartTS: 425070846483628033, CurrentSQLDigest: "", AllSQLDigests: []string{"sql1", "sql2", digest.String()}, @@ -1439,8 +1403,8 @@ func TestTiDBTrx(t *testing.T) { Username: "user1", CurrentDB: "db1", } - sm.txnInfo[1].BlockStartTime.Valid = true - sm.txnInfo[1].BlockStartTime.Time = blockTime2 + sm.TxnInfo[1].BlockStartTime.Valid = true + sm.TxnInfo[1].BlockStartTime.Time = blockTime2 tk.Session().SetSessionManager(sm) tk.MustQuery("select * from information_schema.TIDB_TRX;").Check(testkit.Rows( diff --git a/kv/BUILD.bazel b/kv/BUILD.bazel index e93533551a7de..32dd9f1474179 100644 --- a/kv/BUILD.bazel +++ b/kv/BUILD.bazel @@ -79,6 +79,7 @@ go_test( "//types", "//util/codec", "@com_github_pingcap_errors//:errors", + "@com_github_pingcap_kvproto//pkg/coprocessor", "@com_github_pingcap_kvproto//pkg/deadlock", "@com_github_pingcap_kvproto//pkg/kvrpcpb", "@com_github_stretchr_testify//assert", diff --git a/server/mock_conn.go b/server/mock_conn.go index 4ef086b01d3b7..c6c6579618074 100644 --- a/server/mock_conn.go +++ b/server/mock_conn.go @@ -19,10 +19,12 @@ import ( "bytes" "context" "flag" + "math/rand" "testing" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/parser/auth" tmysql "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/util/arena" "github.com/pingcap/tidb/util/chunk" @@ -39,6 +41,8 @@ type MockConn interface { Dispatch(ctx context.Context, data []byte) error // Close releases resources Close() + // ID returns the connection ID. + ID() uint64 } type mockConn struct { @@ -66,6 +70,11 @@ func (mc *mockConn) Close() { require.NoError(mc.t, mc.clientConn.Close()) } +// ID implements MockConn.ID +func (mc *mockConn) ID() uint64 { + return mc.clientConn.connectionID +} + // CreateMockServer creates a mock server. func CreateMockServer(t *testing.T, store kv.Storage) *Server { if !RunInGoTest { @@ -85,20 +94,28 @@ func CreateMockServer(t *testing.T, store kv.Storage) *Server { // CreateMockConn creates a mock connection together with a session. func CreateMockConn(t *testing.T, server *Server) MockConn { - tc, err := server.driver.OpenCtx(uint64(0), 0, uint8(tmysql.DefaultCollationID), "", nil) + connID := rand.Uint64() + tc, err := server.driver.OpenCtx(connID, 0, uint8(tmysql.DefaultCollationID), "", nil) require.NoError(t, err) cc := &clientConn{ - server: server, - salt: []byte{}, - collation: tmysql.DefaultCollationID, - alloc: arena.NewAllocator(1024), - chunkAlloc: chunk.NewAllocator(), + connectionID: connID, + server: server, + salt: []byte{}, + collation: tmysql.DefaultCollationID, + alloc: arena.NewAllocator(1024), + chunkAlloc: chunk.NewAllocator(), pkt: &packetIO{ bufWriter: bufio.NewWriter(bytes.NewBuffer(nil)), }, } cc.setCtx(tc) + cc.server.rwlock.Lock() + server.clients[cc.connectionID] = cc + cc.server.rwlock.Unlock() + tc.Session.SetSessionManager(server) + err = tc.Session.Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost"}, nil, nil) + require.NoError(t, err) return &mockConn{ clientConn: cc, t: t, diff --git a/sessionctx/sessionstates/session_states_test.go b/sessionctx/sessionstates/session_states_test.go index e8b9e85b4bb06..0a6405bdfdce6 100644 --- a/sessionctx/sessionstates/session_states_test.go +++ b/sessionctx/sessionstates/session_states_test.go @@ -979,6 +979,7 @@ func TestPreparedStatements(t *testing.T) { for _, tt := range tests { conn1 := server.CreateMockConn(t, sv) tk1 := testkit.NewTestKitWithSession(t, store, conn1.Context().Session) + conn1.Context().Session.GetSessionVars().User = nil var param any if tt.setFunc != nil { param = tt.setFunc(tk1, conn1) @@ -1362,6 +1363,7 @@ func TestShowStateFail(t *testing.T) { }) for _, tt := range tests { conn1 := server.CreateMockConn(t, sv) + conn1.Context().Session.GetSessionVars().User = nil tk1 := testkit.NewTestKitWithSession(t, store, conn1.Context().Session) tt.setFunc(tk1, conn1) if tt.showErr == 0 { diff --git a/testkit/BUILD.bazel b/testkit/BUILD.bazel index 509a67db80875..4588965ecff5a 100644 --- a/testkit/BUILD.bazel +++ b/testkit/BUILD.bazel @@ -5,7 +5,6 @@ go_library( srcs = [ "asynctestkit.go", "dbtestkit.go", - "mocksessionmanager.go", "mockstore.go", "result.go", "stepped.go", @@ -21,11 +20,9 @@ go_library( "//parser/ast", "//parser/terror", "//session", - "//session/txninfo", "//sessionctx/variable", "//store/driver", "//store/mockstore", - "//util", "//util/breakpoint", "//util/sqlexec", "@com_github_pingcap_errors//:errors", diff --git a/testkit/testutil/BUILD.bazel b/testkit/testutil/BUILD.bazel index dd662dc0aa80f..a8673852403bf 100644 --- a/testkit/testutil/BUILD.bazel +++ b/testkit/testutil/BUILD.bazel @@ -4,6 +4,7 @@ go_library( name = "testutil", srcs = [ "handle.go", + "mocksessionmanager.go", "require.go", ], importpath = "github.com/pingcap/tidb/testkit/testutil", @@ -11,8 +12,10 @@ go_library( deps = [ "//kv", "//parser/mysql", + "//session/txninfo", "//sessionctx/stmtctx", "//types", + "//util", "//util/codec", "//util/collate", "@com_github_stretchr_testify//require", diff --git a/testkit/mocksessionmanager.go b/testkit/testutil/mocksessionmanager.go similarity index 80% rename from testkit/mocksessionmanager.go rename to testkit/testutil/mocksessionmanager.go index 5e775c02087e4..670b6af2adf4e 100644 --- a/testkit/mocksessionmanager.go +++ b/testkit/testutil/mocksessionmanager.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package testkit +package testutil import ( "crypto/tls" @@ -23,12 +23,14 @@ import ( // MockSessionManager is a mocked session manager which is used for test. type MockSessionManager struct { - PS []*util.ProcessInfo + PS []*util.ProcessInfo + SerID uint64 + TxnInfo []*txninfo.TxnInfo } // ShowTxnList is to show txn list. func (msm *MockSessionManager) ShowTxnList() []*txninfo.TxnInfo { - return nil + return msm.TxnInfo } // ShowProcessList implements the SessionManager.ShowProcessList interface. @@ -51,29 +53,29 @@ func (msm *MockSessionManager) GetProcessInfo(id uint64) (*util.ProcessInfo, boo } // Kill implements the SessionManager.Kill interface. -func (msm *MockSessionManager) Kill(cid uint64, query bool) { +func (*MockSessionManager) Kill(uint64, bool) { } // KillAllConnections implements the SessionManager.KillAllConections interface. -func (msm *MockSessionManager) KillAllConnections() { +func (*MockSessionManager) KillAllConnections() { } // UpdateTLSConfig implements the SessionManager.UpdateTLSConfig interface. -func (msm *MockSessionManager) UpdateTLSConfig(cfg *tls.Config) { +func (*MockSessionManager) UpdateTLSConfig(*tls.Config) { } // ServerID get server id. func (msm *MockSessionManager) ServerID() uint64 { - return 1 + return msm.SerID } // StoreInternalSession is to store internal session. -func (msm *MockSessionManager) StoreInternalSession(se interface{}) {} +func (*MockSessionManager) StoreInternalSession(interface{}) {} // DeleteInternalSession is to delete the internal session pointer from the map in the SessionManager -func (msm *MockSessionManager) DeleteInternalSession(se interface{}) {} +func (*MockSessionManager) DeleteInternalSession(interface{}) {} // GetInternalSessionStartTSList is to get all startTS of every transactions running in the current internal sessions -func (msm *MockSessionManager) GetInternalSessionStartTSList() []uint64 { +func (*MockSessionManager) GetInternalSessionStartTSList() []uint64 { return nil } diff --git a/util/processinfo.go b/util/processinfo.go index d80c7a302c401..57a1a0e92abc1 100644 --- a/util/processinfo.go +++ b/util/processinfo.go @@ -174,11 +174,11 @@ type SessionManager interface { KillAllConnections() UpdateTLSConfig(cfg *tls.Config) ServerID() uint64 - // Put the internal session pointer to the map in the SessionManager + // StoreInternalSession puts the internal session pointer to the map in the SessionManager. StoreInternalSession(se interface{}) - // Delete the internal session pointer from the map in the SessionManager + // DeleteInternalSession deletes the internal session pointer from the map in the SessionManager. DeleteInternalSession(se interface{}) - // Get all startTS of every transactions running in the current internal sessions + // GetInternalSessionStartTSList gets all startTS of every transactions running in the current internal sessions. GetInternalSessionStartTSList() []uint64 } diff --git a/util/ranger/BUILD.bazel b/util/ranger/BUILD.bazel index 622db8075ce87..f09aa8f7e7135 100644 --- a/util/ranger/BUILD.bazel +++ b/util/ranger/BUILD.bazel @@ -38,6 +38,7 @@ go_test( name = "ranger_test", timeout = "short", srcs = [ + "bench_test.go", "main_test.go", "ranger_test.go", "types_test.go",