robocar-simulator/pkg/gateway/controls_test.go

286 lines
6.7 KiB
Go
Raw Normal View History

package gateway
2020-11-29 17:42:33 +00:00
import (
"github.com/cyrilix/robocar-protobuf/go/events"
"github.com/cyrilix/robocar-simulator/pkg/simulator"
2020-11-29 17:42:33 +00:00
"testing"
2021-02-16 16:46:10 +00:00
"time"
2020-11-29 17:42:33 +00:00
)
2021-02-16 16:46:10 +00:00
func TestGateway_Start(t *testing.T) {
simulatorMock := Gw2SimMock{}
err := simulatorMock.listen()
if err != nil {
t.Errorf("unable to start mock gw: %v", err)
}
defer func() {
if err := simulatorMock.Close(); err != nil {
t.Errorf("unable to stop simulator mock: %v", err)
}
}()
carConfig := simulator.CarConfigMsg{
MsgType: simulator.MsgTypeCarConfig,
BodyStyle: simulator.CarConfigBodyStyleCar01,
CarName: "car-test",
}
camConfig :=simulator.CamConfigMsg{
MsgType: simulator.MsgTypeCameraConfig,
Fov: "0",
FishEyeX: "0",
FishEyeY: "0",
ImgW: "120",
ImgH: "50",
ImgD: "0",
ImgEnc: simulator.CameraImageEncJpeg,
OffsetX: "0",
OffsetY: "0",
OffsetZ: "0",
RotX: "0",
}
gw, err := New(simulatorMock.Addr(),
2021-02-16 16:46:10 +00:00
&carConfig,
&simulator.RacerBioMsg{},
&camConfig,
)
if err != nil {
t.Fatalf("unable to init simulator gateway: %v", err)
}
go gw.Start()
defer gw.Close()
carNotify := simulatorMock.NotifyCar()
select {
case <- time.Tick(500 * time.Millisecond):
t.Errorf("a car configuration message should be send")
case msg := <-carNotify:
if *msg != carConfig {
t.Errorf("[carConfig] invalid car config: %v, wants %v", &msg, carConfig)
}
}
camNotify := simulatorMock.NotifyCamera()
select {
case <- time.Tick(500 * time.Millisecond):
t.Errorf("a camera configuration message should be send")
case msg := <-camNotify:
if *msg != camConfig {
t.Errorf("[carConfig] invalid camera config: %v, wants %v", &msg, camConfig)
}
}
}
2020-11-29 17:42:33 +00:00
func TestGateway_WriteSteering(t *testing.T) {
cases := []struct {
name string
msg *events.SteeringMessage
previousMsg *simulator.ControlMsg
expectedMsg simulator.ControlMsg
}{
{"First Message",
&events.SteeringMessage{Steering: 0.5, Confidence: 1},
nil,
simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.50",
Throttle: "0.0",
Brake: "0.0",
2020-11-29 17:42:33 +00:00
}},
{"Update steering",
&events.SteeringMessage{Steering: -0.5, Confidence: 1},
&simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.0",
Brake: "0.0",
2020-11-29 17:42:33 +00:00
},
simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "-0.50",
Throttle: "0.0",
Brake: "0.0",
2020-11-29 17:42:33 +00:00
}},
{"Update steering shouldn't erase throttle value",
&events.SteeringMessage{Steering: -0.3, Confidence: 1},
&simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.6",
Brake: "0.1",
2020-11-29 17:42:33 +00:00
},
simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "-0.30",
Throttle: "0.6",
Brake: "0.1",
2020-11-29 17:42:33 +00:00
}},
}
simulatorMock := Gw2SimMock{}
2020-11-29 17:42:33 +00:00
err := simulatorMock.listen()
if err != nil {
t.Errorf("unable to start mock gw: %v", err)
2020-11-29 17:42:33 +00:00
}
defer func() {
2020-11-29 17:42:33 +00:00
if err := simulatorMock.Close(); err != nil {
t.Errorf("unable to stop simulator mock: %v", err)
}
}()
gw, err := New(simulatorMock.Addr(),
2021-02-16 16:46:10 +00:00
&simulator.CarConfigMsg{MsgType: simulator.MsgTypeCarConfig},
&simulator.RacerBioMsg{},
&simulator.CamConfigMsg{
ImgH: "128",
ImgW: "160",
})
2020-11-29 17:42:33 +00:00
if err != nil {
t.Fatalf("unable to init simulator gateway: %v", err)
}
2021-02-16 16:46:10 +00:00
//go gw.Start()
//defer gw.Close()
//<- simulatorMock.NotifyCar()
//<- simulatorMock.NotifyCamera()
2020-11-29 17:42:33 +00:00
for _, c := range cases {
gw.lastControl = c.previousMsg
2020-11-29 17:42:33 +00:00
gw.WriteSteering(c.msg)
2020-11-29 17:42:33 +00:00
2021-02-16 16:46:10 +00:00
ctrlMsg := <-simulatorMock.NotifyCtrl()
2020-11-29 17:42:33 +00:00
if *ctrlMsg != c.expectedMsg {
t.Errorf("[%v] bad messge received: %#v, wants %#v", c.name, ctrlMsg, c.expectedMsg)
}
}
}
func TestGateway_WriteThrottle(t *testing.T) {
cases := []struct {
name string
msg *events.ThrottleMessage
previousMsg *simulator.ControlMsg
expectedMsg simulator.ControlMsg
}{
{"First Message",
&events.ThrottleMessage{Throttle: 0.5, Confidence: 1},
nil,
simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.0",
Throttle: "0.50",
Brake: "0.0",
2020-11-29 17:42:33 +00:00
}},
{"Update Throttle",
&events.ThrottleMessage{Throttle: 0.6, Confidence: 1},
&simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0",
Throttle: "0.4",
Brake: "0",
2020-11-29 17:42:33 +00:00
},
simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0",
Throttle: "0.60",
Brake: "0.0",
2020-11-29 17:42:33 +00:00
}},
{"Update steering shouldn't erase throttle value",
&events.ThrottleMessage{Throttle: 0.3, Confidence: 1},
&simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.6",
Brake: "0.0",
2020-11-29 17:42:33 +00:00
},
simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.30",
Brake: "0.0",
2020-11-29 17:42:33 +00:00
}},
{"Throttle to brake",
&events.ThrottleMessage{Throttle: -0.7, Confidence: 1},
&simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.6",
Brake: "0.0",
2020-11-29 17:42:33 +00:00
},
simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.0",
Brake: "0.70",
2020-11-29 17:42:33 +00:00
}},
{"Update brake",
&events.ThrottleMessage{Throttle: -0.2, Confidence: 1},
&simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.0",
Brake: "0.5",
2020-11-29 17:42:33 +00:00
},
simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.0",
Brake: "0.20",
2020-11-29 17:42:33 +00:00
}},
{"Brake to throttle",
&events.ThrottleMessage{Throttle: 0.9, Confidence: 1},
&simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.0",
Brake: "0.4",
2020-11-29 17:42:33 +00:00
},
simulator.ControlMsg{
MsgType: simulator.MsgTypeControl,
2021-02-08 22:07:09 +00:00
Steering: "0.2",
Throttle: "0.90",
Brake: "0.0",
2020-11-29 17:42:33 +00:00
}},
}
simulatorMock := Gw2SimMock{}
2020-11-29 17:42:33 +00:00
err := simulatorMock.listen()
if err != nil {
t.Errorf("unable to start mock gw: %v", err)
2020-11-29 17:42:33 +00:00
}
defer func() {
2020-11-29 17:42:33 +00:00
if err := simulatorMock.Close(); err != nil {
t.Errorf("unable to stop simulator mock: %v", err)
}
}()
gw, err := New(simulatorMock.Addr(), &simulator.CarConfigMsg{MsgType: simulator.MsgTypeCarConfig},
2021-02-16 16:46:10 +00:00
&simulator.RacerBioMsg{},
&simulator.CamConfigMsg{
ImgH: "128",
ImgW: "160",
})
2021-02-16 16:46:10 +00:00
if err != nil {
t.Fatalf("unable to init simulator gateway: %v", err)
}
2021-02-16 16:46:10 +00:00
//go gw.Start()
//<- simulatorMock.NotifyCar()
//<- simulatorMock.NotifyCamera()
2020-11-29 17:42:33 +00:00
for _, c := range cases {
gw.lastControl = c.previousMsg
2020-11-29 17:42:33 +00:00
gw.WriteThrottle(c.msg)
2020-11-29 17:42:33 +00:00
2021-02-16 16:46:10 +00:00
ctrlMsg := <-simulatorMock.NotifyCtrl()
2020-11-29 17:42:33 +00:00
if *ctrlMsg != c.expectedMsg {
t.Errorf("[%v] bad messge received: %#v, wants %#v", c.name, ctrlMsg, c.expectedMsg)
}
}
}