25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

120 satır
3.1 KiB

  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package leakcheck contains functions to check leaked goroutines.
  15. //
  16. // Call "defer leakcheck.Check(t)" at the beginning of tests.
  17. //
  18. // This is very closely based on grpc-go's leakcheck.
  19. package leakcheck
  20. import (
  21. "runtime"
  22. "sort"
  23. "strings"
  24. "time"
  25. )
  26. var goroutinesToIgnore = []string{
  27. "net/http/transport.go",
  28. "net/http/h2_bundle.go",
  29. "src/go.opencensus.io/stats/view/worker.go",
  30. "testing.Main(",
  31. "testing.tRunner(",
  32. "testing.(*M).",
  33. "runtime.goexit",
  34. "created by runtime.gc",
  35. "created by runtime/trace.Start",
  36. "interestingGoroutines",
  37. "runtime.MHeap_Scavenger",
  38. "signal.signal_recv",
  39. "sigterm.handler",
  40. "runtime_mcall",
  41. "(*loggingT).flushDaemon",
  42. "goroutine in C code",
  43. }
  44. // RegisterIgnoreGoroutine appends s into the ignore goroutine list. The
  45. // goroutines whose stack trace contains s will not be identified as leaked
  46. // goroutines. Not thread-safe, only call this function in init().
  47. func RegisterIgnoreGoroutine(s string) {
  48. goroutinesToIgnore = append(goroutinesToIgnore, s)
  49. }
  50. func ignore(g string) bool {
  51. sl := strings.SplitN(g, "\n", 2)
  52. if len(sl) != 2 {
  53. return true
  54. }
  55. stack := strings.TrimSpace(sl[1])
  56. if strings.HasPrefix(stack, "testing.RunTests") {
  57. return true
  58. }
  59. if stack == "" {
  60. return true
  61. }
  62. for _, s := range goroutinesToIgnore {
  63. if strings.Contains(stack, s) {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. // interestingGoroutines returns all goroutines we care about for the purpose of
  70. // leak checking. It excludes testing or runtime ones.
  71. func interestingGoroutines() (gs []string) {
  72. buf := make([]byte, 2<<20)
  73. buf = buf[:runtime.Stack(buf, true)]
  74. for _, g := range strings.Split(string(buf), "\n\n") {
  75. if !ignore(g) {
  76. gs = append(gs, g)
  77. }
  78. }
  79. sort.Strings(gs)
  80. return
  81. }
  82. // Errorfer is the interface that wraps the Errorf method. It's a subset of
  83. // testing.TB to make it easy to use Check.
  84. type Errorfer interface {
  85. Errorf(format string, args ...interface{})
  86. }
  87. func check(efer Errorfer, timeout time.Duration) {
  88. // Loop, waiting for goroutines to shut down.
  89. // Wait up to timeout, but finish as quickly as possible.
  90. deadline := time.Now().Add(timeout)
  91. var leaked []string
  92. for time.Now().Before(deadline) {
  93. if leaked = interestingGoroutines(); len(leaked) == 0 {
  94. return
  95. }
  96. time.Sleep(50 * time.Millisecond)
  97. }
  98. for _, g := range leaked {
  99. efer.Errorf("Leaked goroutine: %v", g)
  100. }
  101. }
  102. // Check looks at the currently-running goroutines and checks if there are any
  103. // interestring (created by gRPC) goroutines leaked. It waits up to 10 seconds
  104. // in the error cases.
  105. func Check(efer Errorfer) {
  106. check(efer, 10*time.Second)
  107. }