good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit 0cda1c34 authored by Anmol Sethi's avatar Anmol Sethi
Browse files

Fix race in wasm test

parent 9b8320ed
Branches
Tags
No related merge requests found
......@@ -11,7 +11,7 @@ GOOS=js GOARCH=wasm golint -set_exit_status ./...
wsjstestOut="$(mktemp)"
go install ./internal/wsjstest
timeout 30s wsjstest >> "$wsjstestOut" &
timeout 30s wsjstest >> "$wsjstestOut" 2>&1 &
wsjstestPID=$!
# See https://superuser.com/a/900134
......@@ -24,6 +24,7 @@ fi
go install github.com/agnivade/wasmbrowsertest
GOOS=js GOARCH=wasm go test -exec=wasmbrowsertest ./... -args "$WS_ECHO_SERVER_URL"
kill "$wsjstestPID"
if ! wait "$wsjstestPID"; then
echo "--- wsjstest exited unsuccessfully"
echo "output:"
......
File moved
......@@ -9,8 +9,9 @@ import (
"net/http"
"net/http/httptest"
"os"
"runtime"
"os/signal"
"strings"
"syscall"
"nhooyr.io/websocket"
"nhooyr.io/websocket/internal/wsecho"
......@@ -33,12 +34,13 @@ func main() {
if !errors.As(err, &ce) || ce.Code != websocket.StatusNormalClosure {
log.Fatalf("unexpected loop error: %+v", err)
}
os.Exit(0)
}))
wsURL := strings.Replace(s.URL, "http", "ws", 1)
fmt.Printf("%v\n", wsURL)
runtime.Goexit()
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGTERM)
<-sigs
}
......@@ -35,6 +35,9 @@ type Conn struct {
readSignal chan struct{}
readBufMu sync.Mutex
readBuf []wsjs.MessageEvent
// Only used by tests
receivedCloseFrame chan struct{}
}
func (c *Conn) close(err error) {
......@@ -55,7 +58,11 @@ func (c *Conn) init() {
c.isReadClosed = &atomicInt64{}
c.receivedCloseFrame = make(chan struct{})
c.releaseOnClose = c.ws.OnClose(func(e wsjs.CloseEvent) {
close(c.receivedCloseFrame)
cerr := CloseError{
Code: StatusCode(e.Code),
Reason: e.Reason,
......
// +build js
package websocket
import (
"context"
"fmt"
)
func (c *Conn) WaitCloseFrame(ctx context.Context) error {
select {
case <-c.receivedCloseFrame:
return nil
case <-ctx.Done():
return fmt.Errorf("failed to wait for close frame: %w", ctx.Err())
}
}
......@@ -50,4 +50,9 @@ func TestConn(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = c.WaitCloseFrame(ctx)
if err != nil {
t.Fatal(err)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment