Skip to content

Commit

Permalink
Add NATS test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
  • Loading branch information
darkodraskovic committed Jun 22, 2020
1 parent e64a026 commit c923b28
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions pkg/messaging/nats/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ import (
"fmt"
"testing"

"github.com/golang/protobuf/proto"
"github.com/mainflux/mainflux/pkg/messaging"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
topic = "topic"
data = "payload"
channel = "9b7b1b3f-b1b0-46a8-a717-b8213f9eda3b"
subtopic = "engine"
topic = "topic"
chansPrefix = "channels"
data = "payload"
channel = "9b7b1b3f-b1b0-46a8-a717-b8213f9eda3b"
subtopic = "engine"
)

var (
msgChan = make(chan messaging.Message)
)

func TestPubsub(t *testing.T) {
err := pubsub.Subscribe(topic, handler)
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 {
Expand Down Expand Up @@ -61,11 +63,14 @@ func TestPubsub(t *testing.T) {
}

for _, tc := range cases {
expectedMsg := message(tc.channel, tc.subtopic, tc.payload)
payload, err := payload(expectedMsg)
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, messaging.Message{Payload: payload})
err = pubsub.Publish(topic, expectedMsg)
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))

receivedMsg := <-msgChan
Expand All @@ -77,18 +82,3 @@ func handler(msg messaging.Message) error {
msgChan <- msg
return nil
}

func message(channel, subtopic string, payload []byte) messaging.Message {
return messaging.Message{
Channel: channel,
Subtopic: subtopic,
Payload: payload,
}
}
func payload(m messaging.Message) ([]byte, error) {
protoMsg, err := proto.Marshal(&m)
if err != nil {
return nil, err
}
return protoMsg, nil
}

0 comments on commit c923b28

Please sign in to comment.