1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Mainflux.mainflux/ws/mocks/messages.go
Ivan Milošević effade00aa MF-325 - Add SPDX license and copyright headers (#362)
* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Add SPDX license and copyright headers

Signed-off-by: Ivan Milošević <iva@blokovi.com>

* MF-325 - Change mainflux version from 0.4.0 to 0.5.0

Signed-off-by: Ivan Milošević <iva@blokovi.com>
2018-08-26 13:15:48 +02:00

50 lines
996 B
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package mocks
import (
"sync"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/ws"
)
var _ ws.Service = (*mockService)(nil)
type mockService struct {
subscriptions map[uint64]*ws.Channel
pubError error
mutex sync.Mutex
}
// NewService returns mock message publisher.
func NewService(subs map[uint64]*ws.Channel, pubError error) ws.Service {
return &mockService{subs, pubError, sync.Mutex{}}
}
func (svc *mockService) Publish(msg mainflux.RawMessage) error {
if len(msg.Payload) == 0 {
return svc.pubError
}
svc.mutex.Lock()
defer svc.mutex.Unlock()
svc.subscriptions[msg.Channel].Messages <- msg
return nil
}
func (svc *mockService) Subscribe(chanID uint64, channel *ws.Channel) error {
svc.mutex.Lock()
defer svc.mutex.Unlock()
if _, ok := svc.subscriptions[chanID]; !ok {
return ws.ErrFailedSubscription
}
svc.subscriptions[chanID] = channel
return nil
}