integration framework: log number of leaked goroutines

This commit is contained in:
Antonio Ojea 2022-03-01 11:18:37 +01:00
parent 77f6476d34
commit 2660ac8a6d

View File

@ -172,12 +172,30 @@ func EtcdMain(tests func() int) {
// Bail out early when -help was given as parameter.
flag.Parse()
before := runtime.NumGoroutine()
stop, err := startEtcd()
if err != nil {
klog.Fatalf("cannot run integration tests: unable to start etcd: %v", err)
}
result := tests()
stop() // Don't defer this. See os.Exit documentation.
// Log the number of goroutines leaked.
// TODO: we should work on reducing this as much as possible.
var dg int
for i := 0; i < 10; i++ {
// Leave some room for goroutines we can not get rid of
// like k8s.io/klog/v2.(*loggingT).flushDaemon()
// TODO: adjust this number based on a more exhaustive analysis.
if dg = runtime.NumGoroutine() - before; dg <= 4 {
os.Exit(result)
}
// Allow goroutines to schedule and die off.
runtime.Gosched()
time.Sleep(100 * time.Millisecond)
}
after := runtime.NumGoroutine()
klog.Infof("unexpected number of goroutines: before: %d after %d", before, after)
os.Exit(result)
}