mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00
feat: add mqtt mock client (#245)
Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
parent
0cab7d5878
commit
1d41d050fe
@ -17,13 +17,14 @@ import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
elements "github.com/rddl-network/elements-rpc"
|
||||
"github.com/rddl-network/elements-rpc/utils/mocks"
|
||||
elementsmocks "github.com/rddl-network/elements-rpc/utils/mocks"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/planetmint/planetmint-go/app"
|
||||
"github.com/planetmint/planetmint-go/config"
|
||||
"github.com/planetmint/planetmint-go/lib"
|
||||
"github.com/planetmint/planetmint-go/util"
|
||||
"github.com/planetmint/planetmint-go/util/mocks"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -46,7 +47,8 @@ func New(t *testing.T, configs ...Config) *Network {
|
||||
validatorTmpDir := t.TempDir()
|
||||
|
||||
// use mock client for testing
|
||||
elements.Client = &mocks.MockClient{}
|
||||
util.MQTTClient = &mocks.MockMQTTClient{}
|
||||
elements.Client = &elementsmocks.MockClient{}
|
||||
|
||||
// enable application logger in tests
|
||||
appLogger := util.GetAppLogger()
|
||||
|
43
util/mocks/mqtt.go
Normal file
43
util/mocks/mqtt.go
Normal file
@ -0,0 +1,43 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
)
|
||||
|
||||
// MockMQTTClient is the mock mqtt client
|
||||
type MockMQTTClient struct {
|
||||
ConnectFunc func() mqtt.Token
|
||||
DisconnectFunc func(quiesce uint)
|
||||
PublishFunc func(topic string, qos byte, retained bool, payload interface{}) mqtt.Token
|
||||
}
|
||||
|
||||
// GetConnectFunc fetches the mock client's `Connect` func
|
||||
func GetConnectFunc() mqtt.Token {
|
||||
token := mqtt.DummyToken{}
|
||||
return &token
|
||||
}
|
||||
|
||||
// GetDisconnectFunc fetches the mock client's `Disconnect` func
|
||||
func GetDisconnectFunc(_ uint) {
|
||||
}
|
||||
|
||||
// GetPublishFunc fetches the mock client's `Publish` func
|
||||
func GetPublishFunc(_ string, _ byte, _ bool, _ interface{}) mqtt.Token {
|
||||
token := mqtt.DummyToken{}
|
||||
return &token
|
||||
}
|
||||
|
||||
// Connect is the mock client's `Disconnect` func
|
||||
func (m *MockMQTTClient) Connect() mqtt.Token {
|
||||
return GetConnectFunc()
|
||||
}
|
||||
|
||||
// Disconnect is the mock client's `Disconnect` func
|
||||
func (m *MockMQTTClient) Disconnect(quiesce uint) {
|
||||
GetDisconnectFunc(quiesce)
|
||||
}
|
||||
|
||||
// Publish is the mock client's `Publish` func
|
||||
func (m *MockMQTTClient) Publish(topic string, qos byte, retained bool, payload interface{}) mqtt.Token {
|
||||
return GetPublishFunc(topic, qos, retained, payload)
|
||||
}
|
37
util/mqtt.go
37
util/mqtt.go
@ -11,14 +11,18 @@ import (
|
||||
"github.com/planetmint/planetmint-go/x/dao/types"
|
||||
)
|
||||
|
||||
func SendMqttMessagesToServer(ctx sdk.Context, challenge types.Challenge) {
|
||||
err := sendMqttMessages(challenge)
|
||||
if err != nil {
|
||||
GetAppLogger().Error(ctx, "MQTT error: "+err.Error())
|
||||
}
|
||||
// MQTTClientI interface
|
||||
type MQTTClientI interface {
|
||||
Connect() mqtt.Token
|
||||
Disconnect(quiesce uint)
|
||||
Publish(topic string, qos byte, retained bool, payload interface{}) mqtt.Token
|
||||
}
|
||||
|
||||
func sendMqttMessages(challenge types.Challenge) (err error) {
|
||||
var (
|
||||
MQTTClient MQTTClientI
|
||||
)
|
||||
|
||||
func init() {
|
||||
conf := config.GetConfig()
|
||||
hostPort := net.JoinHostPort(conf.MqttDomain, strconv.FormatInt(int64(conf.MqttPort), 10))
|
||||
uri := fmt.Sprintf("tcp://%s", hostPort)
|
||||
@ -27,27 +31,38 @@ func sendMqttMessages(challenge types.Challenge) (err error) {
|
||||
opts.SetClientID(conf.ValidatorAddress)
|
||||
opts.SetUsername(conf.MqttUser)
|
||||
opts.SetPassword(conf.MqttPassword)
|
||||
client := mqtt.NewClient(opts)
|
||||
MQTTClient = mqtt.NewClient(opts)
|
||||
}
|
||||
|
||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||
func SendMqttMessagesToServer(ctx sdk.Context, challenge types.Challenge) {
|
||||
err := sendMqttMessages(challenge)
|
||||
if err != nil {
|
||||
GetAppLogger().Error(ctx, "MQTT error: "+err.Error())
|
||||
return
|
||||
}
|
||||
GetAppLogger().Info(ctx, "MQTT message successfully sent")
|
||||
}
|
||||
|
||||
func sendMqttMessages(challenge types.Challenge) (err error) {
|
||||
if token := MQTTClient.Connect(); token.Wait() && token.Error() != nil {
|
||||
err = token.Error()
|
||||
return
|
||||
}
|
||||
blockHeight := strconv.FormatInt(challenge.GetHeight(), 10)
|
||||
token := client.Publish("cmnd/"+challenge.GetChallengee()+"/PoPInit", 0, false, blockHeight)
|
||||
token := MQTTClient.Publish("cmnd/"+challenge.GetChallengee()+"/PoPInit", 0, false, blockHeight)
|
||||
token.Wait()
|
||||
err = token.Error()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
token = client.Publish("cmnd/"+challenge.GetChallenger()+"/PoPInit", 0, false, blockHeight)
|
||||
token = MQTTClient.Publish("cmnd/"+challenge.GetChallenger()+"/PoPInit", 0, false, blockHeight)
|
||||
token.Wait()
|
||||
err = token.Error()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client.Disconnect(1000)
|
||||
MQTTClient.Disconnect(1000)
|
||||
return
|
||||
}
|
||||
|
@ -3,20 +3,20 @@ package util
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/planetmint/planetmint-go/util/mocks"
|
||||
"github.com/planetmint/planetmint-go/x/dao/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSendMqttMessages(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Skip("Skip this test case as this test case expects a working MQTT connection" +
|
||||
"the test case is intended to work manually.")
|
||||
|
||||
var challenge types.Challenge
|
||||
challenge.Initiator = ""
|
||||
challenge.Challengee = "plmnt15gdanx0nm2lwsx30a6wft7429p32dhzaq37c06"
|
||||
challenge.Challenger = "plmnt1683t0us0r85840nsepx6jrk2kjxw7zrcnkf0rp"
|
||||
challenge.Height = 58
|
||||
// Use MQTT mock client
|
||||
MQTTClient = &mocks.MockMQTTClient{}
|
||||
err := sendMqttMessages(challenge)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user