From cf8a941d109ac3b8a0c7f2be422dcedc9d0032aa Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Wed, 19 Apr 2023 16:49:35 +0800 Subject: [PATCH] testenv: unite all GOMAXPROCS at test (#43175) --- server/BUILD.bazel | 2 +- server/server_test.go | 5 ++--- session/BUILD.bazel | 1 + session/testutil.go | 5 ++--- store/mockstore/BUILD.bazel | 1 + store/mockstore/mockstore.go | 2 ++ testkit/BUILD.bazel | 2 +- testkit/testenv/BUILD.bazel | 9 +++++++++ testkit/testenv/testenv.go | 26 ++++++++++++++++++++++++++ testkit/testkit.go | 5 ++--- 10 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 testkit/testenv/BUILD.bazel create mode 100644 testkit/testenv/testenv.go diff --git a/server/BUILD.bazel b/server/BUILD.bazel index f70c624b82..63d84ca98b 100644 --- a/server/BUILD.bazel +++ b/server/BUILD.bazel @@ -193,6 +193,7 @@ go_test( "//testkit", "//testkit/external", "//testkit/testdata", + "//testkit/testenv", "//testkit/testmain", "//testkit/testsetup", "//types", @@ -203,7 +204,6 @@ go_test( "//util/cpuprofile", "//util/dbterror/exeerrors", "//util/deadlockhistory", - "//util/mathutil", "//util/mock", "//util/plancodec", "//util/resourcegrouptag", diff --git a/server/server_test.go b/server/server_test.go index a4e6b34b5d..46efb59500 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -26,7 +26,6 @@ import ( "os" "path/filepath" "regexp" - "runtime" "strconv" "strings" "testing" @@ -40,7 +39,7 @@ import ( "github.com/pingcap/tidb/kv" tmysql "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/testkit" - "github.com/pingcap/tidb/util/mathutil" + "github.com/pingcap/tidb/testkit/testenv" "github.com/pingcap/tidb/util/versioninfo" "github.com/stretchr/testify/require" "go.uber.org/zap" @@ -62,7 +61,7 @@ type testServerClient struct { // newTestServerClient return a testServerClient with unique address func newTestServerClient() *testServerClient { - runtime.GOMAXPROCS(mathutil.Min(8, runtime.GOMAXPROCS(0))) + testenv.SetGOMAXPROCSForTest() return &testServerClient{ port: 0, statusPort: 0, diff --git a/session/BUILD.bazel b/session/BUILD.bazel index 08a831a78a..2fb534927e 100644 --- a/session/BUILD.bazel +++ b/session/BUILD.bazel @@ -67,6 +67,7 @@ go_library( "//table/temptable", "//tablecodec", "//telemetry", + "//testkit/testenv", "//ttl/ttlworker", "//types", "//types/parser_driver", diff --git a/session/testutil.go b/session/testutil.go index d9a908474a..592ce9b45b 100644 --- a/session/testutil.go +++ b/session/testutil.go @@ -16,14 +16,13 @@ package session import ( "context" - "runtime" "testing" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/mockstore" - "github.com/pingcap/tidb/util/mathutil" + "github.com/pingcap/tidb/testkit/testenv" "github.com/pingcap/tidb/util/sqlexec" "github.com/stretchr/testify/require" atomicutil "go.uber.org/atomic" @@ -40,7 +39,7 @@ var ( // CreateStoreAndBootstrap creates a mock store and bootstrap it. func CreateStoreAndBootstrap(t *testing.T) (kv.Storage, *domain.Domain) { - runtime.GOMAXPROCS(mathutil.Min(8, runtime.GOMAXPROCS(0))) + testenv.SetGOMAXPROCSForTest() store, err := mockstore.NewMockStore() require.NoError(t, err) dom, err := BootstrapSession(store) diff --git a/store/mockstore/BUILD.bazel b/store/mockstore/BUILD.bazel index d5d42a411a..f01bf10a18 100644 --- a/store/mockstore/BUILD.bazel +++ b/store/mockstore/BUILD.bazel @@ -16,6 +16,7 @@ go_library( "//store/mockstore/mockcopr", "//store/mockstore/mockstorage", "//store/mockstore/unistore", + "//testkit/testenv", "@com_github_pingcap_errors//:errors", "@com_github_tikv_client_go_v2//testutils", "@com_github_tikv_client_go_v2//tikv", diff --git a/store/mockstore/mockstore.go b/store/mockstore/mockstore.go index 6cf3eb15d3..d498ea1956 100644 --- a/store/mockstore/mockstore.go +++ b/store/mockstore/mockstore.go @@ -22,6 +22,7 @@ import ( "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/store/mockstore/unistore" + "github.com/pingcap/tidb/testkit/testenv" "github.com/tikv/client-go/v2/testutils" "github.com/tikv/client-go/v2/tikv" pd "github.com/tikv/pd/client" @@ -163,6 +164,7 @@ var DDLCheckerInjector func(kv.Storage) kv.Storage // NewMockStore creates a mocked tikv store, the path is the file path to store the data. // If path is an empty string, a memory storage will be created. func NewMockStore(options ...MockTiKVStoreOption) (kv.Storage, error) { + testenv.SetGOMAXPROCSForTest() opt := mockOptions{ clusterInspector: func(c testutils.Cluster) { BootstrapWithSingleStore(c) diff --git a/testkit/BUILD.bazel b/testkit/BUILD.bazel index 16e766350c..fb812420e1 100644 --- a/testkit/BUILD.bazel +++ b/testkit/BUILD.bazel @@ -28,12 +28,12 @@ go_library( "//sessionctx/variable", "//store/driver", "//store/mockstore", + "//testkit/testenv", "//util", "//util/breakpoint", "//util/chunk", "//util/gctuner", "//util/intest", - "//util/mathutil", "//util/sqlexec", "@com_github_pingcap_errors//:errors", "@com_github_pingcap_failpoint//:failpoint", diff --git a/testkit/testenv/BUILD.bazel b/testkit/testenv/BUILD.bazel new file mode 100644 index 0000000000..5f96a6c9dc --- /dev/null +++ b/testkit/testenv/BUILD.bazel @@ -0,0 +1,9 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "testenv", + srcs = ["testenv.go"], + importpath = "github.com/pingcap/tidb/testkit/testenv", + visibility = ["//visibility:public"], + deps = ["//util/mathutil"], +) diff --git a/testkit/testenv/testenv.go b/testkit/testenv/testenv.go new file mode 100644 index 0000000000..cf86c4ff47 --- /dev/null +++ b/testkit/testenv/testenv.go @@ -0,0 +1,26 @@ +// Copyright 2023 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 testenv + +import ( + "runtime" + + "github.com/pingcap/tidb/util/mathutil" +) + +// SetGOMAXPROCSForTest sets GOMAXPROCS to 8 if it is greater than 8. +func SetGOMAXPROCSForTest() { + runtime.GOMAXPROCS(mathutil.Min(8, runtime.GOMAXPROCS(0))) +} diff --git a/testkit/testkit.go b/testkit/testkit.go index ef0ffd11ac..81778a1cef 100644 --- a/testkit/testkit.go +++ b/testkit/testkit.go @@ -19,7 +19,6 @@ package testkit import ( "context" "fmt" - "runtime" "strings" "sync" "testing" @@ -32,9 +31,9 @@ import ( "github.com/pingcap/tidb/parser/terror" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx/variable" + "github.com/pingcap/tidb/testkit/testenv" "github.com/pingcap/tidb/util/chunk" "github.com/pingcap/tidb/util/intest" - "github.com/pingcap/tidb/util/mathutil" "github.com/pingcap/tidb/util/sqlexec" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -58,7 +57,7 @@ type TestKit struct { // NewTestKit returns a new *TestKit. func NewTestKit(t testing.TB, store kv.Storage) *TestKit { require.True(t, intest.InTest, "you should add --tags=intest when to test, see https://pingcap.github.io/tidb-dev-guide/get-started/setup-an-ide.html for help") - runtime.GOMAXPROCS(mathutil.Min(16, runtime.GOMAXPROCS(0))) + testenv.SetGOMAXPROCSForTest() tk := &TestKit{ require: require.New(t), assert: assert.New(t),