Skip to content

Commit 8d297f1

Browse files
neildgopherbot
authored andcommitted
http2: Move most tests from the http2 package to the http2_test package.
This change makes it easier to move x/net/http2 into std. Moving the http2 package into std and importing it from net/http (rather than bundling it as net/http/h2_bundle.go) requires removing the http2->net/http dependency. Moving tests into the http2_test package allows them to continue importing net/http without creating a cycle. Change-Id: If0799a94a6d2c90f02d7f391e352e14e6a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/749280 Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Nicholas Husin <husin@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Nicholas Husin <nsh@golang.org>
1 parent 3eb9327 commit 8d297f1

14 files changed

Lines changed: 863 additions & 681 deletions

http2/clientconn_test.go

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Infrastructure for testing ClientConn.RoundTrip.
66
// Put actual tests in transport_test.go.
77

8-
package http2
8+
package http2_test
99

1010
import (
1111
"bytes"
@@ -20,6 +20,7 @@ import (
2020
"testing/synctest"
2121
"time"
2222

23+
. "golang.org/x/net/http2"
2324
"golang.org/x/net/http2/hpack"
2425
"golang.org/x/net/internal/gate"
2526
)
@@ -108,29 +109,32 @@ type testClientConn struct {
108109
netconn *synctestNetConn
109110
}
110111

111-
func newTestClientConnFromClientConn(t testing.TB, cc *ClientConn) *testClientConn {
112+
func newTestClientConnFromClientConn(t testing.TB, tr *Transport, cc *ClientConn) *testClientConn {
112113
tc := &testClientConn{
113114
t: t,
114-
tr: cc.t,
115+
tr: tr,
115116
cc: cc,
116117
}
117118

118119
// srv is the side controlled by the test.
119120
var srv *synctestNetConn
120-
if cc.tconn == nil {
121+
if tconn := cc.TestNetConn(); tconn == nil {
121122
// If cc.tconn is nil, we're being called with a new conn created by the
122123
// Transport's client pool. This path skips dialing the server, and we
123124
// create a test connection pair here.
124-
cc.tconn, srv = synctestNetPipe()
125+
var cli *synctestNetConn
126+
cli, srv = synctestNetPipe()
127+
cc.TestSetNetConn(cli)
125128
} else {
126129
// If cc.tconn is non-nil, we're in a test which provides a conn to the
127130
// Transport via a TLSNextProto hook. Extract the test connection pair.
128-
if tc, ok := cc.tconn.(*tls.Conn); ok {
131+
if tc, ok := tconn.(*tls.Conn); ok {
129132
// Unwrap any *tls.Conn to the underlying net.Conn,
130133
// to avoid dealing with encryption in tests.
131-
cc.tconn = tc.NetConn()
134+
tconn = tc.NetConn()
135+
cc.TestSetNetConn(tconn)
132136
}
133-
srv = cc.tconn.(*synctestNetConn).peer
137+
srv = tconn.(*synctestNetConn).peer
134138
}
135139

136140
srv.SetReadDeadline(time.Now())
@@ -141,7 +145,7 @@ func newTestClientConnFromClientConn(t testing.TB, cc *ClientConn) *testClientCo
141145
tc.testConnFramer = testConnFramer{
142146
t: t,
143147
fr: tc.fr,
144-
dec: hpack.NewDecoder(initialHeaderTableSize, nil),
148+
dec: hpack.NewDecoder(InitialHeaderTableSize, nil),
145149
}
146150
tc.fr.SetMaxReadFrameSize(10 << 20)
147151
t.Cleanup(func() {
@@ -154,12 +158,12 @@ func newTestClientConnFromClientConn(t testing.TB, cc *ClientConn) *testClientCo
154158
func (tc *testClientConn) readClientPreface() {
155159
tc.t.Helper()
156160
// Read the client's HTTP/2 preface, sent prior to any HTTP/2 frames.
157-
buf := make([]byte, len(clientPreface))
161+
buf := make([]byte, len(ClientPreface))
158162
if _, err := io.ReadFull(tc.netconn, buf); err != nil {
159163
tc.t.Fatalf("reading preface: %v", err)
160164
}
161-
if !bytes.Equal(buf, clientPreface) {
162-
tc.t.Fatalf("client preface: %q, want %q", buf, clientPreface)
165+
if !bytes.Equal(buf, []byte(ClientPreface)) {
166+
tc.t.Fatalf("client preface: %q, want %q", buf, ClientPreface)
163167
}
164168
}
165169

@@ -168,7 +172,7 @@ func newTestClientConn(t testing.TB, opts ...any) *testClientConn {
168172

169173
tt := newTestTransport(t, opts...)
170174
const singleUse = false
171-
_, err := tt.tr.newClientConn(nil, singleUse, nil)
175+
_, err := tt.tr.TestNewClientConn(nil, singleUse, nil)
172176
if err != nil {
173177
t.Fatalf("newClientConn: %v", err)
174178
}
@@ -303,8 +307,8 @@ func (tc *testClientConn) roundTrip(req *http.Request) *testRoundTrip {
303307
tc.roundtrips = append(tc.roundtrips, rt)
304308
go func() {
305309
defer close(rt.donec)
306-
rt.resp, rt.respErr = tc.cc.roundTrip(req, func(cs *clientStream) {
307-
rt.id.Store(cs.ID)
310+
rt.resp, rt.respErr = tc.cc.TestRoundTrip(req, func(streamID uint32) {
311+
rt.id.Store(streamID)
308312
})
309313
}()
310314
synctest.Wait()
@@ -348,17 +352,11 @@ func (tc *testClientConn) makeHeaderBlockFragment(s ...string) []byte {
348352
// inflowWindow returns the amount of inbound flow control available for a stream,
349353
// or for the connection if streamID is 0.
350354
func (tc *testClientConn) inflowWindow(streamID uint32) int32 {
351-
tc.cc.mu.Lock()
352-
defer tc.cc.mu.Unlock()
353-
if streamID == 0 {
354-
return tc.cc.inflow.avail + tc.cc.inflow.unsent
355-
}
356-
cs := tc.cc.streams[streamID]
357-
if cs == nil {
358-
tc.t.Errorf("no stream with id %v", streamID)
359-
return -1
355+
w, err := tc.cc.TestInflowWindow(streamID)
356+
if err != nil {
357+
tc.t.Error(err)
360358
}
361-
return cs.inflow.avail + cs.inflow.unsent
359+
return w
362360
}
363361

364362
// testRoundTrip manages a RoundTrip in progress.
@@ -508,10 +506,7 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
508506
for _, o := range opts {
509507
switch o := o.(type) {
510508
case func(*http.Transport):
511-
if tr.t1 == nil {
512-
tr.t1 = &http.Transport{}
513-
}
514-
o(tr.t1)
509+
o(tr.TestTransport())
515510
case func(*Transport):
516511
o(tr)
517512
case *Transport:
@@ -520,12 +515,10 @@ func newTestTransport(t testing.TB, opts ...any) *testTransport {
520515
}
521516
tt.tr = tr
522517

523-
tr.transportTestHooks = &transportTestHooks{
524-
newclientconn: func(cc *ClientConn) {
525-
tc := newTestClientConnFromClientConn(t, cc)
526-
tt.ccs = append(tt.ccs, tc)
527-
},
528-
}
518+
tr.TestSetNewClientConnHook(func(cc *ClientConn) {
519+
tc := newTestClientConnFromClientConn(t, tr, cc)
520+
tt.ccs = append(tt.ccs, tc)
521+
})
529522

530523
t.Cleanup(func() {
531524
synctest.Wait()

http2/config_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package http2
5+
package http2_test
66

77
import (
88
"net/http"
99
"testing"
1010
"time"
11+
12+
. "golang.org/x/net/http2"
1113
)
1214

1315
func TestConfigServerSettings(t *testing.T) { synctestTest(t, testConfigServerSettings) }

http2/connframes_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package http2
5+
package http2_test
66

77
import (
88
"bytes"
@@ -13,6 +13,7 @@ import (
1313
"slices"
1414
"testing"
1515

16+
. "golang.org/x/net/http2"
1617
"golang.org/x/net/http2/hpack"
1718
)
1819

@@ -257,7 +258,7 @@ func (tf *testConnFramer) wantRSTStream(streamID uint32, code ErrCode) {
257258
tf.t.Helper()
258259
fr := readFrame[*RSTStreamFrame](tf.t, tf)
259260
if fr.StreamID != streamID || fr.ErrCode != code {
260-
tf.t.Fatalf("got %v, want RST_STREAM StreamID=%v, code=%v", summarizeFrame(fr), streamID, code)
261+
tf.t.Fatalf("got %v, want RST_STREAM StreamID=%v, code=%v", SummarizeFrame(fr), streamID, code)
261262
}
262263
}
263264

@@ -291,7 +292,7 @@ func (tf *testConnFramer) wantGoAway(maxStreamID uint32, code ErrCode) {
291292
tf.t.Helper()
292293
fr := readFrame[*GoAwayFrame](tf.t, tf)
293294
if fr.LastStreamID != maxStreamID || fr.ErrCode != code {
294-
tf.t.Fatalf("got %v, want GOAWAY LastStreamID=%v, code=%v", summarizeFrame(fr), maxStreamID, code)
295+
tf.t.Fatalf("got %v, want GOAWAY LastStreamID=%v, code=%v", SummarizeFrame(fr), maxStreamID, code)
295296
}
296297
}
297298

0 commit comments

Comments
 (0)