Skip to content

Commit

Permalink
http2: report an error if goroutines outlive serverTester tests
Browse files Browse the repository at this point in the history
Change-Id: Icd2152b4bddacf12120be16c32c8c2d52d235fbd
Reviewed-on: https://go-review.googlesource.com/c/net/+/589075
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
  • Loading branch information
neild committed May 30, 2024
1 parent 5608279 commit 67e8d0c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
8 changes: 1 addition & 7 deletions http2/clientconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"io"
"net/http"
"reflect"
"runtime"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -513,12 +512,7 @@ func newTestTransport(t *testing.T, opts ...func(*Transport)) *testTransport {
if len(tt.ccs) > 0 {
t.Fatalf("%v test ClientConns created, but not examined by test", len(tt.ccs))
}
if count := tt.group.Count(); count != 1 {
buf := make([]byte, 16*1024)
n := runtime.Stack(buf, true)
t.Logf("stacks:\n%s", buf[:n])
t.Fatalf("%v goroutines still running after test completed, expect 1", count)
}
tt.group.Close(t)
})

return tt
Expand Down
5 changes: 5 additions & 0 deletions http2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ var optQuiet = func(server *http.Server) {
func newServerTester(t testing.TB, handler http.HandlerFunc, opts ...interface{}) *serverTester {
t.Helper()
g := newSynctest(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
t.Cleanup(func() {
g.Close(t)
})

h1server := &http.Server{}
h2server := &Server{
group: g,
Expand Down Expand Up @@ -191,6 +195,7 @@ func newServerTester(t testing.TB, handler http.HandlerFunc, opts ...interface{}

t.Cleanup(func() {
st.Close()
g.AdvanceTime(goAwayTimeout) // give server time to shut down
})

connc := make(chan *serverConn)
Expand Down
11 changes: 11 additions & 0 deletions http2/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"sync"
"testing"
"time"
)

Expand Down Expand Up @@ -58,6 +59,16 @@ func (g *synctestGroup) Count() int {
return count
}

// Close calls t.Fatal if the group contains any running goroutines.
func (g *synctestGroup) Close(t testing.TB) {
if count := g.Count(); count != 1 {
buf := make([]byte, 16*1024)
n := runtime.Stack(buf, true)
t.Logf("stacks:\n%s", buf[:n])
t.Fatalf("%v goroutines still running after test completed, expect 1", count)
}
}

// Wait blocks until every goroutine in the group and their direct children are idle.
func (g *synctestGroup) Wait() {
for i := 0; ; i++ {
Expand Down

0 comments on commit 67e8d0c

Please sign in to comment.