good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit e92b5474 authored by Anmol Sethi's avatar Anmol Sethi Committed by GitHub
Browse files

Merge pull request #202 from nhooyr/wstest

Simplify wstest.Pipe
parents 108d760d 97345d80
No related branches found
No related tags found
No related merge requests found
......@@ -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, "")
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment