Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ws): wrongly sending nil as error when write on close fails #1118

Merged
merged 1 commit into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions js/modules/k6/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,13 @@ func (s *Socket) closeConnection(code int) error {
s.shutdownOnce.Do(func() {
rt := common.GetRuntime(s.ctx)

writeErr := s.conn.WriteControl(websocket.CloseMessage,
err = s.conn.WriteControl(websocket.CloseMessage,
websocket.FormatCloseMessage(code, ""),
time.Now().Add(writeWait),
)
if writeErr != nil {
if err != nil {
// Just call the handler, we'll try to close the connection anyway
s.handleEvent("error", rt.ToValue(err))
err = writeErr
}

// trigger `close` event when the client closes the connection
Expand Down
29 changes: 29 additions & 0 deletions js/modules/k6/ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,35 @@ func TestErrors(t *testing.T) {
assert.NoError(t, err)
assertSessionMetricsEmitted(t, stats.GetBufferedSamples(samples), "", sr("WSBIN_URL/ws-echo"), 101, "")
})

t.Run("error on close", func(t *testing.T) {
_, err := common.RunString(rt, sr(`
var closed = false;
let res = ws.connect("WSBIN_URL/ws-close", function(socket){
socket.on('open', function open() {
socket.setInterval(function timeout() {
socket.ping();
}, 1000);
});

socket.on("ping", function() {
socket.close();
});

socket.on("error", function(errorEvent) {
if (errorEvent == null) {
throw new Error(JSON.stringify(errorEvent));
}
if (!closed) {
closed = true;
socket.close();
}
});
});
`))
assert.NoError(t, err)
assertSessionMetricsEmitted(t, stats.GetBufferedSamples(samples), "", sr("WSBIN_URL/ws-close"), 101, "")
})
}

func TestSystemTags(t *testing.T) {
Expand Down