-
Notifications
You must be signed in to change notification settings - Fork 0
/
leak_test.go
36 lines (26 loc) · 844 Bytes
/
leak_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
package problems
import (
"runtime"
"testing"
)
// Go Routines Leak: go routines are not garbage collected even when blocked on unreachable channels.
func Test_Leak(t *testing.T) {
block := make(chan bool)
// start N go routines
for i := 0; i < 1000; i++ {
// block on channel receive
go func(c chan bool) {
// channel is unreachable as there are no possible receivers
c <- true
}(block)
}
// number of live go routines
liveGoRoutines := runtime.NumGoroutine()
t.Logf("number of go routines before GC: %v", liveGoRoutines)
// force GC
runtime.GC()
// assert that blocked go routines are not garbage collected when blocked on unreachable channels
if liveGoRoutines == runtime.NumGoroutine() {
t.Logf("expected number of go routines to have decreased after GC but was still: %v", runtime.NumGoroutine())
}
}