leak.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package testutil
  5. import (
  6. "fmt"
  7. "net/http"
  8. "os"
  9. "regexp"
  10. "runtime"
  11. "sort"
  12. "strings"
  13. "testing"
  14. "time"
  15. )
  16. /*
  17. CheckLeakedGoroutine verifies tests do not leave any leaky
  18. goroutines. It returns true when there are goroutines still
  19. running(leaking) after all tests.
  20. import "github.com/coreos/etcd/pkg/testutil"
  21. func TestMain(m *testing.M) {
  22. v := m.Run()
  23. if v == 0 && testutil.CheckLeakedGoroutine() {
  24. os.Exit(1)
  25. }
  26. os.Exit(v)
  27. }
  28. func TestSample(t *testing.T) {
  29. defer testutil.AfterTest(t)
  30. ...
  31. }
  32. */
  33. func CheckLeakedGoroutine() bool {
  34. if testing.Short() {
  35. // not counting goroutines for leakage in -short mode
  36. return false
  37. }
  38. gs := interestingGoroutines()
  39. if len(gs) == 0 {
  40. return false
  41. }
  42. stackCount := make(map[string]int)
  43. re := regexp.MustCompile(`\(0[0-9a-fx, ]*\)`)
  44. for _, g := range gs {
  45. // strip out pointer arguments in first function of stack dump
  46. normalized := string(re.ReplaceAll([]byte(g), []byte("(...)")))
  47. stackCount[normalized]++
  48. }
  49. fmt.Fprintf(os.Stderr, "Too many goroutines running after all test(s).\n")
  50. for stack, count := range stackCount {
  51. fmt.Fprintf(os.Stderr, "%d instances of:\n%s\n", count, stack)
  52. }
  53. return true
  54. }
  55. func AfterTest(t *testing.T) {
  56. http.DefaultTransport.(*http.Transport).CloseIdleConnections()
  57. if testing.Short() {
  58. return
  59. }
  60. var bad string
  61. badSubstring := map[string]string{
  62. ").writeLoop(": "a Transport",
  63. "created by net/http/httptest.(*Server).Start": "an httptest.Server",
  64. "timeoutHandler": "a TimeoutHandler",
  65. "net.(*netFD).connect(": "a timing out dial",
  66. ").noteClientGone(": "a closenotifier sender",
  67. ").readLoop(": "a Transport",
  68. }
  69. var stacks string
  70. for i := 0; i < 6; i++ {
  71. bad = ""
  72. stacks = strings.Join(interestingGoroutines(), "\n\n")
  73. for substr, what := range badSubstring {
  74. if strings.Contains(stacks, substr) {
  75. bad = what
  76. }
  77. }
  78. if bad == "" {
  79. return
  80. }
  81. // Bad stuff found, but goroutines might just still be
  82. // shutting down, so give it some time.
  83. time.Sleep(50 * time.Millisecond)
  84. }
  85. t.Errorf("Test appears to have leaked %s:\n%s", bad, stacks)
  86. }
  87. func interestingGoroutines() (gs []string) {
  88. buf := make([]byte, 2<<20)
  89. buf = buf[:runtime.Stack(buf, true)]
  90. for _, g := range strings.Split(string(buf), "\n\n") {
  91. sl := strings.SplitN(g, "\n", 2)
  92. if len(sl) != 2 {
  93. continue
  94. }
  95. stack := strings.TrimSpace(sl[1])
  96. if stack == "" ||
  97. strings.Contains(stack, "created by testing.RunTests") ||
  98. strings.Contains(stack, "testing.Main(") ||
  99. strings.Contains(stack, "runtime.goexit") ||
  100. strings.Contains(stack, "github.com/coreos/etcd/pkg/testutil.interestingGoroutines") ||
  101. strings.Contains(stack, "github.com/coreos/etcd/pkg/logutil.(*MergeLogger).outputLoop") ||
  102. strings.Contains(stack, "github.com/golang/glog.(*loggingT).flushDaemon") ||
  103. strings.Contains(stack, "created by runtime.gc") ||
  104. strings.Contains(stack, "runtime.MHeap_Scavenger") {
  105. continue
  106. }
  107. gs = append(gs, stack)
  108. }
  109. sort.Strings(gs)
  110. return
  111. }