Skip to content

Commit 0cda1c3

Browse files
committed
Fix race in wasm test
1 parent 9b8320e commit 0cda1c3

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

ci/wasm.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ GOOS=js GOARCH=wasm golint -set_exit_status ./...
1111

1212
wsjstestOut="$(mktemp)"
1313
go install ./internal/wsjstest
14-
timeout 30s wsjstest >> "$wsjstestOut" &
14+
timeout 30s wsjstest >> "$wsjstestOut" 2>&1 &
1515
wsjstestPID=$!
1616

1717
# See https://superuser.com/a/900134
@@ -24,6 +24,7 @@ fi
2424
go install github.com/agnivade/wasmbrowsertest
2525
GOOS=js GOARCH=wasm go test -exec=wasmbrowsertest ./... -args "$WS_ECHO_SERVER_URL"
2626

27+
kill "$wsjstestPID"
2728
if ! wait "$wsjstestPID"; then
2829
echo "--- wsjstest exited unsuccessfully"
2930
echo "output:"
File renamed without changes.

internal/wsjstest/main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
"net/http"
1010
"net/http/httptest"
1111
"os"
12-
"runtime"
12+
"os/signal"
1313
"strings"
14+
"syscall"
1415

1516
"nhooyr.io/websocket"
1617
"nhooyr.io/websocket/internal/wsecho"
@@ -33,12 +34,13 @@ func main() {
3334
if !errors.As(err, &ce) || ce.Code != websocket.StatusNormalClosure {
3435
log.Fatalf("unexpected loop error: %+v", err)
3536
}
36-
37-
os.Exit(0)
3837
}))
3938

4039
wsURL := strings.Replace(s.URL, "http", "ws", 1)
4140
fmt.Printf("%v\n", wsURL)
4241

43-
runtime.Goexit()
42+
sigs := make(chan os.Signal)
43+
signal.Notify(sigs, syscall.SIGTERM)
44+
45+
<-sigs
4446
}

websocket_js.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type Conn struct {
3535
readSignal chan struct{}
3636
readBufMu sync.Mutex
3737
readBuf []wsjs.MessageEvent
38+
39+
// Only used by tests
40+
receivedCloseFrame chan struct{}
3841
}
3942

4043
func (c *Conn) close(err error) {
@@ -55,7 +58,11 @@ func (c *Conn) init() {
5558

5659
c.isReadClosed = &atomicInt64{}
5760

61+
c.receivedCloseFrame = make(chan struct{})
62+
5863
c.releaseOnClose = c.ws.OnClose(func(e wsjs.CloseEvent) {
64+
close(c.receivedCloseFrame)
65+
5966
cerr := CloseError{
6067
Code: StatusCode(e.Code),
6168
Reason: e.Reason,

websocket_js_export_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// +build js
2+
3+
package websocket
4+
5+
import (
6+
"context"
7+
"fmt"
8+
)
9+
10+
func (c *Conn) WaitCloseFrame(ctx context.Context) error {
11+
select {
12+
case <-c.receivedCloseFrame:
13+
return nil
14+
case <-ctx.Done():
15+
return fmt.Errorf("failed to wait for close frame: %w", ctx.Err())
16+
}
17+
}

websocket_js_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ func TestConn(t *testing.T) {
5050
if err != nil {
5151
t.Fatal(err)
5252
}
53+
54+
err = c.WaitCloseFrame(ctx)
55+
if err != nil {
56+
t.Fatal(err)
57+
}
5358
}

0 commit comments

Comments
 (0)