Skip to content

Commit dc34ee5

Browse files
committed
Add WASM test
1 parent 599534b commit dc34ee5

10 files changed

+174
-86
lines changed

cmp_test.go renamed to assert_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package websocket_test
22

33
import (
4+
"context"
5+
"fmt"
46
"reflect"
57

68
"github.com/google/go-cmp/cmp"
9+
10+
"nhooyr.io/websocket"
11+
"nhooyr.io/websocket/wsjson"
712
)
813

914
// https://github.com/google/go-cmp/issues/40#issuecomment-328615283
@@ -51,3 +56,44 @@ func structTypes(v reflect.Value, m map[reflect.Type]struct{}) {
5156
}
5257
}
5358
}
59+
60+
func assertEqualf(exp, act interface{}, f string, v ...interface{}) error {
61+
if diff := cmpDiff(exp, act); diff != "" {
62+
return fmt.Errorf(f+": %v", append(v, diff)...)
63+
}
64+
return nil
65+
}
66+
67+
func assertJSONEcho(ctx context.Context, c *websocket.Conn, n int) error {
68+
exp := randString(n)
69+
err := wsjson.Write(ctx, c, exp)
70+
if err != nil {
71+
return err
72+
}
73+
74+
var act interface{}
75+
err = wsjson.Read(ctx, c, &act)
76+
if err != nil {
77+
return err
78+
}
79+
80+
return assertEqualf(exp, act, "unexpected JSON")
81+
}
82+
83+
func assertJSONRead(ctx context.Context, c *websocket.Conn, exp interface{}) error {
84+
var act interface{}
85+
err := wsjson.Read(ctx, c, &act)
86+
if err != nil {
87+
return err
88+
}
89+
90+
return assertEqualf(exp, act, "unexpected JSON")
91+
}
92+
93+
func randBytes(n int) []byte {
94+
return make([]byte, n)
95+
}
96+
97+
func randString(n int) string {
98+
return string(randBytes(n))
99+
}

ci/wasm.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ set -euo pipefail
44
cd "$(dirname "${0}")"
55
cd "$(git rev-parse --show-toplevel)"
66

7+
stdout="$(mktemp -d)/stdout"
8+
mkfifo "$stdout"
9+
go run ./internal/wsecho/cmd > "$stdout" &
10+
11+
WS_ECHO_SERVER_URL="$(head -n 1 "$stdout")"
12+
713
GOOS=js GOARCH=wasm go vet ./...
814
go install golang.org/x/lint/golint
915
GOOS=js GOARCH=wasm golint -set_exit_status ./...
10-
GOOS=js GOARCH=wasm go test ./...
16+
GOOS=js GOARCH=wasm go test ./... -args "$WS_ECHO_SERVER_URL"

internal/echoserver/echoserver.go

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

internal/wsecho/cmd/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// +build !js
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"net/http"
8+
"net/http/httptest"
9+
"runtime"
10+
"strings"
11+
12+
"nhooyr.io/websocket/internal/wsecho"
13+
)
14+
15+
func main() {
16+
s := httptest.NewServer(http.HandlerFunc(wsecho.Serve))
17+
wsURL := strings.Replace(s.URL, "http", "ws", 1)
18+
fmt.Printf("%v\n", wsURL)
19+
20+
runtime.Goexit()
21+
}

internal/wsecho/wsecho.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// +build !js
2+
3+
package wsecho
4+
5+
import (
6+
"context"
7+
"io"
8+
"log"
9+
"net/http"
10+
"time"
11+
12+
"nhooyr.io/websocket"
13+
)
14+
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+
31+
// Loop echos every msg received from c until an error
32+
// occurs or the context expires.
33+
// The read limit is set to 1 << 40.
34+
func Loop(ctx context.Context, c *websocket.Conn) {
35+
defer c.Close(websocket.StatusInternalError, "")
36+
37+
c.SetReadLimit(1 << 40)
38+
39+
ctx, cancel := context.WithTimeout(ctx, time.Minute)
40+
defer cancel()
41+
42+
b := make([]byte, 32768)
43+
echo := func() error {
44+
typ, r, err := c.Reader(ctx)
45+
if err != nil {
46+
return err
47+
}
48+
49+
w, err := c.Writer(ctx, typ)
50+
if err != nil {
51+
return err
52+
}
53+
54+
_, err = io.CopyBuffer(w, r, b)
55+
if err != nil {
56+
return err
57+
}
58+
59+
err = w.Close()
60+
if err != nil {
61+
return err
62+
}
63+
64+
return nil
65+
}
66+
67+
for {
68+
err := echo()
69+
if err != nil {
70+
return
71+
}
72+
}
73+
}

websocket_autobahn_python_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"strings"
2121
"testing"
2222
"time"
23+
24+
"nhooyr.io/websocket/internal/wsecho"
2325
)
2426

2527
// https://github.com/crossbario/autobahn-python/tree/master/wstest
@@ -34,7 +36,7 @@ func TestPythonAutobahnServer(t *testing.T) {
3436
t.Logf("server handshake failed: %+v", err)
3537
return
3638
}
37-
echoLoop(r.Context(), c)
39+
wsecho.Loop(r.Context(), c)
3840
}))
3941
defer s.Close()
4042

@@ -186,7 +188,7 @@ func TestPythonAutobahnClientOld(t *testing.T) {
186188
if err != nil {
187189
t.Fatal(err)
188190
}
189-
echoLoop(ctx, c)
191+
wsecho.Loop(ctx, c)
190192
}()
191193
}
192194

websocket_bench_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"nhooyr.io/websocket"
16+
"nhooyr.io/websocket/internal/wsecho"
1617
)
1718

1819
func BenchmarkConn(b *testing.B) {
@@ -54,7 +55,7 @@ func benchConn(b *testing.B, echo, stream bool, size int) {
5455
return err
5556
}
5657
if echo {
57-
echoLoop(r.Context(), c)
58+
wsecho.Loop(r.Context(), c)
5859
} else {
5960
discardLoop(r.Context(), c)
6061
}

websocket_js_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,34 @@ package websocket_test
22

33
import (
44
"context"
5+
"net/http"
6+
"os"
57
"testing"
68
"time"
79

810
"nhooyr.io/websocket"
911
)
1012

13+
var wsEchoServerURL = os.Args[1]
14+
1115
func TestWebSocket(t *testing.T) {
1216
t.Parallel()
1317

14-
_, _, err := websocket.Dial(context.Background(), "ws://localhost:8081", nil)
18+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
19+
defer cancel()
20+
21+
c, resp, err := websocket.Dial(ctx, wsEchoServerURL, nil)
1522
if err != nil {
1623
t.Fatal(err)
1724
}
1825

19-
time.Sleep(time.Second)
26+
err = assertEqualf(&http.Response{}, resp, "unexpected http response")
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
31+
err = assertJSONEcho(ctx, c, 4096)
32+
if err != nil {
33+
t.Fatal(err)
34+
}
2035
}

websocket_test.go

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"go.uber.org/multierr"
3030

3131
"nhooyr.io/websocket"
32+
"nhooyr.io/websocket/internal/wsecho"
3233
"nhooyr.io/websocket/wsjson"
3334
"nhooyr.io/websocket/wspb"
3435
)
@@ -966,7 +967,7 @@ func TestAutobahn(t *testing.T) {
966967

967968
ctx := r.Context()
968969
if testingClient {
969-
echoLoop(r.Context(), c)
970+
wsecho.Loop(r.Context(), c)
970971
return nil
971972
}
972973

@@ -1007,7 +1008,7 @@ func TestAutobahn(t *testing.T) {
10071008
return
10081009
}
10091010

1010-
echoLoop(ctx, c)
1011+
wsecho.Loop(ctx, c)
10111012
}
10121013
t.Run(name, func(t *testing.T) {
10131014
t.Parallel()
@@ -1849,47 +1850,6 @@ func TestAutobahn(t *testing.T) {
18491850
})
18501851
}
18511852

1852-
func echoLoop(ctx context.Context, c *websocket.Conn) {
1853-
defer c.Close(websocket.StatusInternalError, "")
1854-
1855-
c.SetReadLimit(1 << 40)
1856-
1857-
ctx, cancel := context.WithTimeout(ctx, time.Minute)
1858-
defer cancel()
1859-
1860-
b := make([]byte, 32768)
1861-
echo := func() error {
1862-
typ, r, err := c.Reader(ctx)
1863-
if err != nil {
1864-
return err
1865-
}
1866-
1867-
w, err := c.Writer(ctx, typ)
1868-
if err != nil {
1869-
return err
1870-
}
1871-
1872-
_, err = io.CopyBuffer(w, r, b)
1873-
if err != nil {
1874-
return err
1875-
}
1876-
1877-
err = w.Close()
1878-
if err != nil {
1879-
return err
1880-
}
1881-
1882-
return nil
1883-
}
1884-
1885-
for {
1886-
err := echo()
1887-
if err != nil {
1888-
return
1889-
}
1890-
}
1891-
}
1892-
18931853
func assertCloseStatus(err error, code websocket.StatusCode) error {
18941854
var cerr websocket.CloseError
18951855
if !errors.As(err, &cerr) {
@@ -1898,24 +1858,6 @@ func assertCloseStatus(err error, code websocket.StatusCode) error {
18981858
return assertEqualf(code, cerr.Code, "unexpected status code")
18991859
}
19001860

1901-
func assertJSONRead(ctx context.Context, c *websocket.Conn, exp interface{}) (err error) {
1902-
var act interface{}
1903-
err = wsjson.Read(ctx, c, &act)
1904-
if err != nil {
1905-
return err
1906-
}
1907-
1908-
return assertEqualf(exp, act, "unexpected JSON")
1909-
}
1910-
1911-
func randBytes(n int) []byte {
1912-
return make([]byte, n)
1913-
}
1914-
1915-
func randString(n int) string {
1916-
return string(randBytes(n))
1917-
}
1918-
19191861
func assertEcho(ctx context.Context, c *websocket.Conn, typ websocket.MessageType, n int) error {
19201862
p := randBytes(n)
19211863
err := c.Write(ctx, typ, p)
@@ -1949,13 +1891,6 @@ func assertSubprotocol(c *websocket.Conn, exp string) error {
19491891
return assertEqualf(exp, c.Subprotocol(), "unexpected subprotocol")
19501892
}
19511893

1952-
func assertEqualf(exp, act interface{}, f string, v ...interface{}) error {
1953-
if diff := cmpDiff(exp, act); diff != "" {
1954-
return fmt.Errorf(f+": %v", append(v, diff)...)
1955-
}
1956-
return nil
1957-
}
1958-
19591894
func assertNetConnRead(r io.Reader, exp string) error {
19601895
act := make([]byte, len(exp))
19611896
_, err := r.Read(act)

wsjson/wsjson_js.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ func write(ctx context.Context, c *websocket.Conn, v interface{}) error {
5454
return err
5555
}
5656

57-
return c.Write(ctx, websocket.MessageBinary, b)
57+
return c.Write(ctx, websocket.MessageText, b)
5858
}

0 commit comments

Comments
 (0)