From 6ead6aaf8eb5c3a5c7f533109e0a3baab763289a Mon Sep 17 00:00:00 2001 From: Anmol Sethi <hi@nhooyr.io> Date: Fri, 7 Apr 2023 07:59:28 -0700 Subject: [PATCH] autobahn_test: Use docker to avoid issues with python2 EOL Also ran gofmt on everything. Thanks again @paralin. #334 Co-authored-by: Christian Stewart <christian@paral.in> --- accept.go | 1 + accept_test.go | 1 + autobahn_test.go | 62 +++++++++++++++++++++++++++++------- close.go | 1 + close_test.go | 1 + compress.go | 1 + compress_test.go | 1 + conn.go | 1 + conn_test.go | 1 + dial.go | 1 + dial_test.go | 1 + doc.go | 11 ++++--- export_test.go | 1 + frame_test.go | 1 + internal/test/wstest/echo.go | 2 +- internal/test/wstest/pipe.go | 1 + internal/wsjs/wsjs_js.go | 1 + make.sh | 17 ++++++++++ read.go | 1 + write.go | 1 + 20 files changed, 90 insertions(+), 18 deletions(-) create mode 100755 make.sh diff --git a/accept.go b/accept.go index 542b61e..d918aab 100644 --- a/accept.go +++ b/accept.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/accept_test.go b/accept_test.go index 67ece25..ae17c0b 100644 --- a/accept_test.go +++ b/accept_test.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/autobahn_test.go b/autobahn_test.go index 7c735a3..4df4b66 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket_test @@ -33,6 +34,12 @@ var excludedAutobahnCases = []string{ var autobahnCases = []string{"*"} +// Used to run individual test cases. autobahnCases runs only those cases matched +// and not excluded by excludedAutobahnCases. Adding cases here means excludedAutobahnCases +// is niled. +// TODO: +var forceAutobahnCases = []string{} + func TestAutobahn(t *testing.T) { t.Parallel() @@ -43,16 +50,18 @@ func TestAutobahn(t *testing.T) { if os.Getenv("AUTOBAHN") == "fast" { // These are the slow tests. excludedAutobahnCases = append(excludedAutobahnCases, - "9.*", "13.*", "12.*", + "9.*", "12.*", "13.*", ) } - ctx, cancel := context.WithTimeout(context.Background(), time.Minute*15) + ctx, cancel := context.WithTimeout(context.Background(), time.Hour) defer cancel() - wstestURL, closeFn, err := wstestClientServer(ctx) + wstestURL, closeFn, err := wstestServer(ctx) assert.Success(t, err) - defer closeFn() + defer func() { + assert.Success(t, closeFn()) + }() err = waitWS(ctx, wstestURL) assert.Success(t, err) @@ -100,17 +109,24 @@ func waitWS(ctx context.Context, url string) error { return ctx.Err() } -func wstestClientServer(ctx context.Context) (url string, closeFn func(), err error) { +// TODO: Let docker pick the port and use docker port to find it. +// Does mean we can't use -i but that's fine. +func wstestServer(ctx context.Context) (url string, closeFn func() error, err error) { serverAddr, err := unusedListenAddr() if err != nil { return "", nil, err } + _, serverPort, err := net.SplitHostPort(serverAddr) + if err != nil { + return "", nil, err + } url = "ws://" + serverAddr + const outDir = "ci/out/wstestClientReports" specFile, err := tempJSONFile(map[string]interface{}{ "url": url, - "outdir": "ci/out/wstestClientReports", + "outdir": outDir, "cases": autobahnCases, "exclude-cases": excludedAutobahnCases, }) @@ -118,26 +134,48 @@ func wstestClientServer(ctx context.Context) (url string, closeFn func(), err er return "", nil, fmt.Errorf("failed to write spec: %w", err) } - ctx, cancel := context.WithTimeout(context.Background(), time.Minute*15) + ctx, cancel := context.WithTimeout(ctx, time.Hour) defer func() { if err != nil { cancel() } }() - args := []string{"--mode", "fuzzingserver", "--spec", specFile, + wd, err := os.Getwd() + if err != nil { + return "", nil, err + } + + var args []string + args = append(args, "run", "-i", "--rm", + "-v", fmt.Sprintf("%s:%[1]s", specFile), + "-v", fmt.Sprintf("%s/ci:/ci", wd), + fmt.Sprintf("-p=%s:%s", serverAddr, serverPort), + "crossbario/autobahn-testsuite", + ) + args = append(args, "wstest", "--mode", "fuzzingserver", "--spec", specFile, // Disables some server that runs as part of fuzzingserver mode. // See https://github.com/crossbario/autobahn-testsuite/blob/058db3a36b7c3a1edf68c282307c6b899ca4857f/autobahntestsuite/autobahntestsuite/wstest.py#L124 "--webport=0", - } - wstest := exec.CommandContext(ctx, "wstest", args...) + ) + fmt.Println(strings.Join(args, " ")) + // TODO: pull image in advance + wstest := exec.CommandContext(ctx, "docker", args...) + // TODO: log to *testing.T + wstest.Stdout = os.Stdout + wstest.Stderr = os.Stderr err = wstest.Start() if err != nil { return "", nil, fmt.Errorf("failed to start wstest: %w", err) } - return url, func() { - wstest.Process.Kill() + // TODO: kill + return url, func() error { + err = wstest.Process.Kill() + if err != nil { + return fmt.Errorf("failed to kill wstest: %w", err) + } + return nil }, nil } diff --git a/close.go b/close.go index d76dc2f..eab49a8 100644 --- a/close.go +++ b/close.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/close_test.go b/close_test.go index 00a48d9..6bf3c25 100644 --- a/close_test.go +++ b/close_test.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/compress.go b/compress.go index f49d9e5..6873447 100644 --- a/compress.go +++ b/compress.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/compress_test.go b/compress_test.go index 2c4c896..7b0e3a6 100644 --- a/compress_test.go +++ b/compress_test.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/conn.go b/conn.go index beb26ce..25b5a20 100644 --- a/conn.go +++ b/conn.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/conn_test.go b/conn_test.go index 0fbd174..19961d1 100644 --- a/conn_test.go +++ b/conn_test.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket_test diff --git a/dial.go b/dial.go index a79b55e..7e77d6e 100644 --- a/dial.go +++ b/dial.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/dial_test.go b/dial_test.go index 28c255c..e5f8ab3 100644 --- a/dial_test.go +++ b/dial_test.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/doc.go b/doc.go index efa920e..a2b873c 100644 --- a/doc.go +++ b/doc.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js // Package websocket implements the RFC 6455 WebSocket protocol. @@ -16,7 +17,7 @@ // // More documentation at https://nhooyr.io/websocket. // -// Wasm +// # Wasm // // The client side supports compiling to Wasm. // It wraps the WebSocket browser API. @@ -25,8 +26,8 @@ // // Some important caveats to be aware of: // -// - Accept always errors out -// - Conn.Ping is no-op -// - HTTPClient, HTTPHeader and CompressionMode in DialOptions are no-op -// - *http.Response from Dial is &http.Response{} with a 101 status code on success +// - Accept always errors out +// - Conn.Ping is no-op +// - HTTPClient, HTTPHeader and CompressionMode in DialOptions are no-op +// - *http.Response from Dial is &http.Response{} with a 101 status code on success package websocket // import "nhooyr.io/websocket" diff --git a/export_test.go b/export_test.go index 88b82c9..d618a15 100644 --- a/export_test.go +++ b/export_test.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/frame_test.go b/frame_test.go index 7682624..93ad8b5 100644 --- a/frame_test.go +++ b/frame_test.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/internal/test/wstest/echo.go b/internal/test/wstest/echo.go index 8f4e47c..0938a13 100644 --- a/internal/test/wstest/echo.go +++ b/internal/test/wstest/echo.go @@ -21,7 +21,7 @@ func EchoLoop(ctx context.Context, c *websocket.Conn) error { c.SetReadLimit(1 << 30) - ctx, cancel := context.WithTimeout(ctx, time.Minute) + ctx, cancel := context.WithTimeout(ctx, time.Minute*5) defer cancel() b := make([]byte, 32<<10) diff --git a/internal/test/wstest/pipe.go b/internal/test/wstest/pipe.go index f3d4c51..8e1deb4 100644 --- a/internal/test/wstest/pipe.go +++ b/internal/test/wstest/pipe.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package wstest diff --git a/internal/wsjs/wsjs_js.go b/internal/wsjs/wsjs_js.go index 26ffb45..88e8f43 100644 --- a/internal/wsjs/wsjs_js.go +++ b/internal/wsjs/wsjs_js.go @@ -1,3 +1,4 @@ +//go:build js // +build js // Package wsjs implements typed access to the browser javascript WebSocket API. diff --git a/make.sh b/make.sh new file mode 100755 index 0000000..578203c --- /dev/null +++ b/make.sh @@ -0,0 +1,17 @@ +#!/bin/sh +set -eu + +cd "$(dirname "$0")" + +fmt() { + go mod tidy + gofmt -s -w . + goimports -w "-local=$(go list -m)" . +} + +if ! command -v wasmbrowsertest >/dev/null; then + go install github.com/agnivade/wasmbrowsertest@latest +fi + +fmt +go test -race --timeout=1h ./... "$@" diff --git a/read.go b/read.go index c4234f2..19727fd 100644 --- a/read.go +++ b/read.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket diff --git a/write.go b/write.go index 58bfdf9..7921eac 100644 --- a/write.go +++ b/write.go @@ -1,3 +1,4 @@ +//go:build !js // +build !js package websocket -- GitLab