From 0fd2decbd5099215dfd9f22be78a2693f9c151ae Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 15 Jan 2025 22:37:00 -0800 Subject: [PATCH] http3: rename singleRoundTripper interface to clientConn (#4875) No functional change expected. --- http3/mock_clientconn_test.go | 120 ++++++++++++++++++++++++++ http3/mock_singleroundtripper_test.go | 120 -------------------------- http3/mockgen.go | 4 +- http3/transport.go | 26 +++--- http3/transport_test.go | 40 ++++----- 5 files changed, 155 insertions(+), 155 deletions(-) create mode 100644 http3/mock_clientconn_test.go delete mode 100644 http3/mock_singleroundtripper_test.go diff --git a/http3/mock_clientconn_test.go b/http3/mock_clientconn_test.go new file mode 100644 index 00000000..deb40ede --- /dev/null +++ b/http3/mock_clientconn_test.go @@ -0,0 +1,120 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/quic-go/quic-go/http3 (interfaces: TestClientConnInterface) +// +// Generated by this command: +// +// mockgen -typed -build_flags=-tags=gomock -mock_names=TestClientConnInterface=MockClientConn -package http3 -destination mock_clientconn_test.go github.com/quic-go/quic-go/http3 TestClientConnInterface +// + +// Package http3 is a generated GoMock package. +package http3 + +import ( + context "context" + http "net/http" + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockClientConn is a mock of TestClientConnInterface interface. +type MockClientConn struct { + ctrl *gomock.Controller + recorder *MockClientConnMockRecorder + isgomock struct{} +} + +// MockClientConnMockRecorder is the mock recorder for MockClientConn. +type MockClientConnMockRecorder struct { + mock *MockClientConn +} + +// NewMockClientConn creates a new mock instance. +func NewMockClientConn(ctrl *gomock.Controller) *MockClientConn { + mock := &MockClientConn{ctrl: ctrl} + mock.recorder = &MockClientConnMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockClientConn) EXPECT() *MockClientConnMockRecorder { + return m.recorder +} + +// OpenRequestStream mocks base method. +func (m *MockClientConn) OpenRequestStream(arg0 context.Context) (RequestStream, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "OpenRequestStream", arg0) + ret0, _ := ret[0].(RequestStream) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// OpenRequestStream indicates an expected call of OpenRequestStream. +func (mr *MockClientConnMockRecorder) OpenRequestStream(arg0 any) *MockClientConnOpenRequestStreamCall { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenRequestStream", reflect.TypeOf((*MockClientConn)(nil).OpenRequestStream), arg0) + return &MockClientConnOpenRequestStreamCall{Call: call} +} + +// MockClientConnOpenRequestStreamCall wrap *gomock.Call +type MockClientConnOpenRequestStreamCall struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *MockClientConnOpenRequestStreamCall) Return(arg0 RequestStream, arg1 error) *MockClientConnOpenRequestStreamCall { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *MockClientConnOpenRequestStreamCall) Do(f func(context.Context) (RequestStream, error)) *MockClientConnOpenRequestStreamCall { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *MockClientConnOpenRequestStreamCall) DoAndReturn(f func(context.Context) (RequestStream, error)) *MockClientConnOpenRequestStreamCall { + c.Call = c.Call.DoAndReturn(f) + return c +} + +// RoundTrip mocks base method. +func (m *MockClientConn) RoundTrip(arg0 *http.Request) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RoundTrip", arg0) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RoundTrip indicates an expected call of RoundTrip. +func (mr *MockClientConnMockRecorder) RoundTrip(arg0 any) *MockClientConnRoundTripCall { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RoundTrip", reflect.TypeOf((*MockClientConn)(nil).RoundTrip), arg0) + return &MockClientConnRoundTripCall{Call: call} +} + +// MockClientConnRoundTripCall wrap *gomock.Call +type MockClientConnRoundTripCall struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *MockClientConnRoundTripCall) Return(arg0 *http.Response, arg1 error) *MockClientConnRoundTripCall { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *MockClientConnRoundTripCall) Do(f func(*http.Request) (*http.Response, error)) *MockClientConnRoundTripCall { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *MockClientConnRoundTripCall) DoAndReturn(f func(*http.Request) (*http.Response, error)) *MockClientConnRoundTripCall { + c.Call = c.Call.DoAndReturn(f) + return c +} diff --git a/http3/mock_singleroundtripper_test.go b/http3/mock_singleroundtripper_test.go deleted file mode 100644 index 646088b8..00000000 --- a/http3/mock_singleroundtripper_test.go +++ /dev/null @@ -1,120 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/quic-go/quic-go/http3 (interfaces: SingleRoundTripper) -// -// Generated by this command: -// -// mockgen -typed -build_flags=-tags=gomock -package http3 -destination mock_singleroundtripper_test.go github.com/quic-go/quic-go/http3 SingleRoundTripper -// - -// Package http3 is a generated GoMock package. -package http3 - -import ( - context "context" - http "net/http" - reflect "reflect" - - gomock "go.uber.org/mock/gomock" -) - -// MockSingleRoundTripper is a mock of SingleRoundTripper interface. -type MockSingleRoundTripper struct { - ctrl *gomock.Controller - recorder *MockSingleRoundTripperMockRecorder - isgomock struct{} -} - -// MockSingleRoundTripperMockRecorder is the mock recorder for MockSingleRoundTripper. -type MockSingleRoundTripperMockRecorder struct { - mock *MockSingleRoundTripper -} - -// NewMockSingleRoundTripper creates a new mock instance. -func NewMockSingleRoundTripper(ctrl *gomock.Controller) *MockSingleRoundTripper { - mock := &MockSingleRoundTripper{ctrl: ctrl} - mock.recorder = &MockSingleRoundTripperMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockSingleRoundTripper) EXPECT() *MockSingleRoundTripperMockRecorder { - return m.recorder -} - -// OpenRequestStream mocks base method. -func (m *MockSingleRoundTripper) OpenRequestStream(arg0 context.Context) (RequestStream, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "OpenRequestStream", arg0) - ret0, _ := ret[0].(RequestStream) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// OpenRequestStream indicates an expected call of OpenRequestStream. -func (mr *MockSingleRoundTripperMockRecorder) OpenRequestStream(arg0 any) *MockSingleRoundTripperOpenRequestStreamCall { - mr.mock.ctrl.T.Helper() - call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "OpenRequestStream", reflect.TypeOf((*MockSingleRoundTripper)(nil).OpenRequestStream), arg0) - return &MockSingleRoundTripperOpenRequestStreamCall{Call: call} -} - -// MockSingleRoundTripperOpenRequestStreamCall wrap *gomock.Call -type MockSingleRoundTripperOpenRequestStreamCall struct { - *gomock.Call -} - -// Return rewrite *gomock.Call.Return -func (c *MockSingleRoundTripperOpenRequestStreamCall) Return(arg0 RequestStream, arg1 error) *MockSingleRoundTripperOpenRequestStreamCall { - c.Call = c.Call.Return(arg0, arg1) - return c -} - -// Do rewrite *gomock.Call.Do -func (c *MockSingleRoundTripperOpenRequestStreamCall) Do(f func(context.Context) (RequestStream, error)) *MockSingleRoundTripperOpenRequestStreamCall { - c.Call = c.Call.Do(f) - return c -} - -// DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *MockSingleRoundTripperOpenRequestStreamCall) DoAndReturn(f func(context.Context) (RequestStream, error)) *MockSingleRoundTripperOpenRequestStreamCall { - c.Call = c.Call.DoAndReturn(f) - return c -} - -// RoundTrip mocks base method. -func (m *MockSingleRoundTripper) RoundTrip(arg0 *http.Request) (*http.Response, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "RoundTrip", arg0) - ret0, _ := ret[0].(*http.Response) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// RoundTrip indicates an expected call of RoundTrip. -func (mr *MockSingleRoundTripperMockRecorder) RoundTrip(arg0 any) *MockSingleRoundTripperRoundTripCall { - mr.mock.ctrl.T.Helper() - call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RoundTrip", reflect.TypeOf((*MockSingleRoundTripper)(nil).RoundTrip), arg0) - return &MockSingleRoundTripperRoundTripCall{Call: call} -} - -// MockSingleRoundTripperRoundTripCall wrap *gomock.Call -type MockSingleRoundTripperRoundTripCall struct { - *gomock.Call -} - -// Return rewrite *gomock.Call.Return -func (c *MockSingleRoundTripperRoundTripCall) Return(arg0 *http.Response, arg1 error) *MockSingleRoundTripperRoundTripCall { - c.Call = c.Call.Return(arg0, arg1) - return c -} - -// Do rewrite *gomock.Call.Do -func (c *MockSingleRoundTripperRoundTripCall) Do(f func(*http.Request) (*http.Response, error)) *MockSingleRoundTripperRoundTripCall { - c.Call = c.Call.Do(f) - return c -} - -// DoAndReturn rewrite *gomock.Call.DoAndReturn -func (c *MockSingleRoundTripperRoundTripCall) DoAndReturn(f func(*http.Request) (*http.Response, error)) *MockSingleRoundTripperRoundTripCall { - c.Call = c.Call.DoAndReturn(f) - return c -} diff --git a/http3/mockgen.go b/http3/mockgen.go index 83a3974f..1d790e11 100644 --- a/http3/mockgen.go +++ b/http3/mockgen.go @@ -2,7 +2,7 @@ package http3 -//go:generate sh -c "go run go.uber.org/mock/mockgen -typed -build_flags=\"-tags=gomock\" -package http3 -destination mock_singleroundtripper_test.go github.com/quic-go/quic-go/http3 SingleRoundTripper" -type SingleRoundTripper = singleRoundTripper +//go:generate sh -c "go run go.uber.org/mock/mockgen -typed -build_flags=\"-tags=gomock\" -mock_names=TestClientConnInterface=MockClientConn -package http3 -destination mock_clientconn_test.go github.com/quic-go/quic-go/http3 TestClientConnInterface" +type TestClientConnInterface = clientConn //go:generate sh -c "go run go.uber.org/mock/mockgen -typed -package http3 -destination mock_quic_early_listener_test.go github.com/quic-go/quic-go/http3 QUICEarlyListener" diff --git a/http3/transport.go b/http3/transport.go index cbe99e37..b90f1190 100644 --- a/http3/transport.go +++ b/http3/transport.go @@ -37,17 +37,17 @@ type RoundTripOpt struct { OnlyCachedConn bool } -type singleRoundTripper interface { +type clientConn interface { OpenRequestStream(context.Context) (RequestStream, error) RoundTrip(*http.Request) (*http.Response, error) } type roundTripperWithCount struct { - cancel context.CancelFunc - dialing chan struct{} // closed as soon as quic.Dial(Early) returned - dialErr error - conn quic.EarlyConnection - rt singleRoundTripper + cancel context.CancelFunc + dialing chan struct{} // closed as soon as quic.Dial(Early) returned + dialErr error + conn quic.EarlyConnection + clientConn clientConn useCount atomic.Int64 } @@ -107,7 +107,7 @@ type Transport struct { initOnce sync.Once initErr error - newClient func(quic.EarlyConnection) singleRoundTripper + newClientConn func(quic.EarlyConnection) clientConn clients map[string]*roundTripperWithCount transport *quic.Transport @@ -125,8 +125,8 @@ type RoundTripper = Transport var ErrNoCachedConn = errors.New("http3: no cached connection was available") func (t *Transport) init() error { - if t.newClient == nil { - t.newClient = func(conn quic.EarlyConnection) singleRoundTripper { + if t.newClientConn == nil { + t.newClientConn = func(conn quic.EarlyConnection) clientConn { return newClientConn( conn, t.EnableDatagrams, @@ -223,7 +223,7 @@ func (t *Transport) roundTripOpt(req *http.Request, opt RoundTripOpt) (*http.Res } traceGotConn(trace, cl.conn, isReused) defer cl.useCount.Add(-1) - rsp, err := cl.rt.RoundTrip(req) + rsp, err := cl.clientConn.RoundTrip(req) if err != nil { // request aborted due to context cancellation select { @@ -285,7 +285,7 @@ func (t *Transport) getClient(ctx context.Context, hostname string, onlyCached b return } cl.conn = conn - cl.rt = rt + cl.clientConn = rt }() t.clients[hostname] = cl } @@ -306,7 +306,7 @@ func (t *Transport) getClient(ctx context.Context, hostname string, onlyCached b return cl, isReused, nil } -func (t *Transport) dial(ctx context.Context, hostname string) (quic.EarlyConnection, singleRoundTripper, error) { +func (t *Transport) dial(ctx context.Context, hostname string) (quic.EarlyConnection, clientConn, error) { var tlsConf *tls.Config if t.TLSClientConfig == nil { tlsConf = &tls.Config{} @@ -356,7 +356,7 @@ func (t *Transport) dial(ctx context.Context, hostname string) (quic.EarlyConnec if err != nil { return nil, nil, err } - return conn, t.newClient(conn), nil + return conn, t.newClientConn(conn), nil } func (t *Transport) resolveUDPAddr(ctx context.Context, network, addr string) (*net.UDPAddr, error) { diff --git a/http3/transport_test.go b/http3/transport_test.go index f7c8fca8..83c6273a 100644 --- a/http3/transport_test.go +++ b/http3/transport_test.go @@ -217,7 +217,7 @@ func TestTransportMultipleQUICVersions(t *testing.T) { func TestTransportConnectionReuse(t *testing.T) { mockCtrl := gomock.NewController(t) - cl := NewMockSingleRoundTripper(mockCtrl) + cl := NewMockClientConn(mockCtrl) conn := mockquic.NewMockEarlyConnection(mockCtrl) handshakeChan := make(chan struct{}) close(handshakeChan) @@ -228,7 +228,7 @@ func TestTransportConnectionReuse(t *testing.T) { dialCount++ return conn, nil }, - newClient: func(quic.EarlyConnection) singleRoundTripper { return cl }, + newClientConn: func(quic.EarlyConnection) clientConn { return cl }, } req1 := mustNewRequest("GET", "https://quic-go.net/file1.html", nil) @@ -279,7 +279,7 @@ func TestTransportConnectionRedial(t *testing.T) { func testTransportConnectionRedial(t *testing.T, connClosed bool, roundtripErr, expectedErr error) { mockCtrl := gomock.NewController(t) - cl := NewMockSingleRoundTripper(mockCtrl) + cl := NewMockClientConn(mockCtrl) conn := mockquic.NewMockEarlyConnection(mockCtrl) handshakeChan := make(chan struct{}) close(handshakeChan) @@ -290,7 +290,7 @@ func testTransportConnectionRedial(t *testing.T, connClosed bool, roundtripErr, dialCount++ return conn, nil }, - newClient: func(quic.EarlyConnection) singleRoundTripper { return cl }, + newClientConn: func(quic.EarlyConnection) clientConn { return cl }, } // the first request succeeds @@ -338,7 +338,7 @@ func testTransportConnectionRedial(t *testing.T, connClosed bool, roundtripErr, func TestTransportRequestContextCancellation(t *testing.T) { mockCtrl := gomock.NewController(t) - cl := NewMockSingleRoundTripper(mockCtrl) + cl := NewMockClientConn(mockCtrl) conn := mockquic.NewMockEarlyConnection(mockCtrl) handshakeChan := make(chan struct{}) close(handshakeChan) @@ -349,7 +349,7 @@ func TestTransportRequestContextCancellation(t *testing.T) { dialCount++ return conn, nil }, - newClient: func(quic.EarlyConnection) singleRoundTripper { return cl }, + newClientConn: func(quic.EarlyConnection) clientConn { return cl }, } // the first request succeeds @@ -385,7 +385,7 @@ func TestTransportRequestContextCancellation(t *testing.T) { func TestTransportConnetionRedialHandshakeError(t *testing.T) { mockCtrl := gomock.NewController(t) - cl := NewMockSingleRoundTripper(mockCtrl) + cl := NewMockClientConn(mockCtrl) conn := mockquic.NewMockEarlyConnection(mockCtrl) handshakeChan := make(chan struct{}) close(handshakeChan) @@ -400,7 +400,7 @@ func TestTransportConnetionRedialHandshakeError(t *testing.T) { } return conn, nil }, - newClient: func(quic.EarlyConnection) singleRoundTripper { return cl }, + newClientConn: func(quic.EarlyConnection) clientConn { return cl }, } req1 := mustNewRequest("GET", "https://quic-go.net/file1.html", nil) @@ -423,8 +423,8 @@ func TestTransportCloseEstablishedConnections(t *testing.T) { Dial: func(context.Context, string, *tls.Config, *quic.Config) (quic.EarlyConnection, error) { return conn, nil }, - newClient: func(quic.EarlyConnection) singleRoundTripper { - cl := NewMockSingleRoundTripper(mockCtrl) + newClientConn: func(quic.EarlyConnection) clientConn { + cl := NewMockClientConn(mockCtrl) cl.EXPECT().RoundTrip(gomock.Any()).Return(&http.Response{}, nil) return cl }, @@ -476,6 +476,7 @@ func TestTransportCloseIdleConnections(t *testing.T) { mockCtrl := gomock.NewController(t) conn1 := mockquic.NewMockEarlyConnection(mockCtrl) conn2 := mockquic.NewMockEarlyConnection(mockCtrl) + roundTripCalled := make(chan struct{}) tr := &Transport{ Dial: func(_ context.Context, hostname string, _ *tls.Config, _ *quic.Config) (quic.EarlyConnection, error) { switch hostname { @@ -488,6 +489,15 @@ func TestTransportCloseIdleConnections(t *testing.T) { return nil, errors.New("unexpected hostname") } }, + newClientConn: func(quic.EarlyConnection) clientConn { + cl := NewMockClientConn(mockCtrl) + cl.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(func(r *http.Request) (*http.Response, error) { + roundTripCalled <- struct{}{} + <-r.Context().Done() + return nil, nil + }) + return cl + }, } req1 := mustNewRequest(http.MethodGet, "https://site1.com", nil) req2 := mustNewRequest(http.MethodGet, "https://site2.com", nil) @@ -496,17 +506,7 @@ func TestTransportCloseIdleConnections(t *testing.T) { ctx2, cancel2 := context.WithCancel(context.Background()) req1 = req1.WithContext(ctx1) req2 = req2.WithContext(ctx2) - roundTripCalled := make(chan struct{}) reqFinished := make(chan struct{}) - tr.newClient = func(quic.EarlyConnection) singleRoundTripper { - cl := NewMockSingleRoundTripper(mockCtrl) - cl.EXPECT().RoundTrip(gomock.Any()).DoAndReturn(func(r *http.Request) (*http.Response, error) { - roundTripCalled <- struct{}{} - <-r.Context().Done() - return nil, nil - }) - return cl - } go func() { tr.RoundTrip(req1) reqFinished <- struct{}{}