Skip to content

Commit 86a73b5

Browse files
committed
add rough test to detect goroutine leak
1 parent c4301e3 commit 86a73b5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

timeout_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,23 @@ func TestRunContext(t *testing.T) {
222222
}
223223
})
224224
}
225+
226+
func TestRun_leak(t *testing.T) {
227+
beforeGoroutine := runtime.NumGoroutine()
228+
for range make([]struct{}, 30) {
229+
tio := &Timeout{
230+
Cmd: exec.Command(stubCmd, "-sleep=0.1"),
231+
Duration: time.Second,
232+
KillAfter: time.Second,
233+
}
234+
go func() {
235+
tio.Run()
236+
}()
237+
}
238+
time.Sleep(time.Second)
239+
afterGoroutine := runtime.NumGoroutine()
240+
241+
if beforeGoroutine+10 < afterGoroutine {
242+
t.Errorf("goroutine may be leaked. before: %d, after: %d", beforeGoroutine, afterGoroutine)
243+
}
244+
}

0 commit comments

Comments
 (0)