2020-10-27 22:27:15 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-27 22:27:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/net/http2"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2020-10-27 22:27:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testTransport = http2.Transport{}
|
|
|
|
)
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
func newTestHTTP2Connection() (*HTTP2Connection, net.Conn) {
|
2021-10-19 19:01:17 +00:00
|
|
|
edgeConn, cfdConn := net.Pipe()
|
2020-10-27 22:27:15 +00:00
|
|
|
var connIndex = uint8(0)
|
2021-07-16 15:14:37 +00:00
|
|
|
log := zerolog.Nop()
|
2021-08-17 14:30:02 +00:00
|
|
|
obs := NewObserver(&log, &log, false)
|
|
|
|
controlStream := NewControlStream(
|
|
|
|
obs,
|
|
|
|
mockConnectedFuse{},
|
|
|
|
&NamedTunnelConfig{},
|
|
|
|
connIndex,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
1*time.Second,
|
|
|
|
)
|
2020-10-27 22:27:15 +00:00
|
|
|
return NewHTTP2Connection(
|
2021-10-19 19:01:17 +00:00
|
|
|
cfdConn,
|
|
|
|
// OriginProxy is set in testConfig
|
2020-10-27 22:27:15 +00:00
|
|
|
testConfig,
|
|
|
|
&pogs.ConnectionOptions{},
|
2021-08-17 14:30:02 +00:00
|
|
|
obs,
|
2020-10-27 22:27:15 +00:00
|
|
|
connIndex,
|
2021-08-17 14:30:02 +00:00
|
|
|
controlStream,
|
2021-07-16 15:14:37 +00:00
|
|
|
&log,
|
2020-10-27 22:27:15 +00:00
|
|
|
), edgeConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServeHTTP(t *testing.T) {
|
|
|
|
tests := []testRequest{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
endpoint: "ok",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: []byte(http.StatusText(http.StatusOK)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "large_file",
|
|
|
|
endpoint: "large_file",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: testLargeResp,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Bad request",
|
|
|
|
endpoint: "400",
|
|
|
|
expectedStatus: http.StatusBadRequest,
|
|
|
|
expectedBody: []byte(http.StatusText(http.StatusBadRequest)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Internal server error",
|
|
|
|
endpoint: "500",
|
|
|
|
expectedStatus: http.StatusInternalServerError,
|
|
|
|
expectedBody: []byte(http.StatusText(http.StatusInternalServerError)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Proxy error",
|
|
|
|
endpoint: "error",
|
|
|
|
expectedStatus: http.StatusBadGateway,
|
|
|
|
expectedBody: nil,
|
|
|
|
isProxyError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
endpoint := fmt.Sprintf("http://localhost:8080/%s", test.endpoint)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, test.expectedStatus, resp.StatusCode)
|
|
|
|
if test.expectedBody != nil {
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, test.expectedBody, respBody)
|
|
|
|
}
|
|
|
|
if test.isProxyError {
|
2021-03-26 04:04:56 +00:00
|
|
|
require.Equal(t, responseMetaHeaderCfd, resp.Header.Get(ResponseMetaHeader))
|
2020-10-27 22:27:15 +00:00
|
|
|
} else {
|
2021-03-26 04:04:56 +00:00
|
|
|
require.Equal(t, responseMetaHeaderOrigin, resp.Header.Get(ResponseMetaHeader))
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockNamedTunnelRPCClient struct {
|
2021-03-04 18:45:39 +00:00
|
|
|
shouldFail error
|
2020-10-27 22:27:15 +00:00
|
|
|
registered chan struct{}
|
|
|
|
unregistered chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc mockNamedTunnelRPCClient) RegisterConnection(
|
|
|
|
c context.Context,
|
|
|
|
config *NamedTunnelConfig,
|
|
|
|
options *tunnelpogs.ConnectionOptions,
|
|
|
|
connIndex uint8,
|
|
|
|
observer *Observer,
|
|
|
|
) error {
|
2021-03-04 18:45:39 +00:00
|
|
|
if mc.shouldFail != nil {
|
|
|
|
return mc.shouldFail
|
|
|
|
}
|
2020-10-27 22:27:15 +00:00
|
|
|
close(mc.registered)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc mockNamedTunnelRPCClient) GracefulShutdown(ctx context.Context, gracePeriod time.Duration) {
|
|
|
|
close(mc.unregistered)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mockNamedTunnelRPCClient) Close() {}
|
|
|
|
|
|
|
|
type mockRPCClientFactory struct {
|
2021-03-04 18:45:39 +00:00
|
|
|
shouldFail error
|
2020-10-27 22:27:15 +00:00
|
|
|
registered chan struct{}
|
|
|
|
unregistered chan struct{}
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
func (mf *mockRPCClientFactory) newMockRPCClient(context.Context, io.ReadWriteCloser, *zerolog.Logger) NamedTunnelRPCClient {
|
2020-10-27 22:27:15 +00:00
|
|
|
return mockNamedTunnelRPCClient{
|
2021-03-04 18:45:39 +00:00
|
|
|
shouldFail: mf.shouldFail,
|
2020-10-27 22:27:15 +00:00
|
|
|
registered: mf.registered,
|
|
|
|
unregistered: mf.unregistered,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type wsRespWriter struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
readPipe *io.PipeReader
|
|
|
|
writePipe *io.PipeWriter
|
2021-10-19 19:01:17 +00:00
|
|
|
closed bool
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWSRespWriter() *wsRespWriter {
|
|
|
|
readPipe, writePipe := io.Pipe()
|
|
|
|
return &wsRespWriter{
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
readPipe,
|
|
|
|
writePipe,
|
2021-10-19 19:01:17 +00:00
|
|
|
false,
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
type nowriter struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nowriter) Write(p []byte) (int, error) {
|
|
|
|
return 0, fmt.Errorf("Writer not implemented")
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func (w *wsRespWriter) RespBody() io.ReadWriter {
|
|
|
|
return nowriter{w.readPipe}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wsRespWriter) Write(data []byte) (n int, err error) {
|
2021-10-19 19:01:17 +00:00
|
|
|
if w.closed {
|
|
|
|
// Simulate writing to http2 ResponseWriter after ServeHTTP has returned
|
|
|
|
panic("Write to closed ResponseWriter")
|
|
|
|
}
|
2020-10-27 22:27:15 +00:00
|
|
|
return w.writePipe.Write(data)
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
func (w *wsRespWriter) close() {
|
|
|
|
w.closed = true
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func TestServeWS(t *testing.T) {
|
|
|
|
http2Conn, _ := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
respWriter := newWSRespWriter()
|
|
|
|
readPipe, writePipe := io.Pipe()
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/ws/echo", readPipe)
|
2020-10-27 22:27:15 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-26 04:04:56 +00:00
|
|
|
req.Header.Set(InternalUpgradeHeader, WebsocketUpgrade)
|
2020-10-27 22:27:15 +00:00
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
serveDone := make(chan struct{})
|
2020-10-27 22:27:15 +00:00
|
|
|
go func() {
|
2021-10-19 19:01:17 +00:00
|
|
|
defer close(serveDone)
|
2020-10-27 22:27:15 +00:00
|
|
|
http2Conn.ServeHTTP(respWriter, req)
|
2021-10-19 19:01:17 +00:00
|
|
|
respWriter.close()
|
2020-10-27 22:27:15 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
data := []byte("test websocket")
|
2021-10-19 19:01:17 +00:00
|
|
|
err = wsutil.WriteClientBinary(writePipe, data)
|
2020-10-27 22:27:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
respBody, err := wsutil.ReadServerBinary(respWriter.RespBody())
|
2020-10-27 22:27:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, data, respBody, fmt.Sprintf("Expect %s, got %s", string(data), string(respBody)))
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
resp := respWriter.Result()
|
|
|
|
// http2RespWriter should rewrite status 101 to 200
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
2021-03-26 04:04:56 +00:00
|
|
|
require.Equal(t, responseMetaHeaderOrigin, resp.Header.Get(ResponseMetaHeader))
|
2020-10-27 22:27:15 +00:00
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
<-serveDone
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestNoWriteAfterServeHTTPReturns is a regression test of https://jira.cfops.it/browse/TUN-5184
|
|
|
|
// to make sure we don't write to the ResponseWriter after the ServeHTTP method returns
|
|
|
|
func TestNoWriteAfterServeHTTPReturns(t *testing.T) {
|
|
|
|
cfdHTTP2Conn, edgeTCPConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
serverDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(serverDone)
|
|
|
|
cfdHTTP2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
edgeTransport := http2.Transport{}
|
|
|
|
edgeHTTP2Conn, err := edgeTransport.NewClientConn(edgeTCPConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
message := []byte(t.Name())
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
readPipe, writePipe := io.Pipe()
|
|
|
|
reqCtx, reqCancel := context.WithCancel(ctx)
|
|
|
|
req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, "http://localhost:8080/ws/flaky", readPipe)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set(InternalUpgradeHeader, WebsocketUpgrade)
|
|
|
|
|
|
|
|
resp, err := edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// http2RespWriter should rewrite status 101 to 200
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-reqCtx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
_ = wsutil.WriteClientBinary(writePipe, message)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
reqCancel()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
wg.Wait()
|
2021-10-19 19:01:17 +00:00
|
|
|
cancel()
|
|
|
|
<-serverDone
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServeControlStream(t *testing.T) {
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
rpcClientFactory := mockRPCClientFactory{
|
|
|
|
registered: make(chan struct{}),
|
|
|
|
unregistered: make(chan struct{}),
|
|
|
|
}
|
2021-08-17 14:30:02 +00:00
|
|
|
|
|
|
|
obs := NewObserver(&log, &log, false)
|
|
|
|
controlStream := NewControlStream(
|
|
|
|
obs,
|
|
|
|
mockConnectedFuse{},
|
|
|
|
&NamedTunnelConfig{},
|
|
|
|
1,
|
|
|
|
rpcClientFactory.newMockRPCClient,
|
|
|
|
nil,
|
|
|
|
1*time.Second,
|
|
|
|
)
|
|
|
|
http2Conn.controlStreamHandler = controlStream
|
2020-10-27 22:27:15 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/", nil)
|
|
|
|
require.NoError(t, err)
|
2021-03-26 04:04:56 +00:00
|
|
|
req.Header.Set(InternalUpgradeHeader, ControlStreamUpgrade)
|
2020-10-27 22:27:15 +00:00
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-rpcClientFactory.registered
|
|
|
|
cancel()
|
|
|
|
<-rpcClientFactory.unregistered
|
2021-01-20 19:41:09 +00:00
|
|
|
assert.False(t, http2Conn.stoppedGracefully)
|
2020-10-27 22:27:15 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:45:39 +00:00
|
|
|
func TestFailRegistration(t *testing.T) {
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
rpcClientFactory := mockRPCClientFactory{
|
|
|
|
shouldFail: errDuplicationConnection,
|
|
|
|
registered: make(chan struct{}),
|
|
|
|
unregistered: make(chan struct{}),
|
|
|
|
}
|
2021-08-17 14:30:02 +00:00
|
|
|
|
|
|
|
obs := NewObserver(&log, &log, false)
|
|
|
|
controlStream := NewControlStream(
|
|
|
|
obs,
|
|
|
|
mockConnectedFuse{},
|
|
|
|
&NamedTunnelConfig{},
|
|
|
|
http2Conn.connIndex,
|
|
|
|
rpcClientFactory.newMockRPCClient,
|
|
|
|
nil,
|
|
|
|
1*time.Second,
|
|
|
|
)
|
|
|
|
http2Conn.controlStreamHandler = controlStream
|
2021-03-04 18:45:39 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/", nil)
|
|
|
|
require.NoError(t, err)
|
2021-03-26 04:04:56 +00:00
|
|
|
req.Header.Set(InternalUpgradeHeader, ControlStreamUpgrade)
|
2021-03-04 18:45:39 +00:00
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp, err := edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusBadGateway, resp.StatusCode)
|
|
|
|
|
|
|
|
assert.NotNil(t, http2Conn.controlStreamErr)
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:41:09 +00:00
|
|
|
func TestGracefulShutdownHTTP2(t *testing.T) {
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
rpcClientFactory := mockRPCClientFactory{
|
|
|
|
registered: make(chan struct{}),
|
|
|
|
unregistered: make(chan struct{}),
|
|
|
|
}
|
2021-02-04 21:09:17 +00:00
|
|
|
events := &eventCollectorSink{}
|
2021-08-17 14:30:02 +00:00
|
|
|
|
2021-02-05 00:07:49 +00:00
|
|
|
shutdownC := make(chan struct{})
|
2021-08-17 14:30:02 +00:00
|
|
|
obs := NewObserver(&log, &log, false)
|
|
|
|
obs.RegisterSink(events)
|
|
|
|
controlStream := NewControlStream(
|
|
|
|
obs,
|
|
|
|
mockConnectedFuse{},
|
|
|
|
&NamedTunnelConfig{},
|
|
|
|
http2Conn.connIndex,
|
|
|
|
rpcClientFactory.newMockRPCClient,
|
|
|
|
shutdownC,
|
|
|
|
1*time.Second,
|
|
|
|
)
|
|
|
|
|
|
|
|
http2Conn.controlStreamHandler = controlStream
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/", nil)
|
|
|
|
require.NoError(t, err)
|
2021-03-26 04:04:56 +00:00
|
|
|
req.Header.Set(InternalUpgradeHeader, ControlStreamUpgrade)
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
_, _ = edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-rpcClientFactory.registered:
|
|
|
|
break //ok
|
|
|
|
case <-time.Tick(time.Second):
|
|
|
|
t.Fatal("timeout out waiting for registration")
|
|
|
|
}
|
|
|
|
|
|
|
|
// signal graceful shutdown
|
2021-02-05 00:07:49 +00:00
|
|
|
close(shutdownC)
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-rpcClientFactory.unregistered:
|
|
|
|
break //ok
|
|
|
|
case <-time.Tick(time.Second):
|
|
|
|
t.Fatal("timeout out waiting for unregistered signal")
|
|
|
|
}
|
2021-08-17 14:30:02 +00:00
|
|
|
assert.True(t, controlStream.IsStopped())
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
2021-02-04 21:09:17 +00:00
|
|
|
|
|
|
|
events.assertSawEvent(t, Event{
|
|
|
|
Index: http2Conn.connIndex,
|
|
|
|
EventType: Unregistering,
|
|
|
|
})
|
2021-01-20 19:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func benchmarkServeHTTP(b *testing.B, test testRequest) {
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
endpoint := fmt.Sprintf("http://localhost:8080/%s", test.endpoint)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StartTimer()
|
|
|
|
resp, err := edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
b.StopTimer()
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.Equal(b, test.expectedStatus, resp.StatusCode)
|
|
|
|
if test.expectedBody != nil {
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.Equal(b, test.expectedBody, respBody)
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2021-01-20 19:41:09 +00:00
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func BenchmarkServeHTTPSimple(b *testing.B) {
|
|
|
|
test := testRequest{
|
|
|
|
name: "ok",
|
|
|
|
endpoint: "ok",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: []byte(http.StatusText(http.StatusOK)),
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkServeHTTP(b, test)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkServeHTTPLargeFile(b *testing.B) {
|
|
|
|
test := testRequest{
|
|
|
|
name: "large_file",
|
|
|
|
endpoint: "large_file",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: testLargeResp,
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkServeHTTP(b, test)
|
|
|
|
}
|