forked from temporalio/temporal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoroutine.go
More file actions
140 lines (120 loc) · 3.29 KB
/
goroutine.go
File metadata and controls
140 lines (120 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package runtime
import (
"errors"
"fmt"
"os"
"reflect"
"runtime"
"runtime/pprof"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type (
WaitOptions struct {
CheckInterval time.Duration
MaxDuration time.Duration
NumGoRoutines int
}
)
var (
defaultWaitOptions = WaitOptions{
CheckInterval: 1 * time.Millisecond,
MaxDuration: 1 * time.Second,
NumGoRoutines: 1,
}
)
func WithCheckInterval(checkInterval time.Duration) func(*WaitOptions) {
return func(wo *WaitOptions) {
wo.CheckInterval = checkInterval
}
}
func WithMaxDuration(maxDuration time.Duration) func(*WaitOptions) {
return func(wo *WaitOptions) {
wo.MaxDuration = maxDuration
}
}
func WithNumGoRoutines(numGoRoutines int) func(*WaitOptions) {
return func(wo *WaitOptions) {
wo.NumGoRoutines = numGoRoutines
}
}
// WaitGoRoutineWithFn waits for a go routine with the given function to appear in call stacks,
// using different WaitOptions, and returns the number of attempts needed to find the go routine.
func WaitGoRoutineWithFn(t testing.TB, fn any, opts ...func(*WaitOptions)) int {
wo := defaultWaitOptions
for _, opt := range opts {
opt(&wo)
}
fnName, err := functionName(fn)
require.NoError(t, err)
attempt := 1
numFound := 0
require.Eventually(t,
func() bool {
numFound, err = numGoRoutinesWithFn(fnName)
require.NoError(t, err)
if numFound == wo.NumGoRoutines {
t.Logf("Found %s function %d times on %d attempt\n", fnName, numFound, attempt)
return true
}
attempt++
return false
},
wo.MaxDuration,
wo.CheckInterval,
"Function %s must be found %d times but was found %d times in all go routine call stacks after %s", fnName, wo.NumGoRoutines, numFound, wo.MaxDuration.String())
return attempt
}
func AssertNoGoRoutineWithFn(t testing.TB, fn any) {
fnName, err := functionName(fn)
require.NoError(t, err)
numFound, err := numGoRoutinesWithFn(fnName)
require.NoError(t, err)
require.Zero(t, numFound)
}
// PrintGoRoutines prints all go routines.
func PrintGoRoutines() {
_ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
}
func numGoRoutinesWithFn(fnName string) (int, error) {
// 20 is a buffer for go routines that might be created between the next 2 lines. 10 is not enough!
stackRecords := make([]runtime.StackRecord, runtime.NumGoroutine()+20)
stackRecordsLen, ok := runtime.GoroutineProfile(stackRecords)
if !ok {
return 0, errors.New(fmt.Sprintf("Size %d is too small for stack records. Need %d", len(stackRecords), stackRecordsLen))
}
numFound := 0
for _, stackRecord := range stackRecords {
frames := runtime.CallersFrames(stackRecord.Stack())
for {
frame, more := frames.Next()
if strings.Contains(frame.Function, fnName) {
numFound++
}
if !more {
break
}
}
}
return numFound, nil
}
func functionName(fn any) (string, error) {
if fnName, isString := fn.(string); isString {
return fnName, nil
}
if fnName, isFunc := functionNameForPC(reflect.ValueOf(fn).Pointer()); isFunc {
return fnName, nil
}
return "", errors.New(fmt.Sprintf("Invalid function %#v", fn))
}
func functionNameForPC(pc uintptr) (string, bool) {
fn := runtime.FuncForPC(pc)
if fn == nil {
return "", false
}
elements := strings.Split(fn.Name(), ".")
shortName := elements[len(elements)-1]
return strings.TrimSuffix(shortName, "-fm"), true
}