[testtools] Add utility method to generate fake mqtt message
This commit is contained in:
parent
abefe81090
commit
ae323d1690
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/cyrilix/robocar-base/mqttdevice"
|
"github.com/cyrilix/robocar-base/mqttdevice"
|
||||||
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
"github.com/testcontainers/testcontainers-go"
|
"github.com/testcontainers/testcontainers-go"
|
||||||
"github.com/testcontainers/testcontainers-go/wait"
|
"github.com/testcontainers/testcontainers-go/wait"
|
||||||
"sync"
|
"sync"
|
||||||
@ -53,8 +54,53 @@ func (f *FakePublisher) Publish(topic string, payload mqttdevice.MqttValue) {
|
|||||||
f.msg[topic] = payload
|
f.msg[topic] = payload
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f* FakePublisher) PublishedEvent(topic string) mqttdevice.MqttValue{
|
func (f *FakePublisher) PublishedEvent(topic string) mqttdevice.MqttValue {
|
||||||
f.muMsg.Lock()
|
f.muMsg.Lock()
|
||||||
defer f.muMsg.Unlock()
|
defer f.muMsg.Unlock()
|
||||||
return f.msg[topic]
|
return f.msg[topic]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type fakeMessage struct {
|
||||||
|
qos byte
|
||||||
|
topic string
|
||||||
|
payload []byte
|
||||||
|
acked bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMessage) Duplicate() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMessage) Qos() byte {
|
||||||
|
return f.qos
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMessage) Retained() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMessage) Topic() string {
|
||||||
|
return f.topic
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMessage) MessageID() uint16 {
|
||||||
|
return 1234
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMessage) Payload() []byte {
|
||||||
|
return f.payload
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeMessage) Ack() {
|
||||||
|
f.acked = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFakeMessage(topic string, payload []byte) mqtt.Message {
|
||||||
|
return &fakeMessage{
|
||||||
|
qos: 0,
|
||||||
|
topic: topic,
|
||||||
|
payload: payload,
|
||||||
|
acked: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -14,11 +14,11 @@ func TestFakePublisher_Publish(t *testing.T) {
|
|||||||
value mqttdevice.MqttValue
|
value mqttdevice.MqttValue
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{"test/topic1", "test/topic1", mqttdevice.NewMqttValue(1) , "1" },
|
{"test/topic1", "test/topic1", mqttdevice.NewMqttValue(1), "1"},
|
||||||
{"test/topic2", "test/invalid", mqttdevice.NewMqttValue(1) , "" },
|
{"test/topic2", "test/invalid", mqttdevice.NewMqttValue(1), ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases{
|
for _, c := range cases {
|
||||||
p.Publish(c.topic, c.value)
|
p.Publish(c.topic, c.value)
|
||||||
val := p.PublishedEvent(c.topicPublished)
|
val := p.PublishedEvent(c.topicPublished)
|
||||||
if v, _ := val.StringValue(); v != c.expected {
|
if v, _ := val.StringValue(); v != c.expected {
|
||||||
|
Loading…
Reference in New Issue
Block a user