Skip to content

Commit

Permalink
add leak check to the main package
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Aug 8, 2024
1 parent 7741924 commit 88504dd
Show file tree
Hide file tree
Showing 32 changed files with 183 additions and 20 deletions.
6 changes: 6 additions & 0 deletions client/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ import (
"github.com/stretchr/testify/require"
"github.com/tikv/pd/client/errs"
"github.com/tikv/pd/client/retry"
"github.com/tikv/pd/client/testutil"
"go.uber.org/atomic"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func TestPDAllowFollowerHandleHeader(t *testing.T) {
re := require.New(t)
checked := 0
Expand Down
6 changes: 6 additions & 0 deletions client/resource_group/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ import (
"github.com/stretchr/testify/require"
pd "github.com/tikv/pd/client"
"github.com/tikv/pd/client/errs"
"github.com/tikv/pd/client/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func createTestGroupCostController(re *require.Assertions) *groupCostController {
group := &rmpb.ResourceGroup{
Name: "test",
Expand Down
6 changes: 6 additions & 0 deletions pkg/balancer/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func TestBalancerPutAndDelete(t *testing.T) {
re := require.New(t)
balancers := []Balancer[uint32]{
Expand Down
6 changes: 6 additions & 0 deletions pkg/core/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ import (

"github.com/gogo/protobuf/proto"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func BenchmarkDeepClone(b *testing.B) {
for i := 0; i < b.N; i++ {
src := &metapb.Region{Id: 1}
Expand Down
6 changes: 6 additions & 0 deletions pkg/core/rangetree/range_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

type simpleBucketItem struct {
startKey []byte
endKey []byte
Expand Down
6 changes: 6 additions & 0 deletions pkg/core/storelimit/limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ import (

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/core/constant"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func TestStoreLimit(t *testing.T) {
re := require.New(t)
rate := int64(15)
Expand Down
6 changes: 6 additions & 0 deletions pkg/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import (

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/etcdutil"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

const (
rootPath = "/pd"
leaderPath = "/pd/leader"
Expand Down
6 changes: 6 additions & 0 deletions pkg/keyspace/keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ import (
"github.com/tikv/pd/pkg/storage/endpoint"
"github.com/tikv/pd/pkg/storage/kv"
"github.com/tikv/pd/pkg/utils/etcdutil"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

const (
testConfig = "test config"
testConfig1 = "config_entry_1"
Expand Down
6 changes: 6 additions & 0 deletions pkg/ratelimit/concurrency_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ import (
"time"

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func TestConcurrencyLimiter(t *testing.T) {
re := require.New(t)
cl := NewConcurrencyLimiter(10)
Expand Down
6 changes: 6 additions & 0 deletions pkg/schedule/config/store_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func TestMergeCheck(t *testing.T) {
re := require.New(t)
testdata := []struct {
Expand Down
6 changes: 6 additions & 0 deletions pkg/schedule/filter/candidates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import (
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/schedule/config"
"github.com/tikv/pd/pkg/schedule/plan"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

// A dummy comparer for testing.
func idComparer(a, b *core.StoreInfo) int {
if a.GetID() > b.GetID() {
Expand Down
38 changes: 29 additions & 9 deletions pkg/schedule/labeler/labeler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ import (
"github.com/tikv/pd/pkg/storage/kv"
"github.com/tikv/pd/pkg/utils/etcdutil"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func TestAdjustRule(t *testing.T) {
re := require.New(t)
rule := LabelRule{
Expand Down Expand Up @@ -95,7 +100,9 @@ func TestAdjustRule2(t *testing.T) {
func TestGetSetRule(t *testing.T) {
re := require.New(t)
store := endpoint.NewStorageEndpoint(kv.NewMemoryKV(), nil)
labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
labeler, err := NewRegionLabeler(ctx, store, time.Millisecond*10)
re.NoError(err)
rules := []*LabelRule{
{ID: "rule1", Labels: []RegionLabel{{Key: "k1", Value: "v1"}}, RuleType: "key-range", Data: MakeKeyRanges("1234", "5678")},
Expand Down Expand Up @@ -148,7 +155,9 @@ func TestTxnWithEtcd(t *testing.T) {
_, client, clean := etcdutil.NewTestEtcdCluster(t, 1)
defer clean()
store := storage.NewStorageWithEtcdBackend(client, "")
labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
labeler, err := NewRegionLabeler(ctx, store, time.Millisecond*10)
re.NoError(err)
// test patch rules in batch
rulesNum := 200
Expand Down Expand Up @@ -213,7 +222,9 @@ func TestTxnWithEtcd(t *testing.T) {
func TestIndex(t *testing.T) {
re := require.New(t)
store := endpoint.NewStorageEndpoint(kv.NewMemoryKV(), nil)
labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
labeler, err := NewRegionLabeler(ctx, store, time.Millisecond*10)
re.NoError(err)
rules := []*LabelRule{
{ID: "rule0", Labels: []RegionLabel{{Key: "k1", Value: "v0"}}, RuleType: "key-range", Data: MakeKeyRanges("", "")},
Expand Down Expand Up @@ -255,7 +266,9 @@ func TestIndex(t *testing.T) {
func TestSaveLoadRule(t *testing.T) {
re := require.New(t)
store := endpoint.NewStorageEndpoint(kv.NewMemoryKV(), nil)
labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
labeler, err := NewRegionLabeler(ctx, store, time.Millisecond*10)
re.NoError(err)
rules := []*LabelRule{
{ID: "rule1", Labels: []RegionLabel{{Key: "k1", Value: "v1"}}, RuleType: "key-range", Data: MakeKeyRanges("1234", "5678")},
Expand All @@ -266,8 +279,9 @@ func TestSaveLoadRule(t *testing.T) {
err := labeler.SetLabelRule(r)
re.NoError(err)
}

labeler, err = NewRegionLabeler(context.Background(), store, time.Millisecond*100)
ctx1, cancel1 := context.WithCancel(context.Background())
defer cancel1()
labeler, err = NewRegionLabeler(ctx1, store, time.Millisecond*100)
re.NoError(err)
for _, r := range rules {
r2 := labeler.GetLabelRule(r.ID)
Expand Down Expand Up @@ -300,7 +314,9 @@ func expectSameRules(re *require.Assertions, r1, r2 *LabelRule) {
func TestKeyRange(t *testing.T) {
re := require.New(t)
store := endpoint.NewStorageEndpoint(kv.NewMemoryKV(), nil)
labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
labeler, err := NewRegionLabeler(ctx, store, time.Millisecond*10)
re.NoError(err)
rules := []*LabelRule{
{ID: "rule1", Labels: []RegionLabel{{Key: "k1", Value: "v1"}}, RuleType: "key-range", Data: MakeKeyRanges("1234", "5678")},
Expand Down Expand Up @@ -341,7 +357,9 @@ func TestKeyRange(t *testing.T) {
func TestLabelerRuleTTL(t *testing.T) {
re := require.New(t)
store := endpoint.NewStorageEndpoint(kv.NewMemoryKV(), nil)
labeler, err := NewRegionLabeler(context.Background(), store, time.Millisecond*10)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
labeler, err := NewRegionLabeler(ctx, store, time.Millisecond*10)
re.NoError(err)
rules := []*LabelRule{
{
Expand Down Expand Up @@ -416,7 +434,9 @@ func TestGC(t *testing.T) {
re := require.New(t)
// set gcInterval to 1 hour.
store := endpoint.NewStorageEndpoint(kv.NewMemoryKV(), nil)
labeler, err := NewRegionLabeler(context.Background(), store, time.Hour)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
labeler, err := NewRegionLabeler(ctx, store, time.Hour)
re.NoError(err)
ttls := []string{"1ms", "1ms", "1ms", "5ms", "5ms", "10ms", "1h", "24h"}
start, _ := hex.DecodeString("1234")
Expand Down
6 changes: 6 additions & 0 deletions pkg/schedule/operator/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ import (
"github.com/tikv/pd/pkg/mock/mockcluster"
"github.com/tikv/pd/pkg/mock/mockconfig"
"github.com/tikv/pd/pkg/schedule/config"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

type operatorBuilderTestSuite struct {
suite.Suite

Expand Down
11 changes: 10 additions & 1 deletion pkg/schedule/operator/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
type operatorStepTestSuite struct {
suite.Suite

cancel context.CancelFunc
cluster *mockcluster.Cluster
}

Expand All @@ -44,7 +45,9 @@ type testCase struct {
}

func (suite *operatorStepTestSuite) SetupTest() {
suite.cluster = mockcluster.NewCluster(context.Background(), mockconfig.NewTestOptions())
ctx, cancel := context.WithCancel(context.Background())
suite.cancel = cancel
suite.cluster = mockcluster.NewCluster(ctx, mockconfig.NewTestOptions())
for i := 1; i <= 10; i++ {
suite.cluster.PutStoreWithLabels(uint64(i))
}
Expand All @@ -53,6 +56,12 @@ func (suite *operatorStepTestSuite) SetupTest() {
suite.cluster.SetStoreDown(10)
}

func (suite *operatorStepTestSuite) TearDownTest() {
if suite.cancel != nil {
suite.cancel()
}
}

func (suite *operatorStepTestSuite) TestTransferLeader() {
re := suite.Require()
step := TransferLeader{FromStore: 1, ToStore: 2}
Expand Down
6 changes: 6 additions & 0 deletions pkg/schedule/placement/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func TestTrim(t *testing.T) {
re := require.New(t)
rc := newRuleConfig()
Expand Down
6 changes: 6 additions & 0 deletions pkg/schedule/plan/balance_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

type balanceSchedulerPlanAnalyzeTestSuite struct {
suite.Suite

Expand Down
6 changes: 6 additions & 0 deletions pkg/schedule/rangelist/range_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func TestRangeList(t *testing.T) {
re := require.New(t)
rl := NewBuilder().Build()
Expand Down
6 changes: 6 additions & 0 deletions pkg/schedule/scatter/region_scatterer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ import (
"github.com/tikv/pd/pkg/schedule/hbstream"
"github.com/tikv/pd/pkg/schedule/operator"
"github.com/tikv/pd/pkg/schedule/placement"
"github.com/tikv/pd/pkg/utils/testutil"
"github.com/tikv/pd/pkg/versioninfo"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

type sequencer struct {
minID uint64
maxID uint64
Expand Down
6 changes: 6 additions & 0 deletions pkg/schedule/schedulers/balance_leader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ import (
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/mock/mockcluster"
"github.com/tikv/pd/pkg/utils/testutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

func TestBalanceLeaderSchedulerConfigClone(t *testing.T) {
re := require.New(t)
keyRanges1, _ := getKeyRanges([]string{"a", "b", "c", "d"})
Expand Down
Loading

0 comments on commit 88504dd

Please sign in to comment.