From 6849e5d56d241929bcaaafb0675e8e18d70ca86b Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 24 Aug 2021 16:02:04 +0800 Subject: [PATCH] planner/core: migrate leak tests to goleak (#27433) --- planner/core/main_test.go | 31 ++++++++++++++++++++++++ planner/core/preprocess_test.go | 24 ++++++------------ planner/core/rule_result_reorder_test.go | 7 ++++++ 3 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 planner/core/main_test.go diff --git a/planner/core/main_test.go b/planner/core/main_test.go new file mode 100644 index 0000000000000..dbe401ff7739e --- /dev/null +++ b/planner/core/main_test.go @@ -0,0 +1,31 @@ +// Copyright 2021 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package core + +import ( + "testing" + + "github.com/pingcap/tidb/util/testbridge" + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + testbridge.WorkaroundGoCheckFlags() + opts := []goleak.Option{ + goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"), + goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), + } + goleak.VerifyTestMain(m, opts...) +} diff --git a/planner/core/preprocess_test.go b/planner/core/preprocess_test.go index f5057d88958b3..6b331556b22d7 100644 --- a/planner/core/preprocess_test.go +++ b/planner/core/preprocess_test.go @@ -59,6 +59,13 @@ func (s *testValidatorSuite) SetUpTest(c *C) { s.is = infoschema.MockInfoSchema([]*model.TableInfo{core.MockSignedTable()}) } +func (s *testValidatorSuite) TearDownTest(c *C) { + s.dom.Close() + err := s.store.Close() + c.Assert(err, IsNil) + testleak.AfterTest(c)() +} + func (s *testValidatorSuite) runSQL(c *C, sql string, inPrepare bool, terr error) { stmts, err1 := session.Parse(s.ctx, sql) c.Assert(err1, IsNil, Commentf("sql: %s", sql)) @@ -73,11 +80,6 @@ func (s *testValidatorSuite) runSQL(c *C, sql string, inPrepare bool, terr error } func (s *testValidatorSuite) TestValidator(c *C) { - defer testleak.AfterTest(c)() - defer func() { - s.dom.Close() - s.store.Close() - }() tests := []struct { sql string inPrepare bool @@ -315,12 +317,6 @@ func (s *testValidatorSuite) TestValidator(c *C) { } func (s *testValidatorSuite) TestForeignKey(c *C) { - defer testleak.AfterTest(c)() - defer func() { - s.dom.Close() - s.store.Close() - }() - _, err := s.se.Execute(context.Background(), "create table test.t1(a int, b int, c int)") c.Assert(err, IsNil) @@ -346,12 +342,6 @@ func (s *testValidatorSuite) TestForeignKey(c *C) { } func (s *testValidatorSuite) TestDropGlobalTempTable(c *C) { - defer testleak.AfterTest(c)() - defer func() { - s.dom.Close() - s.store.Close() - }() - ctx := context.Background() execSQLList := []string{ "use test", diff --git a/planner/core/rule_result_reorder_test.go b/planner/core/rule_result_reorder_test.go index 452857a1c966a..8931afe90b541 100644 --- a/planner/core/rule_result_reorder_test.go +++ b/planner/core/rule_result_reorder_test.go @@ -44,6 +44,11 @@ func (s *testRuleReorderResultsSerial) SetUpTest(c *C) { c.Assert(err, IsNil) } +func (s *testRuleReorderResultsSerial) TearDownTest(c *C) { + s.dom.Close() + c.Assert(s.store.Close(), IsNil) +} + func (s *testRuleReorderResultsSerial) TestPlanCache(c *C) { tk := testkit.NewTestKit(c, s.store) orgEnable := plannercore.PreparedPlanCacheEnabled() @@ -124,6 +129,8 @@ func (s *testRuleReorderResults) SetUpSuite(c *C) { } func (s *testRuleReorderResults) TearDownSuite(c *C) { + s.dom.Close() + c.Assert(s.store.Close(), IsNil) c.Assert(s.testData.GenerateOutputIfNeeded(), IsNil) }