-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathleaktest_test.go
227 lines (210 loc) · 5.02 KB
/
leaktest_test.go
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package leaktest
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
type testReporter struct {
failed bool
msg string
}
func (tr *testReporter) Errorf(format string, args ...interface{}) {
tr.failed = true
tr.msg = fmt.Sprintf(format, args...)
}
// Client for the TestServer
var testServer *httptest.Server
func TestCheck(t *testing.T) {
leakyFuncs := []struct {
f func()
name string
expectLeak bool
}{
{
name: "Infinite for loop",
expectLeak: true,
f: func() {
for {
time.Sleep(time.Second)
}
},
},
{
name: "Select on a channel not referenced by other goroutines.",
expectLeak: true,
f: func() {
c := make(chan struct{})
<-c
},
},
{
name: "Blocked select on channels not referenced by other goroutines.",
expectLeak: true,
f: func() {
c := make(chan struct{})
c2 := make(chan struct{})
select {
case <-c:
case c2 <- struct{}{}:
}
},
},
{
name: "Blocking wait on sync.Mutex that isn't referenced by other goroutines.",
expectLeak: true,
f: func() {
var mu sync.Mutex
mu.Lock()
mu.Lock()
},
},
{
name: "Blocking wait on sync.RWMutex that isn't referenced by other goroutines.",
expectLeak: true,
f: func() {
var mu sync.RWMutex
mu.RLock()
mu.Lock()
},
},
{
name: "HTTP Client with KeepAlive Disabled.",
expectLeak: false,
f: func() {
tr := &http.Transport{
DisableKeepAlives: true,
}
client := &http.Client{Transport: tr}
_, err := client.Get(testServer.URL)
if err != nil {
t.Error(err)
}
},
},
{
name: "HTTP Client with KeepAlive Enabled.",
expectLeak: true,
f: func() {
tr := &http.Transport{
DisableKeepAlives: false,
}
client := &http.Client{Transport: tr}
_, err := client.Get(testServer.URL)
if err != nil {
t.Error(err)
}
},
},
}
// Start our keep alive server for keep alive tests
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testServer = startKeepAliveEnabledServer(ctx)
// this works because the running goroutine is left running at the
// start of the next test case - so the previous leaks don't affect the
// check for the next one
for _, leakyTestcase := range leakyFuncs {
t.Run(leakyTestcase.name, func(t *testing.T) {
checker := &testReporter{}
snapshot := CheckTimeout(checker, time.Second)
go leakyTestcase.f()
snapshot()
if !checker.failed && leakyTestcase.expectLeak {
t.Error("didn't catch sleeping goroutine")
}
if checker.failed && !leakyTestcase.expectLeak {
t.Error("got leak but didn't expect it")
}
})
}
}
// TestSlowTest verifies that the timeout works on slow tests: it should
// be based on time after the test finishes rather than time after the test's
// start.
func TestSlowTest(t *testing.T) {
defer CheckTimeout(t, 1000 * time.Millisecond)()
go time.Sleep(1500 * time.Millisecond)
time.Sleep(750 * time.Millisecond)
}
func TestEmptyLeak(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
defer CheckContext(ctx, t)()
time.Sleep(time.Second)
}
// TestChangingStackTrace validates that a change in a preexisting goroutine's
// stack is not detected as a leaked goroutine.
func TestChangingStackTrace(t *testing.T) {
started := make(chan struct{})
c1 := make(chan struct{})
c2 := make(chan struct{})
defer close(c2)
go func() {
close(started)
<-c1
<-c2
}()
<-started
func() {
defer CheckTimeout(t, time.Second)()
close(c1)
}()
}
func TestInterestingGoroutine(t *testing.T) {
s := "goroutine 123 [running]:\nmain.main()"
gr, err := interestingGoroutine(s)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if gr.id != 123 {
t.Errorf("goroutine id = %d; want %d", gr.id, 123)
}
if gr.stack != s {
t.Errorf("goroutine stack = %q; want %q", gr.stack, s)
}
stacks := []struct {
stack string
err error
}{
{
stack: "goroutine 123 [running]:",
err: errors.New(`error parsing stack: "goroutine 123 [running]:"`),
},
{
stack: "goroutine 299 [IO wait]:\nnet/http.(*persistConn).readLoop(0xc420556240)",
err: nil,
},
{
stack: "goroutine 123 [running]:\ntesting.RunTests",
err: nil,
},
{
stack: "goroutine 123 [running]:\nfoo\nbar\nruntime.goexit\nbaz",
err: nil,
},
{
stack: "goroutine 123:\nmain.main()",
err: errors.New(`error parsing stack header: "goroutine 123:"`),
},
{
stack: "goroutine NaN [running]:\nmain.main()",
err: errors.New(`error parsing goroutine id: strconv.ParseUint: parsing "NaN": invalid syntax`),
},
}
for i, s := range stacks {
gr, err := interestingGoroutine(s.stack)
if s.err == nil && err != nil {
t.Errorf("%d: error = %v; want nil", i, err)
} else if s.err != nil && (err == nil || err.Error() != s.err.Error()) {
t.Errorf("%d: error = %v; want %s", i, err, s.err)
}
if gr != nil {
t.Errorf("%d: gr = %v; want nil", i, gr)
}
}
}