z_last_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "net.(*netFD).connect(": "a timing out dial",
  72. ").noteClientGone(": "a closenotifier sender",
  73. }
  74. var stacks string
  75. for i := 0; i < 6; i++ {
  76. bad = ""
  77. stacks = strings.Join(interestingGoroutines(), "\n\n")
  78. for substr, what := range badSubstring {
  79. if strings.Contains(stacks, substr) {
  80. bad = what
  81. }
  82. }
  83. if bad == "" {
  84. return
  85. }
  86. // Bad stuff found, but goroutines might just still be
  87. // shutting down, so give it some time.
  88. time.Sleep(50 * time.Millisecond)
  89. }
  90. t.Errorf("Test appears to have leaked %s:\n%s", bad, stacks)
  91. }