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

* Add test setup for mqtt Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add pubsub tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add message and payload helper funcs Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add time.Sleep after subscribe to topic Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add NATS tests setup Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add NATS test cases Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add message forwarded check to handler Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove race affected mqtt tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package nats_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mainflux/mainflux/pkg/messaging"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
topic = "topic"
|
|
chansPrefix = "channels"
|
|
channel = "9b7b1b3f-b1b0-46a8-a717-b8213f9eda3b"
|
|
subtopic = "engine"
|
|
)
|
|
|
|
var (
|
|
msgChan = make(chan messaging.Message)
|
|
data = []byte("payload")
|
|
)
|
|
|
|
func TestPubsub(t *testing.T) {
|
|
err := pubsub.Subscribe(fmt.Sprintf("%s.%s", chansPrefix, topic), handler)
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
err = pubsub.Subscribe(fmt.Sprintf("%s.%s.%s", chansPrefix, topic, subtopic), handler)
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
|
|
cases := []struct {
|
|
desc string
|
|
channel string
|
|
subtopic string
|
|
payload []byte
|
|
}{
|
|
{
|
|
desc: "publish message with nil payload",
|
|
payload: nil,
|
|
},
|
|
{
|
|
desc: "publish message with string payload",
|
|
payload: data,
|
|
},
|
|
{
|
|
desc: "publish message with channel",
|
|
payload: data,
|
|
channel: channel,
|
|
},
|
|
{
|
|
desc: "publish message with subtopic",
|
|
payload: data,
|
|
subtopic: subtopic,
|
|
},
|
|
{
|
|
desc: "publish message with channel and subtopic",
|
|
payload: data,
|
|
channel: channel,
|
|
subtopic: subtopic,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
expectedMsg := messaging.Message{
|
|
Channel: tc.channel,
|
|
Subtopic: tc.subtopic,
|
|
Payload: tc.payload,
|
|
}
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
|
|
err = pubsub.Publish(topic, expectedMsg)
|
|
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
|
|
|
|
receivedMsg := <-msgChan
|
|
assert.Equal(t, expectedMsg, receivedMsg, fmt.Sprintf("%s: expected %+v got %+v\n", tc.desc, expectedMsg, receivedMsg))
|
|
}
|
|
}
|
|
|
|
func handler(msg messaging.Message) error {
|
|
msgChan <- msg
|
|
return nil
|
|
}
|