From 97345d8042c26a1e56f8e3cc5b494230677e4ab0 Mon Sep 17 00:00:00 2001 From: Anmol Sethi <hi@nhooyr.io> Date: Wed, 26 Feb 2020 00:29:13 -0500 Subject: [PATCH] Simplify wstest.Pipe --- conn_test.go | 6 ++++-- internal/test/wstest/pipe.go | 26 ++++---------------------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/conn_test.go b/conn_test.go index 535afe2..28da3c0 100644 --- a/conn_test.go +++ b/conn_test.go @@ -337,8 +337,10 @@ func newConnTest(t testing.TB, dialOpts *websocket.DialOptions, acceptOpts *webs tt = &connTest{t: t, ctx: ctx} tt.appendDone(cancel) - c1, c2, err := wstest.Pipe(dialOpts, acceptOpts) - assert.Success(tt.t, err) + c1, c2 = wstest.Pipe(dialOpts, acceptOpts) + if xrand.Bool() { + c1, c2 = c2, c1 + } tt.appendDone(func() { c2.Close(websocket.StatusInternalError, "") c1.Close(websocket.StatusInternalError, "") diff --git a/internal/test/wstest/pipe.go b/internal/test/wstest/pipe.go index 0a2899e..1534f31 100644 --- a/internal/test/wstest/pipe.go +++ b/internal/test/wstest/pipe.go @@ -5,26 +5,19 @@ package wstest import ( "bufio" "context" - "fmt" "net" "net/http" "net/http/httptest" "nhooyr.io/websocket" - "nhooyr.io/websocket/internal/errd" - "nhooyr.io/websocket/internal/test/xrand" ) // Pipe is used to create an in memory connection // between two websockets analogous to net.Pipe. -func Pipe(dialOpts *websocket.DialOptions, acceptOpts *websocket.AcceptOptions) (_ *websocket.Conn, _ *websocket.Conn, err error) { - defer errd.Wrap(&err, "failed to create ws pipe") - - var serverConn *websocket.Conn - var acceptErr error +func Pipe(dialOpts *websocket.DialOptions, acceptOpts *websocket.AcceptOptions) (clientConn, serverConn *websocket.Conn) { tt := fakeTransport{ h: func(w http.ResponseWriter, r *http.Request) { - serverConn, acceptErr = websocket.Accept(w, r, acceptOpts) + serverConn, _ = websocket.Accept(w, r, acceptOpts) }, } @@ -36,19 +29,8 @@ func Pipe(dialOpts *websocket.DialOptions, acceptOpts *websocket.AcceptOptions) Transport: tt, } - clientConn, _, err := websocket.Dial(context.Background(), "ws://example.com", dialOpts) - if err != nil { - return nil, nil, fmt.Errorf("failed to dial with fake transport: %w", err) - } - - if serverConn == nil { - return nil, nil, fmt.Errorf("failed to get server conn from fake transport: %w", acceptErr) - } - - if xrand.Bool() { - return serverConn, clientConn, nil - } - return clientConn, serverConn, nil + clientConn, _, _ = websocket.Dial(context.Background(), "ws://example.com", dialOpts) + return clientConn, serverConn } type fakeTransport struct { -- GitLab