z_last_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. ").readLoop(": "a Transport",
  65. ").writeLoop(": "a Transport",
  66. "created by net/http/httptest.(*Server).Start": "an httptest.Server",
  67. "timeoutHandler": "a TimeoutHandler",
  68. "net.(*netFD).connect(": "a timing out dial",
  69. ").noteClientGone(": "a closenotifier sender",
  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. }