Skip to content

Commit 78ea5b3

Browse files
committed
pkg/server: isolate initstats tests into dedicated package
1 parent 2278c17 commit 78ea5b3

5 files changed

Lines changed: 191 additions & 72 deletions

File tree

pkg/server/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ go_test(
214214
"@com_github_prometheus_client_golang//prometheus/testutil",
215215
"@com_github_stretchr_testify//require",
216216
"@com_github_tikv_client_go_v2//error",
217-
"@com_github_tikv_client_go_v2//oracle",
218217
"@com_github_tikv_client_go_v2//testutils",
219218
"@com_github_tikv_client_go_v2//tikv",
220219
"@org_uber_go_goleak//:goleak",

pkg/server/stat_test.go

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ import (
2020
"time"
2121

2222
"github.com/pingcap/failpoint"
23-
"github.com/pingcap/tidb/pkg/config"
2423
"github.com/pingcap/tidb/pkg/domain/infosync"
2524
"github.com/pingcap/tidb/pkg/keyspace"
2625
"github.com/pingcap/tidb/pkg/server/internal/util"
2726
"github.com/pingcap/tidb/pkg/session"
2827
"github.com/pingcap/tidb/pkg/store/mockstore"
2928
"github.com/stretchr/testify/require"
30-
"github.com/tikv/client-go/v2/oracle"
3129
)
3230

3331
func TestUptime(t *testing.T) {
@@ -65,72 +63,3 @@ func TestUptime(t *testing.T) {
6563
require.NoError(t, err)
6664
require.GreaterOrEqual(t, stats[upTime].(int64), int64(time.Since(time.Unix(1282967700, 0)).Seconds()))
6765
}
68-
69-
func TestInitStatsSessionBlockGC(t *testing.T) {
70-
origConfig := config.GetGlobalConfig()
71-
defer func() {
72-
config.StoreGlobalConfig(origConfig)
73-
}()
74-
newConfig := *origConfig
75-
for _, lite := range []bool{false, true} {
76-
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStats", "pause"))
77-
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStatsLite", "pause"))
78-
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest", "return(true)"))
79-
newConfig.Performance.LiteInitStats = lite
80-
config.StoreGlobalConfig(&newConfig)
81-
82-
store, err := mockstore.NewMockStore()
83-
require.NoError(t, err)
84-
dom, err := session.BootstrapSession(store)
85-
require.NoError(t, err)
86-
87-
infoSyncer := dom.InfoSyncer()
88-
sv := CreateMockServer(t, store)
89-
sv.SetDomain(dom)
90-
infoSyncer.SetSessionManager(sv)
91-
time.Sleep(time.Second)
92-
require.Eventually(t, func() bool {
93-
now := time.Now()
94-
startTSList := sv.GetInternalSessionStartTSList()
95-
for _, startTs := range startTSList {
96-
if startTs != 0 {
97-
startTime := oracle.GetTimeFromTS(startTs)
98-
// test pass if the min_start_ts is blocked over 1s.
99-
if now.Sub(startTime) > time.Second {
100-
return true
101-
}
102-
}
103-
}
104-
return false
105-
}, 10*time.Second, 10*time.Millisecond, "min_start_ts is not blocked over 1s")
106-
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStats"))
107-
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStatsLite"))
108-
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest"))
109-
dom.Close()
110-
require.NoError(t, store.Close())
111-
}
112-
}
113-
114-
func TestInitStatsSessionBlockGCCanBeCanceled(t *testing.T) {
115-
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest", "return(true)"))
116-
117-
store, err := mockstore.NewMockStore()
118-
require.NoError(t, err)
119-
dom, err := session.BootstrapSession(store)
120-
require.NoError(t, err)
121-
122-
infoSyncer := dom.InfoSyncer()
123-
// This prevents the session from being created because we do not set a session manager.
124-
infoSyncer.SetSessionManager(nil)
125-
h := dom.StatsHandle()
126-
ctx, cancel := context.WithCancel(context.Background())
127-
go func() {
128-
time.Sleep(1 * time.Second)
129-
cancel()
130-
}()
131-
require.ErrorIs(t, h.InitStats(ctx, dom.InfoSchema()), context.Canceled)
132-
require.ErrorIs(t, h.InitStatsLite(ctx), context.Canceled)
133-
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest"))
134-
dom.Close()
135-
require.NoError(t, store.Close())
136-
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_test")
2+
3+
go_test(
4+
name = "initstats_test",
5+
timeout = "short",
6+
srcs = [
7+
"initstats_test.go",
8+
"main_test.go",
9+
],
10+
flaky = True,
11+
shard_count = 2,
12+
deps = [
13+
"//pkg/config",
14+
"//pkg/metrics",
15+
"//pkg/server",
16+
"//pkg/session",
17+
"//pkg/store/mockstore",
18+
"//pkg/store/mockstore/unistore",
19+
"//pkg/testkit/testsetup",
20+
"//pkg/util/topsql/state",
21+
"@com_github_pingcap_failpoint//:failpoint",
22+
"@com_github_stretchr_testify//require",
23+
"@com_github_tikv_client_go_v2//oracle",
24+
"@com_github_tikv_client_go_v2//tikv",
25+
"@org_uber_go_goleak//:goleak",
26+
],
27+
)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2026 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package initstats
16+
17+
import (
18+
"context"
19+
"testing"
20+
"time"
21+
22+
"github.com/pingcap/failpoint"
23+
"github.com/pingcap/tidb/pkg/config"
24+
server2 "github.com/pingcap/tidb/pkg/server"
25+
"github.com/pingcap/tidb/pkg/session"
26+
"github.com/pingcap/tidb/pkg/store/mockstore"
27+
"github.com/stretchr/testify/require"
28+
"github.com/tikv/client-go/v2/oracle"
29+
)
30+
31+
func TestInitStatsSessionBlockGC(t *testing.T) {
32+
origConfig := config.GetGlobalConfig()
33+
defer func() {
34+
config.StoreGlobalConfig(origConfig)
35+
}()
36+
newConfig := *origConfig
37+
for _, lite := range []bool{false, true} {
38+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStats", "pause"))
39+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStatsLite", "pause"))
40+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest", "return(true)"))
41+
newConfig.Performance.LiteInitStats = lite
42+
config.StoreGlobalConfig(&newConfig)
43+
44+
store, err := mockstore.NewMockStore()
45+
require.NoError(t, err)
46+
dom, err := session.BootstrapSession(store)
47+
require.NoError(t, err)
48+
49+
infoSyncer := dom.InfoSyncer()
50+
sv := server2.CreateMockServer(t, store)
51+
sv.SetDomain(dom)
52+
infoSyncer.SetSessionManager(sv)
53+
time.Sleep(time.Second)
54+
require.Eventually(t, func() bool {
55+
now := time.Now()
56+
startTSList := sv.GetInternalSessionStartTSList()
57+
for _, startTS := range startTSList {
58+
if startTS != 0 {
59+
startTime := oracle.GetTimeFromTS(startTS)
60+
if now.Sub(startTime) > time.Second {
61+
return true
62+
}
63+
}
64+
}
65+
return false
66+
}, 10*time.Second, 10*time.Millisecond, "min_start_ts is not blocked over 1s")
67+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStats"))
68+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/beforeInitStatsLite"))
69+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest"))
70+
dom.Close()
71+
require.NoError(t, store.Close())
72+
}
73+
}
74+
75+
func TestInitStatsSessionBlockGCCanBeCanceled(t *testing.T) {
76+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest", "return(true)"))
77+
78+
store, err := mockstore.NewMockStore()
79+
require.NoError(t, err)
80+
dom, err := session.BootstrapSession(store)
81+
require.NoError(t, err)
82+
83+
infoSyncer := dom.InfoSyncer()
84+
infoSyncer.SetSessionManager(nil)
85+
h := dom.StatsHandle()
86+
ctx, cancel := context.WithCancel(context.Background())
87+
go func() {
88+
time.Sleep(time.Second)
89+
cancel()
90+
}()
91+
require.ErrorIs(t, h.InitStats(ctx, dom.InfoSchema()), context.Canceled)
92+
require.ErrorIs(t, h.InitStatsLite(ctx), context.Canceled)
93+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/session/syssession/ForceBlockGCInTest"))
94+
dom.Close()
95+
require.NoError(t, store.Close())
96+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2026 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package initstats
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"reflect"
21+
"testing"
22+
23+
"github.com/pingcap/tidb/pkg/config"
24+
"github.com/pingcap/tidb/pkg/metrics"
25+
"github.com/pingcap/tidb/pkg/server"
26+
"github.com/pingcap/tidb/pkg/store/mockstore/unistore"
27+
"github.com/pingcap/tidb/pkg/testkit/testsetup"
28+
topsqlstate "github.com/pingcap/tidb/pkg/util/topsql/state"
29+
"github.com/tikv/client-go/v2/tikv"
30+
"go.uber.org/goleak"
31+
)
32+
33+
func TestMain(m *testing.M) {
34+
server.RunInGoTest = true
35+
server.RunInGoTestChan = make(chan struct{})
36+
testsetup.SetupForCommonTest()
37+
topsqlstate.EnableTopSQL()
38+
unistore.CheckResourceTagForTopSQLInGoTest = true
39+
40+
tikv.EnableFailpoints()
41+
42+
metrics.RegisterMetrics()
43+
44+
defaultConfig := config.NewConfig()
45+
globalConfig := config.GetGlobalConfig()
46+
if !reflect.DeepEqual(defaultConfig, globalConfig) {
47+
_, _ = fmt.Fprintf(os.Stderr, "server: the global config has been changed.\n")
48+
_, _ = fmt.Fprintf(os.Stderr, "default: %#v\nglobal: %#v", defaultConfig, globalConfig)
49+
}
50+
51+
opts := []goleak.Option{
52+
goleak.IgnoreTopFunction("github.com/golang/glog.(*fileSink).flushDaemon"),
53+
goleak.IgnoreTopFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"),
54+
goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"),
55+
goleak.IgnoreTopFunction("time.Sleep"),
56+
goleak.IgnoreTopFunction("database/sql.(*Tx).awaitDone"),
57+
goleak.IgnoreTopFunction("internal/poll.runtime_pollWait"),
58+
goleak.IgnoreTopFunction("net/http.(*persistConn).readLoop"),
59+
goleak.IgnoreTopFunction("net/http.(*persistConn).writeLoop"),
60+
goleak.IgnoreTopFunction("github.com/pingcap/tidb/pkg/server.NewServer.func1"),
61+
goleak.IgnoreTopFunction("gopkg.in/natefinch/lumberjack%2ev2.(*Logger).millRun"),
62+
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
63+
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"),
64+
goleak.IgnoreTopFunction("github.com/go-sql-driver/mysql.(*mysqlConn).startWatcher.func1"),
65+
}
66+
67+
goleak.VerifyTestMain(m, opts...)
68+
}

0 commit comments

Comments
 (0)