瀏覽代碼

pkg/testutil: more aggressive goroutine stack trace coalescing

Strips out the pointer arguments in the header of the stack trace so
that more stack traces match each other.
Anthony Romano 10 年之前
父節點
當前提交
72b31d6fdc
共有 1 個文件被更改,包括 8 次插入6 次删除
  1. 8 6
      pkg/testutil/leak.go

+ 8 - 6
pkg/testutil/leak.go

@@ -8,6 +8,7 @@ import (
 	"fmt"
 	"net/http"
 	"os"
+	"regexp"
 	"runtime"
 	"sort"
 	"strings"
@@ -42,17 +43,18 @@ func CheckLeakedGoroutine() bool {
 		return false
 	}
 	gs := interestingGoroutines()
+	if len(gs) == 0 {
+		return false
+	}
 
-	n := 0
 	stackCount := make(map[string]int)
+	re := regexp.MustCompile("\\(0[0-9a-fx, ]*\\)")
 	for _, g := range gs {
-		stackCount[g]++
-		n++
+		// strip out pointer arguments in first function of stack dump
+		normalized := string(re.ReplaceAll([]byte(g), []byte("(...)")))
+		stackCount[normalized]++
 	}
 
-	if n == 0 {
-		return false
-	}
 	fmt.Fprintf(os.Stderr, "Too many goroutines running after all test(s).\n")
 	for stack, count := range stackCount {
 		fmt.Fprintf(os.Stderr, "%d instances of:\n%s\n", count, stack)