websocket: add SendAll

This commit is contained in:
Martin/Geno 2018-04-26 21:24:42 +02:00
parent 6b332f7254
commit 5fd5733261
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
2 changed files with 52 additions and 0 deletions

View File

@ -66,3 +66,11 @@ func (s *Server) DelClient(c *Client) {
}
}
}
func (s *Server) SendAll(msg *Message) {
s.clientsMutex.Lock()
defer s.clientsMutex.Unlock()
for _, c := range s.clients {
c.Write(msg)
}
}

View File

@ -5,6 +5,8 @@ import (
"net/http/httptest"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
@ -31,3 +33,45 @@ func TestServer(t *testing.T) {
srv.DelClient(nil)
srv.DelClient(c)
}
func TestServerSendAll(t *testing.T) {
assert := assert.New(t)
srv := NewServer(nil, nil)
assert.NotNil(srv)
out1 := make(chan *Message)
c1 := &Client{
id: uuid.New(),
out: out1,
server: srv,
}
out2 := make(chan *Message)
c2 := &Client{
id: uuid.New(),
out: out2,
server: srv,
}
srv.AddClient(c1)
srv.AddClient(c2)
go func() {
msg := <-out1
assert.Equal("hi", msg.Subject)
}()
go func() {
msg := <-out2
assert.Equal("hi", msg.Subject)
}()
srv.SendAll(&Message{
Subject: "hi",
})
srv.DelClient(c2)
srv.DelClient(c1)
}