Skip to content

Commit a124ea2

Browse files
committed
add several tests cases from golang/go#12989
1 parent 8dba077 commit a124ea2

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

leaktest_test.go

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package leaktest
22

33
import (
44
"fmt"
5+
"sync"
56
"testing"
67
"time"
78
)
@@ -16,18 +17,62 @@ func (tr *testReporter) Errorf(format string, args ...interface{}) {
1617
tr.msg = fmt.Sprintf(format, args)
1718
}
1819

19-
func TestCheck(t *testing.T) {
20-
checker := &testReporter{}
21-
22-
snapshot := Check(checker)
23-
go func() {
20+
var leakyFuncs = []func(){
21+
// Infinite for loop
22+
func() {
2423
for {
2524
time.Sleep(time.Second)
2625
}
27-
}()
26+
},
27+
// Select on a channel not referenced by other goroutines.
28+
func() {
29+
c := make(chan struct{}, 0)
30+
select {
31+
case <-c:
32+
}
33+
},
34+
// Blocked select on channels not referenced by other goroutines.
35+
func() {
36+
c := make(chan struct{}, 0)
37+
c2 := make(chan struct{}, 0)
38+
select {
39+
case <-c:
40+
case c2 <- struct{}{}:
41+
}
42+
},
43+
// Blocking wait on sync.Mutex that isn't referenced by other goroutines.
44+
func() {
45+
var mu sync.Mutex
46+
mu.Lock()
47+
mu.Lock()
48+
},
49+
// Blocking wait on sync.RWMutex that isn't referenced by other goroutines.
50+
func() {
51+
var mu sync.RWMutex
52+
mu.RLock()
53+
mu.Lock()
54+
},
55+
func() {
56+
var mu sync.Mutex
57+
mu.Lock()
58+
c := sync.NewCond(&mu)
59+
c.Wait()
60+
},
61+
}
2862

29-
snapshot()
30-
if !checker.failed {
31-
t.Errorf("didn't catch sleeping goroutine")
63+
func TestCheck(t *testing.T) {
64+
65+
// this works because the running goroutine is left running at the
66+
// start of the next test case - so the previous leaks don't affect the
67+
// check for the next one
68+
for i, fn := range leakyFuncs {
69+
checker := &testReporter{}
70+
snapshot := Check(checker)
71+
go fn()
72+
73+
snapshot()
74+
if !checker.failed {
75+
t.Errorf("didn't catch sleeping goroutine, test #%d", i)
76+
}
3277
}
3378
}

0 commit comments

Comments
 (0)