robocar-steering/pkg/steering/controller_test.go

126 lines
3.9 KiB
Go
Raw Normal View History

2022-08-20 19:33:45 +00:00
package steering
2020-02-08 21:35:47 +00:00
import (
"github.com/cyrilix/robocar-base/testtools"
"github.com/cyrilix/robocar-protobuf/go/events"
mqtt "github.com/eclipse/paho.mqtt.golang"
2022-01-03 17:07:56 +00:00
"google.golang.org/protobuf/proto"
2020-02-08 21:35:47 +00:00
"sync"
"testing"
"time"
)
func TestDefaultSteering(t *testing.T) {
oldRegister := registerCallbacks
oldPublish := publish
defer func() {
registerCallbacks = oldRegister
publish = oldPublish
}()
2022-08-20 19:38:19 +00:00
registerCallbacks = func(p *Controller) error {
2020-02-08 21:35:47 +00:00
return nil
}
var muEventsPublished sync.Mutex
eventsPublished := make(map[string][]byte)
publish = func(client mqtt.Client, topic string, payload *[]byte) {
muEventsPublished.Lock()
defer muEventsPublished.Unlock()
eventsPublished[topic] = *payload
}
steeringTopic := "topic/steering"
driveModeTopic := "topic/driveMode"
rcSteeringTopic := "topic/rcSteering"
tfSteeringTopic := "topic/tfSteering"
2022-08-22 11:12:12 +00:00
objectsTopic := "topic/objects"
2020-02-08 21:35:47 +00:00
2022-08-22 11:12:12 +00:00
p := NewController(nil, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic, objectsTopic)
2020-02-08 21:35:47 +00:00
cases := []struct {
driveMode events.DriveModeMessage
rcSteering events.SteeringMessage
tfSteering events.SteeringMessage
expectedSteering events.SteeringMessage
2022-08-22 11:12:12 +00:00
objects events.ObjectsMessage
2020-02-08 21:35:47 +00:00
}{
{
events.DriveModeMessage{DriveMode: events.DriveMode_USER},
events.SteeringMessage{Steering: 0.3, Confidence: 1.0},
events.SteeringMessage{Steering: 0.4, Confidence: 1.0},
events.SteeringMessage{Steering: 0.3, Confidence: 1.0},
2022-08-22 11:12:12 +00:00
events.ObjectsMessage{},
2020-02-08 21:35:47 +00:00
},
{
events.DriveModeMessage{DriveMode: events.DriveMode_PILOT},
events.SteeringMessage{Steering: 0.5, Confidence: 1.0},
events.SteeringMessage{Steering: 0.6, Confidence: 1.0},
events.SteeringMessage{Steering: 0.6, Confidence: 1.0},
2022-08-22 11:12:12 +00:00
events.ObjectsMessage{},
2020-02-08 21:35:47 +00:00
},
{
events.DriveModeMessage{DriveMode: events.DriveMode_PILOT},
events.SteeringMessage{Steering: 0.4, Confidence: 1.0},
events.SteeringMessage{Steering: 0.7, Confidence: 1.0},
events.SteeringMessage{Steering: 0.7, Confidence: 1.0},
2022-08-22 11:12:12 +00:00
events.ObjectsMessage{},
2020-02-08 21:35:47 +00:00
},
{
events.DriveModeMessage{DriveMode: events.DriveMode_USER},
events.SteeringMessage{Steering: 0.5, Confidence: 1.0},
events.SteeringMessage{Steering: 0.8, Confidence: 1.0},
events.SteeringMessage{Steering: 0.5, Confidence: 1.0},
2022-08-22 11:12:12 +00:00
events.ObjectsMessage{},
2020-02-08 21:35:47 +00:00
},
{
events.DriveModeMessage{DriveMode: events.DriveMode_USER},
events.SteeringMessage{Steering: 0.4, Confidence: 1.0},
events.SteeringMessage{Steering: 0.9, Confidence: 1.0},
events.SteeringMessage{Steering: 0.4, Confidence: 1.0},
2022-08-22 11:12:12 +00:00
events.ObjectsMessage{},
2020-02-08 21:35:47 +00:00
},
{
events.DriveModeMessage{DriveMode: events.DriveMode_USER},
events.SteeringMessage{Steering: 0.6, Confidence: 1.0},
events.SteeringMessage{Steering: -0.3, Confidence: 1.0},
events.SteeringMessage{Steering: 0.6, Confidence: 1.0},
2022-08-22 11:12:12 +00:00
events.ObjectsMessage{},
2020-02-08 21:35:47 +00:00
},
}
go p.Start()
defer func() { close(p.cancel) }()
for _, c := range cases {
p.onDriveMode(nil, testtools.NewFakeMessageFromProtobuf(driveModeTopic, &c.driveMode))
p.onRCSteering(nil, testtools.NewFakeMessageFromProtobuf(rcSteeringTopic, &c.rcSteering))
p.onTFSteering(nil, testtools.NewFakeMessageFromProtobuf(tfSteeringTopic, &c.tfSteering))
2022-08-22 11:12:12 +00:00
p.onObjects(nil, testtools.NewFakeMessageFromProtobuf(objectsTopic, &c.objects))
2020-02-08 21:35:47 +00:00
time.Sleep(10 * time.Millisecond)
for i := 3; i >= 0; i-- {
var msg events.SteeringMessage
muEventsPublished.Lock()
err := proto.Unmarshal(eventsPublished[steeringTopic], &msg)
if err != nil {
t.Errorf("unable to unmarshall response: %v", err)
t.Fail()
}
muEventsPublished.Unlock()
if msg.GetSteering() != c.expectedSteering.GetSteering() {
t.Errorf("bad msg value for mode %v: %v, wants %v",
2022-08-20 19:38:19 +00:00
c.driveMode.String(), msg.GetSteering(), c.expectedSteering.GetSteering())
2020-02-08 21:35:47 +00:00
}
if msg.GetConfidence() != 1. {
t.Errorf("bad throtlle confidence: %v, wants %v", msg.GetConfidence(), 1.)
}
time.Sleep(1 * time.Millisecond)
}
}
}