leak.go 3.4 KB

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