Skip to content

Simplify wstest.Pipe #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down
26 changes: 4 additions & 22 deletions internal/test/wstest/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand All @@ -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 {
Expand Down