Skip to content

Commit 3eb9c59

Browse files
committed
fix
1 parent f452b03 commit 3eb9c59

File tree

5 files changed

+22
-40
lines changed

5 files changed

+22
-40
lines changed

internal/xtest/leak.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,20 @@ import (
55
"runtime"
66
"strings"
77
"testing"
8-
"time"
98
)
109

11-
func checkGoroutinesLeak(onLeak func(goroutines []string)) {
12-
var (
13-
bb = make([]byte, 2<<32)
14-
currentGoroutine string
15-
)
16-
17-
time.Sleep(time.Millisecond)
18-
19-
if n := runtime.Stack(bb, false); n < len(bb) {
20-
currentGoroutine = string(regexp.MustCompile(`^goroutine \d+ `).Find(bb[:n]))
21-
}
22-
23-
if n := runtime.Stack(bb, true); n < len(bb) {
24-
bb = bb[:n]
10+
func goroutineStack(all bool) []byte {
11+
for i := 1 << 16; ; i *= 2 {
12+
bb := make([]byte, i)
13+
if n := runtime.Stack(bb, all); n < i {
14+
return bb[:n]
15+
}
2516
}
17+
}
2618

27-
goroutines := strings.Split(string(bb), "\n\n")
19+
func checkGoroutinesLeak(onLeak func(goroutines []string)) {
20+
currentGoroutine := string(regexp.MustCompile(`^goroutine \d+ `).Find(goroutineStack(false)))
21+
goroutines := strings.Split(string(goroutineStack(true)), "\n\n")
2822
unexpectedGoroutines := make([]string, 0, len(goroutines))
2923

3024
for _, g := range goroutines {
@@ -67,8 +61,11 @@ func checkGoroutinesLeak(onLeak func(goroutines []string)) {
6761

6862
func CheckGoroutinesLeak(tb testing.TB) {
6963
tb.Helper()
70-
checkGoroutinesLeak(func(stacks []string) {
64+
checkGoroutinesLeak(func(goroutines []string) {
7165
tb.Helper()
72-
tb.Errorf("found %d unexpected goroutines:\n%s", len(stacks), strings.Join(stacks, "\n"))
66+
tb.Errorf("found %d unexpected goroutines:\n%s",
67+
len(goroutines),
68+
strings.Join(goroutines, "\n"),
69+
)
7370
})
7471
}

internal/xtest/leak_test.go

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

33
import (
44
"testing"
5-
"time"
65

76
"github.com/stretchr/testify/require"
87
)
@@ -13,29 +12,29 @@ func TestCheckGoroutinesLeak(t *testing.T) {
1312
ch := make(chan struct{})
1413
require.Panics(t, func() {
1514
defer checkGoroutinesLeak(func(goroutines []string) {
16-
panic("panic")
15+
panic(goroutines)
1716
})
1817
go func() {
1918
<-ch
2019
}()
2120
})
2221
close(ch)
23-
}, StopAfter(13*time.Second))
22+
})
2423
})
2524
t.Run("NoLeak", func(t *testing.T) {
2625
TestManyTimes(t, func(t testing.TB) {
27-
ch := make(chan struct{})
2826
require.NotPanics(t, func() {
2927
defer checkGoroutinesLeak(func(goroutines []string) {
30-
panic("panic")
28+
panic(goroutines)
3129
})
30+
ch := make(chan struct{})
3231
defer func() {
3332
<-ch
3433
}()
3534
go func() {
3635
close(ch)
3736
}()
3837
})
39-
}, StopAfter(13*time.Second))
38+
})
4039
})
4140
}

tests/integration/basic_example_database_sql_bindings_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
func TestBasicExampleDatabaseSqlBindings(t *testing.T) {
29-
defer simpleDetectGoroutineLeak(t)
29+
defer xtest.CheckGoroutinesLeak(t)
3030

3131
folder := t.Name()
3232

tests/integration/basic_example_database_sql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
)
2828

2929
func TestBasicExampleDatabaseSql(t *testing.T) {
30-
defer simpleDetectGoroutineLeak(t)
30+
defer xtest.CheckGoroutinesLeak(t)
3131

3232
folder := t.Name()
3333

tests/integration/helpers_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"os"
1212
"path"
13-
"runtime"
1413
"strings"
1514
"testing"
1615
"text/template"
@@ -469,16 +468,3 @@ func driverEngine(db *sql.DB) (engine xsql.Engine) {
469468

470469
return engine
471470
}
472-
473-
func simpleDetectGoroutineLeak(t *testing.T) {
474-
// 1) testing.go => main.main()
475-
// 2) current test
476-
const expectedGoroutinesCount = 2
477-
if num := runtime.NumGoroutine(); num > expectedGoroutinesCount {
478-
bb := make([]byte, 2<<32)
479-
if n := runtime.Stack(bb, true); n < len(bb) {
480-
bb = bb[:n]
481-
}
482-
t.Error(fmt.Sprintf("unexpected goroutines:\n%s\n", string(bb[runtime.Stack(bb, false)+1:])))
483-
}
484-
}

0 commit comments

Comments
 (0)