mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-26 13:48:53 +08:00
NOISSUE - Add mutex to WebSocket service mock (#294)
This commit is contained in:
parent
c5a40aeffa
commit
64e71edf95
@ -60,8 +60,7 @@ func handshake(tsURL string, chanID uint64, token string, addHeader bool) (*webs
|
|||||||
header.Add("Authorization", token)
|
header.Add("Authorization", token)
|
||||||
}
|
}
|
||||||
url := makeURL(tsURL, chanID, token, addHeader)
|
url := makeURL(tsURL, chanID, token, addHeader)
|
||||||
conn, resp, err := websocket.DefaultDialer.Dial(url, header)
|
return websocket.DefaultDialer.Dial(url, header)
|
||||||
return conn, resp, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandshake(t *testing.T) {
|
func TestHandshake(t *testing.T) {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package mocks
|
package mocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/mainflux/mainflux"
|
"github.com/mainflux/mainflux"
|
||||||
"github.com/mainflux/mainflux/ws"
|
"github.com/mainflux/mainflux/ws"
|
||||||
)
|
)
|
||||||
@ -10,25 +12,30 @@ var _ ws.Service = (*mockService)(nil)
|
|||||||
type mockService struct {
|
type mockService struct {
|
||||||
subscriptions map[uint64]ws.Channel
|
subscriptions map[uint64]ws.Channel
|
||||||
pubError error
|
pubError error
|
||||||
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewService returns mock message publisher.
|
// NewService returns mock message publisher.
|
||||||
func NewService(subs map[uint64]ws.Channel, pubError error) ws.Service {
|
func NewService(subs map[uint64]ws.Channel, pubError error) ws.Service {
|
||||||
return mockService{subs, pubError}
|
return &mockService{subs, pubError, sync.Mutex{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc mockService) Publish(msg mainflux.RawMessage) error {
|
func (svc *mockService) Publish(msg mainflux.RawMessage) error {
|
||||||
if len(msg.Payload) == 0 {
|
if len(msg.Payload) == 0 {
|
||||||
return svc.pubError
|
return svc.pubError
|
||||||
}
|
}
|
||||||
|
svc.mutex.Lock()
|
||||||
svc.subscriptions[msg.Channel].Messages <- msg
|
svc.subscriptions[msg.Channel].Messages <- msg
|
||||||
|
svc.mutex.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (svc mockService) Subscribe(chanID uint64, channel ws.Channel) error {
|
func (svc *mockService) Subscribe(chanID uint64, channel ws.Channel) error {
|
||||||
if _, ok := svc.subscriptions[chanID]; !ok {
|
if _, ok := svc.subscriptions[chanID]; !ok {
|
||||||
return ws.ErrFailedSubscription
|
return ws.ErrFailedSubscription
|
||||||
}
|
}
|
||||||
|
svc.mutex.Lock()
|
||||||
svc.subscriptions[chanID] = channel
|
svc.subscriptions[chanID] = channel
|
||||||
|
svc.mutex.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user