main_test.go 3.1 KB

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