Repository: matrixorigin/matrixcube
Pull request #229 (2021-09-10 08:23:51)

added a wrapper for the leaktest util
**What type of PR is this?**

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

**Which issue(s) this PR fixes:**

Fixes #

**What this PR does / why we need it:**

Not Available

**Special notes for your reviewer:**

Not Available

**Additional documentation (e.g. design docs, usage docs, etc.):**

Not Available



Patch:
diff --git a/go.mod b/go.mod
index 78715e2..eceffdf 100644
--- a/go.mod
+++ b/go.mod
@@ -18,6 +18,7 @@ require (
 	github.com/gogo/protobuf v1.3.2
 	github.com/google/btree v1.0.1
 	github.com/juju/ratelimit v1.0.1
+	github.com/lni/goutils v1.3.0
 	github.com/lni/vfs v0.2.1-0.20210810090357-27c7525cf64f
 	github.com/montanaflynn/stats v0.6.6
 	github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d
diff --git a/go.sum b/go.sum
index df9d68d..6f14e76 100644
--- a/go.sum
+++ b/go.sum
@@ -68,6 +68,7 @@ github.com/cockroachdb/datadriven v1.0.0 h1:uhZrAfEayBecH2w2tZmhe20HJ7hDvrrA4x2B
 github.com/cockroachdb/datadriven v1.0.0/go.mod h1:5Ib8Meh+jk1RlHIXej6Pzevx/NLlNvQB9pmSBZErGA4=
 github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
 github.com/cockroachdb/errors v1.6.1/go.mod h1:tm6FTP5G81vwJ5lC0SizQo374JNCOPrHyXGitRJoDqM=
+github.com/cockroachdb/errors v1.7.5/go.mod h1:m/IWRCPXYZ6TvLLDuC0kfLR1pp/+BiZ0h16WHaBMRMM=
 github.com/cockroachdb/errors v1.8.1/go.mod h1:qGwQn6JmZ+oMjuLwjWzUNqblqk0xl4CVV3SQbGwK7Ac=
 github.com/cockroachdb/errors v1.8.2 h1:rnnWK9Nn5kEMOGz9531HuDx/FOleL4NVH20VsDexVC8=
 github.com/cockroachdb/errors v1.8.2/go.mod h1:qGwQn6JmZ+oMjuLwjWzUNqblqk0xl4CVV3SQbGwK7Ac=
@@ -75,6 +76,7 @@ github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f h1:o/kfcElHqOi
 github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
 github.com/cockroachdb/pebble v0.0.0-20210503173641-1387689d3d7c h1:ejPG0gvsvhvxflGd33OK/B/jqhcFZaGSoTKaQUYC9Sk=
 github.com/cockroachdb/pebble v0.0.0-20210503173641-1387689d3d7c/go.mod h1:1XpB4cLQcF189RAcWi4gUc110zJgtOfT7SVNGY8sOe0=
+github.com/cockroachdb/redact v1.0.6/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
 github.com/cockroachdb/redact v1.0.8 h1:8QG/764wK+vmEYoOlfobpe12EQcS81ukx/a4hdVMxNw=
 github.com/cockroachdb/redact v1.0.8/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
 github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 h1:IKgmqgMQlVJIZj19CdocBeSfSaiCbEBZGKODaixqtHM=
@@ -186,6 +188,7 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
 github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM=
 github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
 github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
+github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
 github.com/golang/snappy v0.0.2-0.20190904063534-ff6b7dc882cf h1:gFVkHXmVAhEbxZVDln5V9GKrLaluNoFHDbrZwAWZgws=
 github.com/golang/snappy v0.0.2-0.20190904063534-ff6b7dc882cf/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
 github.com/gomodule/redigo v1.7.1-0.20190724094224-574c33c3df38/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
@@ -297,6 +300,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
 github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
+github.com/lni/goutils v1.3.0 h1:oBhV7Z5DjNWbcy/c3fFj6qo4SnHcpyTY28qfKvLy6UM=
+github.com/lni/goutils v1.3.0/go.mod h1:PUPtBAnZlRPUKWUXCsYkIRWubJbtNHpTAee0sczhlf4=
 github.com/lni/vfs v0.2.1-0.20210810090357-27c7525cf64f h1:ykmePP2E9ZuNRhfZL2bqjMdoYitgzYvIdVogbvnlvhs=
 github.com/lni/vfs v0.2.1-0.20210810090357-27c7525cf64f/go.mod h1:Z98QaAJnPYmgpw5VsksNdEPKpHDp4yeHTTvfHbX5JWE=
 github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=


Test patch:
diff --git a/util/leaktest/leak.go b/util/leaktest/leak.go
index d1b782e..cf16adf 100644
--- a/util/leaktest/leak.go
+++ b/util/leaktest/leak.go
@@ -1,28 +1,4 @@
-// Copyright 2013 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in licenses/BSD-golang.txt.
-
-// Portions of this file are additionally subject to the following
-// license and copyright.
-//
-// Copyright 2016 The Cockroach Authors.
-//
-// 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.
-//
-// Portions of this file are additionally subject to the following
-// license and copyright.
-//
-// Copyright 2016 Peter Mattis.
+// Copyright 2021 MatrixOrigin.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -32,149 +8,24 @@
 //
 // 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. See the AUTHORS file
-// for names of contributors.
-//
-// Package leaktest provides tools to detect leaked goroutines in tests.
-// To use it, call "defer leaktest.AfterTest(t)()" at the beginning of each
-// test that may use goroutines.
-//
-// original code is from
-// https://github.com/cockroachdb/cockroach/blob/master/pkg/util/leaktest/leaktest.go
-// https://github.com/petermattis/goid/blob/master/goid.go
-//
-// see licensing & copyright info above
+// See the License for the specific language governing permissions and
+// limitations under the License.
 
 package leaktest
 
 import (
-	"bytes"
-	"log"
-	"runtime"
-	"sort"
-	"strconv"
-	"strings"
 	"testing"
-	"time"
-)
 
-// ExtractGID returns the GID of the goroutine.
-// from https://github.com/petermattis/goid/blob/master/goid.go
-func ExtractGID(s []byte) int64 {
-	s = s[len("goroutine "):]
-	s = s[:bytes.IndexByte(s, ' ')]
-	gid, _ := strconv.ParseInt(string(s), 10, 64)
-	return gid
-}
-
-// interestingGoroutines returns all goroutines we care about for the purpose
-// of leak checking. It excludes testing or runtime ones.
-func interestingGoroutines() map[int64]string {
-	buf := make([]byte, 2<<20)
-	buf = buf[:runtime.Stack(buf, true)]
-	gs := make(map[int64]string)
-	for _, g := range strings.Split(string(buf), "\n\n") {
-		sl := strings.SplitN(g, "\n", 2)
-		if len(sl) != 2 {
-			continue
-		}
-		stack := strings.TrimSpace(sl[1])
-		if strings.HasPrefix(stack, "testing.RunTests") {
-			continue
-		}
+	ltlib "github.com/lni/goutils/leaktest"
+)
 
-		if stack == "" ||
-			// Below are the stacks ignored by the upstream leaktest code.
-			strings.Contains(stack, "testing.Main(") ||
-			strings.Contains(stack, "testing.tRunner(") ||
-			strings.Contains(stack, "runtime.goexit") ||
-			strings.Contains(stack, "created by runtime.gc") ||
-			strings.Contains(stack, "interestingGoroutines") ||
-			strings.Contains(stack, "runtime.MHeap_Scavenger") ||
-			strings.Contains(stack, "signal.signal_recv") ||
-			strings.Contains(stack, "sigterm.handler") ||
-			strings.Contains(stack, "runtime_mcall") ||
-			strings.Contains(stack, "goroutine in C code") ||
-			strings.Contains(stack, "runtime.CPUProfile") {
-			continue
-		}
-		gs[ExtractGID([]byte(g))] = g
-	}
-	return gs
-}
+var (
+	disabled = true
+)
 
-// AfterTest snapshots the currently-running goroutines and returns a
-// function to be run at the end of tests to see whether any
-// goroutines leaked.
 func AfterTest(t testing.TB) func() {
-	orig := interestingGoroutines()
-	return func() {
-		// FIXME: remove this when goroutine leaks are fixed in cube
-		return
-		if t.Failed() {
-			return
-		}
-		if r := recover(); r != nil {
-			panic(r)
-		}
-		count := 0
-		for {
-			var leaked []string
-			for id, stack := range interestingGoroutines() {
-				if _, ok := orig[id]; !ok {
-					leaked = append(leaked, stack)
-				}
-			}
-			if len(leaked) == 0 {
-				return
-			}
-			if count < 100 {
-				count++
-				time.Sleep(50 * time.Millisecond)
-				continue
-			}
-			sort.Strings(leaked)
-			for _, g := range leaked {
-				t.Errorf("Leaked goroutine: %v", g)
-			}
-			return
-		}
-	}
-}
-
-// GetInterestedGoroutines returns a set of interested goroutines.
-func GetInterestedGoroutines() map[int64]string {
-	return interestingGoroutines()
-}
-
-// AssertNoGoroutineLeak checks and ensures that there is no extra goroutine
-// other than the ones included in the init map.
-func AssertNoGoroutineLeak(init map[int64]string) {
-	count := 0
-	for {
-		var leaked []string
-		for id, stack := range interestingGoroutines() {
-			if _, ok := init[id]; !ok {
-				leaked = append(leaked, stack)
-			}
-		}
-		if len(leaked) == 0 {
-			return
-		}
-		if count < 100 {
-			count++
-			time.Sleep(50 * time.Millisecond)
-			continue
-		}
-		sort.Strings(leaked)
-		for _, g := range leaked {
-			log.Fatalf("Leaked goroutine: %v", g)
-		}
-		if len(leaked) > 0 {
-			panic("goroutine leak identified")
-		}
-		return
+	if disabled {
+		return func() {}
 	}
+	return ltlib.AfterTest(t)
 }