Skip to content

Commit 76a6a26

Browse files
committed
Improve wasm test coverage
1 parent 5da52be commit 76a6a26

File tree

13 files changed

+120
-113
lines changed

13 files changed

+120
-113
lines changed

assert_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package websocket_test
22

33
import (
44
"context"
5+
"encoding/hex"
56
"fmt"
7+
"math/rand"
68
"reflect"
79

810
"github.com/google/go-cmp/cmp"
@@ -91,9 +93,32 @@ func assertJSONRead(ctx context.Context, c *websocket.Conn, exp interface{}) err
9193
}
9294

9395
func randBytes(n int) []byte {
94-
return make([]byte, n)
96+
b := make([]byte, n)
97+
rand.Read(b)
98+
return b
9599
}
96100

97101
func randString(n int) string {
98-
return string(randBytes(n))
102+
return hex.EncodeToString(randBytes(n))[:n]
103+
}
104+
105+
func assertEcho(ctx context.Context, c *websocket.Conn, typ websocket.MessageType, n int) error {
106+
p := randBytes(n)
107+
err := c.Write(ctx, typ, p)
108+
if err != nil {
109+
return err
110+
}
111+
typ2, p2, err := c.Read(ctx)
112+
if err != nil {
113+
return err
114+
}
115+
err = assertEqualf(typ, typ2, "unexpected data type")
116+
if err != nil {
117+
return err
118+
}
119+
return assertEqualf(p, p2, "unexpected payload")
120+
}
121+
122+
func assertSubprotocol(c *websocket.Conn, exp string) error {
123+
return assertEqualf(exp, c.Subprotocol(), "unexpected subprotocol")
99124
}

ci/wasm.sh

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ GOOS=js GOARCH=wasm go vet ./...
99
go install golang.org/x/lint/golint
1010
GOOS=js GOARCH=wasm golint -set_exit_status ./...
1111

12-
wsEchoOut="$(mktemp -d)/stdout"
13-
mkfifo "$wsEchoOut"
14-
go install ./internal/wsecho/cmd/wsecho
15-
wsecho > "$wsEchoOut" &
12+
wsjstestOut="$(mktemp -d)/stdout"
13+
mkfifo "$wsjstestOut"
14+
go install ./internal/wsjstest
15+
timeout 30s wsjstest > "$wsjstestOut" &
16+
wsjstestPID=$!
1617

17-
WS_ECHO_SERVER_URL="$(timeout 10s head -n 1 "$wsEchoOut")" || true
18+
WS_ECHO_SERVER_URL="$(timeout 10s head -n 1 "$wsjstestOut")" || true
1819
if [[ -z $WS_ECHO_SERVER_URL ]]; then
19-
echo "./internal/wsecho/cmd/wsecho failed to start in 10s"
20+
echo "./internal/wsjstest failed to start in 10s"
2021
exit 1
2122
fi
2223

2324
go install github.com/agnivade/wasmbrowsertest
2425
GOOS=js GOARCH=wasm go test -exec=wasmbrowsertest ./... -args "$WS_ECHO_SERVER_URL"
2526

26-
kill %1
27-
wait -n || true
27+
if ! wait "$wsjstestPID"; then
28+
echo "wsjstest exited unsuccessfully"
29+
exit 1
30+
fi

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ require (
2323
golang.org/x/sys v0.0.0-20190919044723-0c1ff786ef13 // indirect
2424
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
2525
golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72
26-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
2726
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
2827
gotest.tools/gotestsum v0.3.5
2928
mvdan.cc/sh v2.6.4+incompatible

internal/wsecho/cmd/wsecho/main.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

internal/wsecho/wsecho.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,15 @@ package wsecho
55
import (
66
"context"
77
"io"
8-
"log"
9-
"net/http"
108
"time"
119

1210
"nhooyr.io/websocket"
1311
)
1412

15-
// Serve provides a streaming WebSocket echo server
16-
// for use in tests.
17-
func Serve(w http.ResponseWriter, r *http.Request) {
18-
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
19-
Subprotocols: []string{"echo"},
20-
InsecureSkipVerify: true,
21-
})
22-
if err != nil {
23-
log.Printf("echo server: failed to accept: %+v", err)
24-
return
25-
}
26-
defer c.Close(websocket.StatusInternalError, "")
27-
28-
Loop(r.Context(), c)
29-
}
30-
3113
// Loop echos every msg received from c until an error
3214
// occurs or the context expires.
3315
// The read limit is set to 1 << 30.
34-
func Loop(ctx context.Context, c *websocket.Conn) {
16+
func Loop(ctx context.Context, c *websocket.Conn) error {
3517
defer c.Close(websocket.StatusInternalError, "")
3618

3719
c.SetReadLimit(1 << 30)
@@ -67,7 +49,7 @@ func Loop(ctx context.Context, c *websocket.Conn) {
6749
for {
6850
err := echo()
6951
if err != nil {
70-
return
52+
return err
7153
}
7254
}
7355
}

internal/wsjs/wsjs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func New(url string, protocols []string) (c WebSocket, err error) {
4141
v: js.Global().Get("WebSocket").New(url, jsProtocols),
4242
}
4343

44-
c.setBinaryType("arrayBuffer")
44+
c.setBinaryType("arraybuffer")
4545

4646
c.Extensions = c.v.Get("extensions").String()
4747
c.Protocol = c.v.Get("protocol").String()

internal/wsjstest/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// +build !js
2+
3+
package main
4+
5+
import (
6+
"errors"
7+
"fmt"
8+
"log"
9+
"net/http"
10+
"net/http/httptest"
11+
"os"
12+
"runtime"
13+
"strings"
14+
15+
"nhooyr.io/websocket"
16+
"nhooyr.io/websocket/internal/wsecho"
17+
)
18+
19+
func main() {
20+
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21+
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
22+
Subprotocols: []string{"echo"},
23+
InsecureSkipVerify: true,
24+
})
25+
if err != nil {
26+
log.Fatalf("echo server: failed to accept: %+v", err)
27+
}
28+
defer c.Close(websocket.StatusInternalError, "")
29+
30+
err = wsecho.Loop(r.Context(), c)
31+
32+
var ce websocket.CloseError
33+
if !errors.As(err, &ce) || ce.Code != websocket.StatusNormalClosure {
34+
log.Fatalf("unexpected loop error: %+v", err)
35+
}
36+
37+
os.Exit(0)
38+
}))
39+
wsURL := strings.Replace(s.URL, "http", "ws", 1)
40+
fmt.Printf("%v\n", wsURL)
41+
42+
runtime.Goexit()
43+
}

netconn_js.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

netconn_normal.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

websocket.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,3 +946,7 @@ func (c *Conn) extractBufioWriterBuf(w io.Writer) {
946946

947947
c.bw.Reset(w)
948948
}
949+
950+
func (c *netConn) netConnReader(ctx context.Context) (MessageType, io.Reader, error) {
951+
return c.c.Reader(c.readContext)
952+
}

0 commit comments

Comments
 (0)