mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00

* NOISSUEE - Create broker package for NATS Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Create funcs to return NATS connection Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * mv os.exit to main Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix Reviews Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix tests and typos Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix CI Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix reviews Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Unify Publisher and Subscriber interfaces Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rename Nats interface Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Mv message.pb.go, messsage.proto and topics.go to broker directory Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix go.mod Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use mainflux broker for writers and twins services Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix go.mod Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix twins tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix make proto Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix message.proto Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix golangcibot Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * regenerate message.pb.go Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix comment Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix comment Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix make proto Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add NATS errors Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
119 lines
2.5 KiB
Go
119 lines
2.5 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package ws_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux/broker"
|
|
"github.com/mainflux/mainflux/ws"
|
|
"github.com/mainflux/mainflux/ws/mocks"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
const (
|
|
chanID = "1"
|
|
pubID = "1"
|
|
protocol = "ws"
|
|
)
|
|
|
|
var msg = broker.Message{
|
|
Channel: chanID,
|
|
Publisher: pubID,
|
|
Protocol: protocol,
|
|
Payload: []byte(`[{"n":"current","t":-5,"v":1.2}]`),
|
|
}
|
|
|
|
func newService(channel *ws.Channel) ws.Service {
|
|
subs := map[string]*ws.Channel{chanID: channel}
|
|
broker := mocks.New(subs)
|
|
return ws.New(broker, nil)
|
|
}
|
|
|
|
func TestPublish(t *testing.T) {
|
|
channel := ws.NewChannel()
|
|
svc := newService(channel)
|
|
|
|
cases := []struct {
|
|
desc string
|
|
msg broker.Message
|
|
err error
|
|
}{
|
|
{
|
|
desc: "publish valid message",
|
|
msg: msg,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "publish empty message",
|
|
msg: broker.Message{},
|
|
err: ws.ErrFailedMessagePublish,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
// Check if message was sent.
|
|
go func(desc string, tcMsg broker.Message) {
|
|
receivedMsg := <-channel.Messages
|
|
assert.Equal(t, tcMsg, receivedMsg, fmt.Sprintf("%s: expected %v got %v\n", desc, tcMsg, receivedMsg))
|
|
}(tc.desc, tc.msg)
|
|
|
|
// Check if publish succeeded.
|
|
err := svc.Publish(context.Background(), "", tc.msg)
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestSubscribe(t *testing.T) {
|
|
channel := ws.NewChannel()
|
|
svc := newService(channel)
|
|
|
|
cases := []struct {
|
|
desc string
|
|
chanID string
|
|
subtopic string
|
|
channel *ws.Channel
|
|
err error
|
|
}{
|
|
{
|
|
desc: "subscription to valid channel",
|
|
chanID: chanID,
|
|
channel: channel,
|
|
err: nil,
|
|
},
|
|
{
|
|
desc: "subscription to channel that should fail",
|
|
chanID: "0",
|
|
channel: channel,
|
|
err: ws.ErrFailedSubscription,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
err := svc.Subscribe(tc.chanID, tc.subtopic, tc.channel)
|
|
assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
|
|
}
|
|
}
|
|
|
|
func TestSend(t *testing.T) {
|
|
channel := ws.NewChannel()
|
|
go func(channel *ws.Channel) {
|
|
receivedMsg := <-channel.Messages
|
|
assert.Equal(t, msg, receivedMsg, fmt.Sprintf("send message to channel: expected %v got %v\n", msg, receivedMsg))
|
|
}(channel)
|
|
|
|
channel.Send(msg)
|
|
}
|
|
|
|
func TestClose(t *testing.T) {
|
|
channel := ws.NewChannel()
|
|
go func() {
|
|
closed := <-channel.Closed
|
|
assert.True(t, closed, "channel closed stayed open")
|
|
}()
|
|
channel.Close()
|
|
}
|