Skip to content

Commit

Permalink
session: migrate test-infra to testify for tidbAsLibrarySuite (pingca…
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun authored Apr 27, 2022
1 parent 1a19f95 commit a0f55d3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
9 changes: 4 additions & 5 deletions session/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ func TestMain(m *testing.M) {
goleak.VerifyTestMain(testmain.WrapTestingM(m, callback), opts...)
}

func GetClusteredIndexSuiteData() testdata.TestData {
return testDataMap["clustered_index_suite"]
}

// TODO: remove once `session` tests migrated to testify
func TestT(t *testing.T) {
check.TestingT(t)
}

func GetClusteredIndexSuiteData() testdata.TestData {
return testDataMap["clustered_index_suite"]
}

func createStoreAndBootstrap(t *testing.T) (kv.Storage, *domain.Domain) {
store, err := mockstore.NewMockStore()
require.NoError(t, err)
Expand Down
32 changes: 0 additions & 32 deletions session/session_test.go → session/session_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net"
"os"
"path"
"runtime"
"sort"
"strconv"
"strings"
Expand All @@ -30,7 +29,6 @@ import (
"testing"
"time"

"github.com/docker/go-units"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
Expand Down Expand Up @@ -93,7 +91,6 @@ var _ = SerialSuites(&testSessionSerialSuite{})
var _ = SerialSuites(&testBackupRestoreSuite{})
var _ = SerialSuites(&testTxnStateSerialSuite{})
var _ = SerialSuites(&testStatisticsSuite{})
var _ = SerialSuites(&testTiDBAsLibrary{})

type testSessionSuiteBase struct {
cluster testutils.Cluster
Expand Down Expand Up @@ -128,8 +125,6 @@ type testStatisticsSuite struct {
testSessionSuiteBase
}

type testTiDBAsLibrary struct{}

func clearStorage(store kv.Storage) error {
txn, err := store.Begin()
if err != nil {
Expand Down Expand Up @@ -5893,33 +5888,6 @@ func (s *testSessionSuite) TestTemporaryTableInterceptor(c *C) {
c.Assert(val, BytesEquals, []byte("v1"))
}

func (s *testTiDBAsLibrary) TestMemoryLeak(c *C) {
initAndCloseTiDB := func() {
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
c.Assert(err, IsNil)
defer store.Close()

dom, err := session.BootstrapSession(store)
//nolint:staticcheck
defer dom.Close()
c.Assert(err, IsNil)
}

runtime.GC()
memStat := runtime.MemStats{}
runtime.ReadMemStats(&memStat)
oldHeapInUse := memStat.HeapInuse

for i := 0; i < 20; i++ {
initAndCloseTiDB()
}

runtime.GC()
runtime.ReadMemStats(&memStat)
// before the fix, initAndCloseTiDB for 20 times will cost 900 MB memory, so we test for a quite loose upper bound.
c.Assert(memStat.HeapInuse-oldHeapInUse, Less, uint64(300*units.MiB))
}

func (s *testSessionSuite) TestTiDBReadStaleness(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_read_staleness='-5'")
Expand Down
53 changes: 53 additions & 0 deletions session/tidb_library_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 session_test

import (
"runtime"
"testing"

"github.com/docker/go-units"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/stretchr/testify/require"
)

func TestMemoryLeak(t *testing.T) {
t.Skip("hack")

initAndCloseTiDB := func() {
store, err := mockstore.NewMockStore(mockstore.WithStoreType(mockstore.EmbedUnistore))
require.NoError(t, err)
defer func() { require.NoError(t, store.Close()) }()

dom, err := session.BootstrapSession(store)
require.NoError(t, err)
dom.Close()
}

runtime.GC()
memStat := runtime.MemStats{}
runtime.ReadMemStats(&memStat)
oldHeapInUse := memStat.HeapInuse

for i := 0; i < 20; i++ {
initAndCloseTiDB()
}

runtime.GC()
runtime.ReadMemStats(&memStat)
// before the fix, initAndCloseTiDB for 20 times will cost 900 MB memory, so we test for a quite loose upper bound.
require.Less(t, memStat.HeapInuse-oldHeapInUse, uint64(300*units.MiB))
}

0 comments on commit a0f55d3

Please sign in to comment.