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: empty stream state on broadcast #129

Merged
merged 4 commits into from
Feb 19, 2024
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
4 changes: 4 additions & 0 deletions pkg/wsserver/wsserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func (s *webSocketServer) Close() {
func (s *webSocketServer) Broadcast(ctx context.Context, stream string, payload interface{}) {
s.mux.Lock()
ss := s.streamMap[stream]
if ss == nil {
ss = &streamState{}
s.streamMap[stream] = ss
}
conns := make([]*webSocketConnection, len(ss.conns))
copy(conns, ss.conns)
s.mux.Unlock()
Expand Down
29 changes: 29 additions & 0 deletions pkg/wsserver/wsserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,35 @@ func TestConnectBadWebsocketHandshake(t *testing.T) {

}

func TestBroadcastStartWithoutConnections(t *testing.T) {
assert := assert.New(t)

w, ts := newTestWebSocketServer()
defer ts.Close()
defer w.Close()

u, _ := url.Parse(ts.URL)
u.Scheme = "ws"
u.Path = "/ws"
stream := "banana"

go w.Broadcast(w.ctx, stream, "Hello World")

c1, _, err := ws.DefaultDialer.Dial(u.String(), nil)
assert.NoError(err)
c1.WriteJSON(&WebSocketCommandMessage{
Type: "start",
Stream: stream,
})

c2, _, err := ws.DefaultDialer.Dial(u.String(), nil)
assert.NoError(err)
c2.WriteJSON(&WebSocketCommandMessage{
Type: "start",
Stream: stream,
})
}

func TestBroadcast(t *testing.T) {
assert := assert.New(t)

Expand Down
Loading