[mqttdevice] Fix driveMode conversion and cyclic dependencies
This commit is contained in:
parent
ef6edec672
commit
a0dedb056b
1
go.sum
1
go.sum
@ -68,6 +68,7 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
|
||||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
|
||||
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
|
@ -96,8 +96,6 @@ func NewMqttValue(v interface{}) MqttValue {
|
||||
return MqttValue(fmt.Sprintf("%0.2f", val))
|
||||
case int, int8, int16, int32, int64:
|
||||
return MqttValue(fmt.Sprintf("%d", val))
|
||||
case mode.DriveMode:
|
||||
return MqttValue(mode.ToString(val))
|
||||
case bool:
|
||||
if val {
|
||||
return []byte("ON")
|
||||
@ -130,6 +128,13 @@ func (m *MqttValue) Float64Value() (float64, error) {
|
||||
func (m *MqttValue) StringValue() (string, error) {
|
||||
return string(*m), nil
|
||||
}
|
||||
func (m *MqttValue) DriveModeValue() (mode.DriveMode, error) {
|
||||
val, err := m.IntValue()
|
||||
if err != nil {
|
||||
return mode.DriveModeInvalid, err
|
||||
}
|
||||
return mode.DriveMode(val), nil
|
||||
}
|
||||
func (m *MqttValue) ByteSliceValue() ([]byte, error) {
|
||||
return *m, nil
|
||||
}
|
||||
|
@ -1,14 +1,15 @@
|
||||
package mqttdevice
|
||||
|
||||
import (
|
||||
"github.com/cyrilix/robocar-base/testtools"
|
||||
"github.com/cyrilix/robocar-base/mode"
|
||||
"github.com/cyrilix/robocar-base/testtools/docker"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIntegration(t *testing.T) {
|
||||
|
||||
ctx, mqttC, mqttUri := testtools.MqttContainer(t)
|
||||
ctx, mqttC, mqttUri := docker.MqttContainer(t)
|
||||
defer mqttC.Terminate(ctx)
|
||||
|
||||
t.Run("ConnectAndClose", func(t *testing.T) {
|
||||
@ -187,3 +188,23 @@ func TestMqttValue_StringValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMqttValue_DriveModeValue(t *testing.T) {
|
||||
cases := []struct {
|
||||
value MqttValue
|
||||
expected mode.DriveMode
|
||||
}{
|
||||
{NewMqttValue(mode.DriveModeUser), mode.DriveModeUser},
|
||||
{NewMqttValue(mode.DriveModePilot), mode.DriveModePilot},
|
||||
{NewMqttValue(mode.DriveModeInvalid), mode.DriveModeInvalid},
|
||||
}
|
||||
for _, c := range cases {
|
||||
val, err := c.value.DriveModeValue()
|
||||
if err != nil {
|
||||
t.Errorf("unexpected conversion error: %v", err)
|
||||
}
|
||||
if c.expected != val {
|
||||
t.Errorf("MqttValue.DriveMode(): %v, wants %v", val, c.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
37
testtools/docker/docker.go
Normal file
37
testtools/docker/docker.go
Normal file
@ -0,0 +1,37 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func MqttContainer(t *testing.T) (context.Context, testcontainers.Container, string) {
|
||||
ctx := context.Background()
|
||||
req := testcontainers.ContainerRequest{
|
||||
Image: "eclipse-mosquitto",
|
||||
ExposedPorts: []string{"1883/tcp"},
|
||||
WaitingFor: wait.ForLog("listen socket on port 1883."),
|
||||
}
|
||||
mqttC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
||||
ContainerRequest: req,
|
||||
Started: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
ip, err := mqttC.Host(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
port, err := mqttC.MappedPort(ctx, "1883/tcp")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
mqttUri := fmt.Sprintf("tcp://%s:%d", ip, port.Int())
|
||||
return ctx, mqttC, mqttUri
|
||||
}
|
@ -1,43 +1,11 @@
|
||||
package testtools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/cyrilix/robocar-base/mqttdevice"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func MqttContainer(t *testing.T) (context.Context, testcontainers.Container, string) {
|
||||
ctx := context.Background()
|
||||
req := testcontainers.ContainerRequest{
|
||||
Image: "eclipse-mosquitto",
|
||||
ExposedPorts: []string{"1883/tcp"},
|
||||
WaitingFor: wait.ForLog("listen socket on port 1883."),
|
||||
}
|
||||
mqttC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
||||
ContainerRequest: req,
|
||||
Started: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
ip, err := mqttC.Host(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
port, err := mqttC.MappedPort(ctx, "1883/tcp")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
mqttUri := fmt.Sprintf("tcp://%s:%d", ip, port.Int())
|
||||
return ctx, mqttC, mqttUri
|
||||
}
|
||||
|
||||
func NewFakePublisher() *FakePublisher {
|
||||
return &FakePublisher{msg: make(map[string]mqttdevice.MqttValue)}
|
||||
|
Loading…
Reference in New Issue
Block a user