Skip to content

Commit

Permalink
Ping should not send data if the connection is closing/closed
Browse files Browse the repository at this point in the history
Also edit tests to make them not fail now that this will throw an
exception
  • Loading branch information
mstoykov committed Apr 5, 2023
1 parent 3a84011 commit 2d9dc2c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
2 changes: 2 additions & 0 deletions websockets/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ func (w *webSocket) send(msg goja.Value) {

// Ping sends a ping message over the websocket.
func (w *webSocket) ping() {
w.assertStateOpen()

pingID := strconv.Itoa(w.sendPings.counter)

w.writeQueueCh <- message{
Expand Down
49 changes: 24 additions & 25 deletions websockets/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,29 +1266,26 @@ func TestLockingUpWithAThrow(t *testing.T) {
require.NoError(t, ts.vu.RuntimeField.Set("l", fmt.Println))
err := ts.ev.Start(func() error {
_, runErr := ts.rt.RunString(sr(`
let a = 0;
const connections = 1000;
async function s() {
let a = 0;
const connections = 1000;
async function s() {
let ws = new WebSocket("WSBIN_URL/ws-echo")
ws.addEventListener("open", () => {
ws.ping()
a++
a++
})
ws.addEventListener("pong", () => {
// l("pong")
// l("pong")
ws.ping()
if (a == connections){
a++
ws.close()
}
if (a == connections){
a++
ws.close()
throw "s";
}
})
ws.addEventListener("close", () =>{
throw "s";
})
}
[...Array(connections)].forEach(_ => s())
}
[...Array(connections)].forEach(_ => s())
`))
return runErr
})
Expand All @@ -1314,23 +1311,25 @@ func TestLockingUpWithAJustGeneralCancel(t *testing.T) {
require.NoError(t, ts.vu.RuntimeField.Set("cancel", cancel))
err := ts.ev.Start(func() error {
_, runErr := ts.rt.RunString(sr(`
let a = 0;
const connections = 1000;
async function s() {
let a = 0;
const connections = 1000;
async function s() {
var ws = new WebSocket("WSBIN_URL/ws-echo")
ws.addEventListener("open", () => {
ws.ping()
})
ws.addEventListener("pong", () => {
ws.ping()
a++
if (a == connections){
cancel()
}
try{
ws.ping() // this will
} catch(e) {}
a++
if (a == connections){
cancel()
}
})
}
[...Array(connections)].forEach(_ => s())
}
[...Array(connections)].forEach(_ => s())
`))
return runErr
})
Expand Down

0 comments on commit 2d9dc2c

Please sign in to comment.