Skip to content

Commit

Permalink
mqtt client test
Browse files Browse the repository at this point in the history
  • Loading branch information
BlenderistDev committed Sep 5, 2024
1 parent b5f8083 commit 0aad2ff
Show file tree
Hide file tree
Showing 5 changed files with 508 additions and 3 deletions.
17 changes: 14 additions & 3 deletions internal/clients/mqtt/client.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
package mqtt

import (
"fmt"
"time"

mqtt "github.com/eclipse/paho.mqtt.golang"
)

//go:generate mockgen -source=client.go -destination=../../../test/mocks/gomock/clients/mqtt/client.go

type logger interface {
Error(msg string, args ...any)
Info(msg string, args ...any)
Debug(msg string, args ...any)
}

type mqttClient interface {
Connect() mqtt.Token
Publish(topic string, qos byte, retained bool, payload interface{}) mqtt.Token
Subscribe(topic string, qos byte, callback mqtt.MessageHandler) mqtt.Token
}

// Client mqtt client.
type Client struct {
topicPrefix string
client mqtt.Client
client mqttClient
logger logger
broker string
}

// NewClient creates new Client.
func NewClient(broker, clientID, username, password string, log logger) *Client {
opts := mqtt.
NewClientOptions().
Expand All @@ -39,6 +48,7 @@ func NewClient(broker, clientID, username, password string, log logger) *Client
}
}

// Connect connection to mqtt broker.
func (c *Client) Connect() error {
if token := c.client.Connect(); token.Wait() && token.Error() != nil {
return token.Error()
Expand All @@ -48,6 +58,7 @@ func (c *Client) Connect() error {
return nil
}

// SendMessage sends mqtt message.
func (c *Client) SendMessage(topic, message string, retained bool) {
c.logger.Debug("start sending mqtt message",
"topic", topic,
Expand All @@ -74,10 +85,10 @@ func (c *Client) SendMessage(topic, message string, retained bool) {
)
}

// Subscribe subscribes to topic.
func (c *Client) Subscribe(topic string) chan string {
ch := make(chan string)
c.client.Subscribe(topic, 0, func(client mqtt.Client, message mqtt.Message) {
fmt.Println(topic, message)
ch <- string(message.Payload())
})

Expand Down
216 changes: 216 additions & 0 deletions internal/clients/mqtt/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package mqtt

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
mock_mqtt "keeneticToMqtt/test/mocks/gomock/clients/mqtt"
mock_mqtttoken "keeneticToMqtt/test/mocks/gomock/clients/mqtt/token"
)

func TestClient_SendMessage(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

const (
prefix = "prefix"
topic = "topic"
message = "message"
retained = true
)

someErr := errors.New("some err")

tests := []struct {
name string
mqttClient func() mqttClient
logger func() logger
}{
{
name: "success publish",
logger: func() logger {
log := mock_mqtt.NewMocklogger(ctrl)
log.EXPECT().Debug("start sending mqtt message",
"topic", topic,
"message", message,
"retained", retained,
)
log.EXPECT().Info("sending mqtt message",
"topic", topic,
"message", message,
"retained", retained,
)

return log
},
mqttClient: func() mqttClient {
ch := make(chan struct{})
go func() {
ch <- struct{}{}
}()

token := mock_mqtttoken.NewMockToken(ctrl)
token.EXPECT().Done().Return(ch)
token.EXPECT().Error().Return(nil)

client := mock_mqtt.NewMockmqttClient(ctrl)
client.EXPECT().Publish(topic, byte(0), retained, message).Return(token)

return client
},
},
{
name: "publish error",
logger: func() logger {
log := mock_mqtt.NewMocklogger(ctrl)
log.EXPECT().Debug("start sending mqtt message",
"topic", topic,
"message", message,
"retained", retained,
)
log.EXPECT().Error("error sending mqtt message",
"error", someErr,
"topic", topic,
"message", message,
"retained", retained,
)

return log
},
mqttClient: func() mqttClient {
ch := make(chan struct{})
go func() {
ch <- struct{}{}
}()

token := mock_mqtttoken.NewMockToken(ctrl)
token.EXPECT().Done().Return(ch)
token.EXPECT().Error().Return(someErr)

client := mock_mqtt.NewMockmqttClient(ctrl)
client.EXPECT().Publish(topic, byte(0), retained, message).Return(token)

return client
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

mqtt := Client{
topicPrefix: prefix,
client: tt.mqttClient(),
logger: tt.logger(),
broker: "",
}

mqtt.SendMessage(topic, message, retained)
})
}
}

func TestClient_Connect(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

const (
broker = "broker"
)

someErr := errors.New("some err")

tests := []struct {
name string
mqttClient func() mqttClient
logger func() logger
expectedErr error
}{
{
name: "success connect",
logger: func() logger {
log := mock_mqtt.NewMocklogger(ctrl)
log.EXPECT().Info("connected to mqtt", "broker", broker)

return log
},
mqttClient: func() mqttClient {
ch := make(chan struct{})
go func() {
ch <- struct{}{}
}()

token := mock_mqtttoken.NewMockToken(ctrl)
token.EXPECT().Wait().Return(true)
token.EXPECT().Error().Return(nil)

client := mock_mqtt.NewMockmqttClient(ctrl)
client.EXPECT().Connect().Return(token)

return client
},
},
{
name: "error while connect",
logger: func() logger {
log := mock_mqtt.NewMocklogger(ctrl)
return log
},
mqttClient: func() mqttClient {
ch := make(chan struct{})
go func() {
ch <- struct{}{}
}()

token := mock_mqtttoken.NewMockToken(ctrl)
token.EXPECT().Wait().Return(true)
token.EXPECT().Error().Return(someErr)
token.EXPECT().Error().Return(someErr)

client := mock_mqtt.NewMockmqttClient(ctrl)
client.EXPECT().Connect().Return(token)

return client
},
expectedErr: someErr,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

mqtt := Client{
client: tt.mqttClient(),
logger: tt.logger(),
broker: broker,
}

err := mqtt.Connect()
if tt.expectedErr == nil {
assert.Nil(t, err)
} else {
assert.ErrorIs(t, err, tt.expectedErr)
}
})
}
}

func TestClient_Subscribe(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

const (
topic = "topic"
)

client := mock_mqtt.NewMockmqttClient(ctrl)
client.EXPECT().Subscribe(topic, byte(0), gomock.Any())

mqtt := Client{
client: client,
}

mqtt.Subscribe(topic)
}
Loading

0 comments on commit 0aad2ff

Please sign in to comment.