2021-01-15 10:31:37 +00:00
|
|
|
package gateway
|
2020-11-29 17:42:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
2021-01-15 10:12:21 +00:00
|
|
|
"github.com/cyrilix/robocar-simulator/pkg/simulator"
|
2020-11-29 17:42:33 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
simulatorMock := Gw2SimMock{}
|
2020-11-29 17:42:33 +00:00
|
|
|
err := simulatorMock.listen()
|
|
|
|
if err != nil {
|
2021-01-15 10:31:37 +00:00
|
|
|
t.Errorf("unable to start mock gw: %v", err)
|
2020-11-29 17:42:33 +00:00
|
|
|
}
|
2021-01-15 10:31:37 +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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-21 22:39:52 +00:00
|
|
|
gw := New(simulatorMock.Addr())
|
2020-11-29 17:42:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init simulator gateway: %v", err)
|
|
|
|
}
|
2021-01-15 10:31:37 +00:00
|
|
|
go gw.Start()
|
|
|
|
defer gw.Close()
|
2020-11-29 17:42:33 +00:00
|
|
|
|
|
|
|
for _, c := range cases {
|
2021-01-15 10:31:37 +00:00
|
|
|
gw.lastControl = c.previousMsg
|
2020-11-29 17:42:33 +00:00
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
gw.WriteSteering(c.msg)
|
2020-11-29 17:42:33 +00:00
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
ctrlMsg := <-simulatorMock.Notify()
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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: "control",
|
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
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
simulatorMock := Gw2SimMock{}
|
2020-11-29 17:42:33 +00:00
|
|
|
err := simulatorMock.listen()
|
|
|
|
if err != nil {
|
2021-01-15 10:31:37 +00:00
|
|
|
t.Errorf("unable to start mock gw: %v", err)
|
2020-11-29 17:42:33 +00:00
|
|
|
}
|
2021-01-15 10:31:37 +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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-21 22:39:52 +00:00
|
|
|
gw := New(simulatorMock.Addr())
|
2020-11-29 17:42:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to init simulator gateway: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2021-01-15 10:31:37 +00:00
|
|
|
gw.lastControl = c.previousMsg
|
2020-11-29 17:42:33 +00:00
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
gw.WriteThrottle(c.msg)
|
2020-11-29 17:42:33 +00:00
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
ctrlMsg := <-simulatorMock.Notify()
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|