From 06b50370fab1e5aa2bef081f0637a442e5701966 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Sun, 31 Mar 2024 00:21:51 +0800 Subject: [PATCH] client/pkg/testutil: add shouldSkip func for interestingGoroutines interestingGoroutines uses so many strings.Contains checks to determine whether to continue, I think it can be avoided. This patch introduces shouldSkip func to do the checks in a loop to make the logic clearer. Signed-off-by: Jes Cok --- client/pkg/testutil/leak.go | 56 ++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/client/pkg/testutil/leak.go b/client/pkg/testutil/leak.go index 9e7a738efa0..bf5aecd62f8 100644 --- a/client/pkg/testutil/leak.go +++ b/client/pkg/testutil/leak.go @@ -130,28 +130,44 @@ func interestingGoroutines() (gs []string) { continue } stack := strings.TrimSpace(sl[1]) - if stack == "" || - strings.Contains(stack, "sync.(*WaitGroup).Done") || - strings.Contains(stack, "os.(*file).close") || - strings.Contains(stack, "os.(*Process).Release") || - strings.Contains(stack, "created by os/signal.init") || - strings.Contains(stack, "runtime/panic.go") || - strings.Contains(stack, "created by testing.RunTests") || - strings.Contains(stack, "created by testing.runTests") || - strings.Contains(stack, "created by testing.(*T).Run") || - strings.Contains(stack, "testing.Main(") || - strings.Contains(stack, "runtime.goexit") || - strings.Contains(stack, "go.etcd.io/etcd/client/pkg/v3/testutil.interestingGoroutines") || - strings.Contains(stack, "go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop") || - strings.Contains(stack, "github.com/golang/glog.(*loggingT).flushDaemon") || - strings.Contains(stack, "created by runtime.gc") || - strings.Contains(stack, "created by text/template/parse.lex") || - strings.Contains(stack, "runtime.MHeap_Scavenger") || - strings.Contains(stack, "rcrypto/internal/boring.(*PublicKeyRSA).finalize") || - strings.Contains(stack, "net.(*netFD).Close(") || - strings.Contains(stack, "testing.(*T).Run") { + if stack == "" { continue } + + shouldSkip := func() bool { + var uninterestingMsgs = [...]string{ + "sync.(*WaitGroup).Done", + "os.(*file).close", + "os.(*Process).Release", + "created by os/signal.init", + "runtime/panic.go", + "created by testing.RunTests", + "created by testing.runTests", + "created by testing.(*T).Run", + "testing.Main(", + "runtime.goexit", + "go.etcd.io/etcd/client/pkg/v3/testutil.interestingGoroutines", + "go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop", + "github.com/golang/glog.(*loggingT).flushDaemon", + "created by runtime.gc", + "created by text/template/parse.lex", + "runtime.MHeap_Scavenger", + "rcrypto/internal/boring.(*PublicKeyRSA).finalize", + "net.(*netFD).Close(", + "testing.(*T).Run", + } + for _, msg := range uninterestingMsgs { + if strings.Contains(stack, msg) { + return true + } + } + return false + }() + + if shouldSkip { + continue + } + gs = append(gs, stack) } sort.Strings(gs)