1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Mainflux.mainflux/ws/adapter_test.go
Nikola Marčetić 42b3682352
MF-415 - Merge mProxy support (#1045)
* NOISSUE - Add mProxy support (#1017)

* Add mproxy

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>

* Fix docker and add EMQ compose

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>

* Fix EMQX name

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>

* Add nats, auth and es

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>

* Removed unucessary vendoring

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Update vendoring

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Fix mproxy interface implementation

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

 NOISSUE - Aligned Event interface method signatures with new spec (#1025)

* Aligned Event interface method signatures with new spec

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

* Updated deps

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

NOISSUE - Update mproxy dependency (#1038)

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Update Vendor with new mProxy (#1043)

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>

Twins merge conflict reverted

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Twins merge conflict reverted

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Twins fixed nats import

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Update deps

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

* Resolved GolangCI remarks

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Resolved GolangCI remarks

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

Resolved GolangCI remarks

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

* Fixed Event interface Unsubscribe() typo

Signed-off-by: Nikola Marcetic <n.marcetic86@gmail.com>

* Update vendors

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>

* Upgrade CI script

Signed-off-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>

Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2020-02-26 17:14:16 +01:00

121 lines
2.6 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package ws_test
import (
"context"
"fmt"
"testing"
"github.com/mainflux/mainflux/ws"
"github.com/mainflux/mainflux/ws/mocks"
broker "github.com/nats-io/nats.go"
"github.com/stretchr/testify/assert"
"github.com/mainflux/mainflux"
)
const (
chanID = "1"
pubID = "1"
protocol = "ws"
)
var msg = mainflux.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}
pubsub := mocks.NewService(subs, broker.ErrInvalidMsg)
return ws.New(pubsub)
}
func TestPublish(t *testing.T) {
channel := ws.NewChannel()
svc := newService(channel)
cases := []struct {
desc string
msg mainflux.Message
err error
}{
{
desc: "publish valid message",
msg: msg,
err: nil,
},
{
desc: "publish empty message",
msg: mainflux.Message{},
err: ws.ErrFailedMessagePublish,
},
}
for _, tc := range cases {
// Check if message was sent.
go func(desc string, tcMsg mainflux.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()
}