z_last_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "net/http"
  7. "runtime"
  8. "sort"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func interestingGoroutines() (gs []string) {
  14. buf := make([]byte, 2<<20)
  15. buf = buf[:runtime.Stack(buf, true)]
  16. for _, g := range strings.Split(string(buf), "\n\n") {
  17. sl := strings.SplitN(g, "\n", 2)
  18. if len(sl) != 2 {
  19. continue
  20. }
  21. stack := strings.TrimSpace(sl[1])
  22. if stack == "" ||
  23. strings.Contains(stack, "created by testing.RunTests") ||
  24. strings.Contains(stack, "testing.Main(") ||
  25. strings.Contains(stack, "runtime.goexit") ||
  26. strings.Contains(stack, "created by runtime.gc") ||
  27. strings.Contains(stack, "runtime.MHeap_Scavenger") {
  28. continue
  29. }
  30. gs = append(gs, stack)
  31. }
  32. sort.Strings(gs)
  33. return
  34. }
  35. // Verify the other tests didn't leave any goroutines running.
  36. // This is in a file named z_last_test.go so it sorts at the end.
  37. func TestGoroutinesRunning(t *testing.T) {
  38. t.Skip("TODO: etcdserver.Sender may still dial closed remote endpoint and need some time to timeout.")
  39. if testing.Short() {
  40. t.Skip("not counting goroutines for leakage in -short mode")
  41. }
  42. gs := interestingGoroutines()
  43. n := 0
  44. stackCount := make(map[string]int)
  45. for _, g := range gs {
  46. stackCount[g]++
  47. n++
  48. }
  49. t.Logf("num goroutines = %d", n)
  50. if n > 0 {
  51. t.Error("Too many goroutines.")
  52. for stack, count := range stackCount {
  53. t.Logf("%d instances of:\n%s", count, stack)
  54. }
  55. }
  56. }
  57. func afterTest(t *testing.T) {
  58. http.DefaultTransport.(*http.Transport).CloseIdleConnections()
  59. if testing.Short() {
  60. return
  61. }
  62. var bad string
  63. badSubstring := map[string]string{
  64. // TODO: there might exist a bug in http package, which will leave
  65. // readLoop without writeLoop after close all idle connections.
  66. // comment this line until we have time to dig into it.
  67. // ").readLoop(": "a Transport",
  68. ").writeLoop(": "a Transport",
  69. "created by net/http/httptest.(*Server).Start": "an httptest.Server",
  70. "timeoutHandler": "a TimeoutHandler",
  71. // TODO: dial goroutines leaks even if the request is cancelled.
  72. // It needs to wait dial timeout to recycle the goroutine.
  73. // comment this line until we have time to dig into it.
  74. "net.(*netFD).connect(": "a timing out dial",
  75. ").noteClientGone(": "a closenotifier sender",
  76. }
  77. var stacks string
  78. for i := 0; i < 6; i++ {
  79. bad = ""
  80. stacks = strings.Join(interestingGoroutines(), "\n\n")
  81. for substr, what := range badSubstring {
  82. if strings.Contains(stacks, substr) {
  83. bad = what
  84. }
  85. }
  86. if bad == "" {
  87. return
  88. }
  89. // Bad stuff found, but goroutines might just still be
  90. // shutting down, so give it some time.
  91. time.Sleep(50 * time.Millisecond)
  92. }
  93. t.Errorf("Test appears to have leaked %s:\n%s", bad, stacks)
  94. }