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

Add check before writing to websocket channel #298

Merged
merged 4 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions internal/events/websockets/websocket_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ func (wc *websocketConnection) protocolError(err error) {
}

func (wc *websocketConnection) send(msg interface{}) error {
if wc.closed {
return i18n.NewError(wc.ctx, i18n.MsgWSClosing) // TODO: set to "closed"
}
select {
case wc.sendMessages <- msg:
return nil
Expand Down
24 changes: 24 additions & 0 deletions internal/events/websockets/websockets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,27 @@ func TestDispatchAutoAck(t *testing.T) {
assert.NoError(t, err)
cbs.AssertExpectations(t)
}

func TestWebsocketSendAfterClose(t *testing.T) {
cbs := &eventsmocks.Callbacks{}
ws, wsc, cancel := newTestWebsockets(t, cbs)
defer cancel()

subscribedConn := make(chan string, 1)
cbs.On("EphemeralSubscription",
mock.MatchedBy(func(s string) bool {
subscribedConn <- s
return true
}),
"ns1", mock.Anything, mock.Anything).Return(nil)

err := wsc.Send(context.Background(), []byte(`{"type":"start","namespace":"ns1","ephemeral":true}`))
assert.NoError(t, err)

connID := <-subscribedConn
connection := ws.connections[connID]
connection.wsConn.Close()
<-connection.senderDone
err = connection.send(map[string]string{"foo": "bar"})
assert.Regexp(t, "FF10160", err)
}