Skip to content

Commit 67e8d0c

Browse files
committed
http2: report an error if goroutines outlive serverTester tests
Change-Id: Icd2152b4bddacf12120be16c32c8c2d52d235fbd Reviewed-on: https://go-review.googlesource.com/c/net/+/589075 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
1 parent 5608279 commit 67e8d0c

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

http2/clientconn_test.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"io"
1515
"net/http"
1616
"reflect"
17-
"runtime"
1817
"sync/atomic"
1918
"testing"
2019
"time"
@@ -513,12 +512,7 @@ func newTestTransport(t *testing.T, opts ...func(*Transport)) *testTransport {
513512
if len(tt.ccs) > 0 {
514513
t.Fatalf("%v test ClientConns created, but not examined by test", len(tt.ccs))
515514
}
516-
if count := tt.group.Count(); count != 1 {
517-
buf := make([]byte, 16*1024)
518-
n := runtime.Stack(buf, true)
519-
t.Logf("stacks:\n%s", buf[:n])
520-
t.Fatalf("%v goroutines still running after test completed, expect 1", count)
521-
}
515+
tt.group.Close(t)
522516
})
523517

524518
return tt

http2/server_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ var optQuiet = func(server *http.Server) {
150150
func newServerTester(t testing.TB, handler http.HandlerFunc, opts ...interface{}) *serverTester {
151151
t.Helper()
152152
g := newSynctest(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
153+
t.Cleanup(func() {
154+
g.Close(t)
155+
})
156+
153157
h1server := &http.Server{}
154158
h2server := &Server{
155159
group: g,
@@ -191,6 +195,7 @@ func newServerTester(t testing.TB, handler http.HandlerFunc, opts ...interface{}
191195

192196
t.Cleanup(func() {
193197
st.Close()
198+
g.AdvanceTime(goAwayTimeout) // give server time to shut down
194199
})
195200

196201
connc := make(chan *serverConn)

http2/sync_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strconv"
1212
"strings"
1313
"sync"
14+
"testing"
1415
"time"
1516
)
1617

@@ -58,6 +59,16 @@ func (g *synctestGroup) Count() int {
5859
return count
5960
}
6061

62+
// Close calls t.Fatal if the group contains any running goroutines.
63+
func (g *synctestGroup) Close(t testing.TB) {
64+
if count := g.Count(); count != 1 {
65+
buf := make([]byte, 16*1024)
66+
n := runtime.Stack(buf, true)
67+
t.Logf("stacks:\n%s", buf[:n])
68+
t.Fatalf("%v goroutines still running after test completed, expect 1", count)
69+
}
70+
}
71+
6172
// Wait blocks until every goroutine in the group and their direct children are idle.
6273
func (g *synctestGroup) Wait() {
6374
for i := 0; ; i++ {

0 commit comments

Comments
 (0)