2021-01-15 10:12:21 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-02-16 16:46:10 +00:00
|
|
|
"fmt"
|
2021-01-15 10:12:21 +00:00
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
2021-02-16 16:46:10 +00:00
|
|
|
"github.com/cyrilix/robocar-simulator/pkg/simulator"
|
2021-01-15 10:12:21 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
func TestGateway_ListenEvents(t *testing.T) {
|
|
|
|
simulatorMock := Sim2GwMock{}
|
2021-01-15 10:12:21 +00:00
|
|
|
err := simulatorMock.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to start mock server: %v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := simulatorMock.Close(); err != nil {
|
|
|
|
t.Errorf("unable to close mock server: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-02-16 16:46:10 +00:00
|
|
|
gw := New(simulatorMock.Addr(),
|
|
|
|
&simulator.CarConfigMsg{MsgType: simulator.MsgTypeCarConfig},
|
|
|
|
&simulator.RacerBioMsg{},
|
|
|
|
&simulator.CamConfigMsg{},
|
|
|
|
)
|
2021-01-15 10:12:21 +00:00
|
|
|
go func() {
|
2021-01-21 22:39:52 +00:00
|
|
|
err := gw.Start()
|
2021-01-15 10:12:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to start gateway simulator: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer func() {
|
2021-01-21 22:39:52 +00:00
|
|
|
if err := gw.Close(); err != nil {
|
2021-01-15 10:12:21 +00:00
|
|
|
t.Errorf("unable to close gateway simulator: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-01-21 22:39:52 +00:00
|
|
|
frameChannel := gw.SubscribeFrame()
|
|
|
|
steeringChannel := gw.SubscribeSteering()
|
|
|
|
throttleChannel := gw.SubscribeThrottle()
|
2021-01-15 10:12:21 +00:00
|
|
|
|
|
|
|
simulatorMock.WaitConnection()
|
2021-02-16 16:46:10 +00:00
|
|
|
simulatorMock.EmitMsg(fmt.Sprintf("{\"msg_type\": \"%s\"}", simulator.MsgTypeCarLoaded))
|
|
|
|
|
2021-01-15 10:12:21 +00:00
|
|
|
log.Trace("read test data")
|
|
|
|
testContent, err := ioutil.ReadFile("testdata/msg.json")
|
|
|
|
lines := strings.Split(string(testContent), "\n")
|
|
|
|
|
|
|
|
for idx, line := range lines {
|
|
|
|
err = simulatorMock.EmitMsg(line)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[line %v/%v] unable to write line: %v", idx+1, len(lines), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsType := map[string]bool{"frame": false, "steering": false, "throttle": false}
|
|
|
|
nbEventsExpected := len(eventsType)
|
|
|
|
wg := sync.WaitGroup{}
|
2021-01-21 22:39:52 +00:00
|
|
|
// Expect number events event
|
2021-01-15 10:12:21 +00:00
|
|
|
|
|
|
|
wg.Add(nbEventsExpected)
|
|
|
|
finished := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
finished <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
timeout := time.Tick(100 * time.Millisecond)
|
|
|
|
|
|
|
|
endLoop := false
|
2021-01-21 22:39:52 +00:00
|
|
|
var frameRef, steeringIdRef, throttleIdRef *events.FrameRef
|
|
|
|
|
2021-01-15 10:12:21 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-01-21 22:39:52 +00:00
|
|
|
case msg := <-frameChannel:
|
|
|
|
checkFrame(t, msg)
|
2021-01-15 10:12:21 +00:00
|
|
|
eventsType["frame"] = true
|
2021-01-21 22:39:52 +00:00
|
|
|
frameRef = msg.Id
|
2021-01-15 10:12:21 +00:00
|
|
|
wg.Done()
|
2021-01-21 22:39:52 +00:00
|
|
|
case msg := <-steeringChannel:
|
|
|
|
checkSteering(t, msg, line)
|
2021-01-15 10:12:21 +00:00
|
|
|
eventsType["steering"] = true
|
2021-01-21 22:39:52 +00:00
|
|
|
steeringIdRef = msg.FrameRef
|
2021-01-15 10:12:21 +00:00
|
|
|
wg.Done()
|
2021-01-21 22:39:52 +00:00
|
|
|
case msg := <-throttleChannel:
|
|
|
|
checkThrottle(t, msg, line)
|
2021-01-15 10:12:21 +00:00
|
|
|
eventsType["throttle"] = true
|
2021-01-21 22:39:52 +00:00
|
|
|
throttleIdRef = msg.FrameRef
|
2021-01-15 10:12:21 +00:00
|
|
|
wg.Done()
|
|
|
|
case <-finished:
|
|
|
|
log.Trace("loop ended")
|
|
|
|
endLoop = true
|
|
|
|
case <-timeout:
|
|
|
|
t.Errorf("not all event are published")
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if endLoop {
|
2021-01-21 22:39:52 +00:00
|
|
|
if frameRef != steeringIdRef || steeringIdRef == nil {
|
|
|
|
t.Errorf("steering msg without frameRef '%#v', wants '%#v'", steeringIdRef, frameRef)
|
|
|
|
}
|
|
|
|
if frameRef != throttleIdRef || throttleIdRef == nil {
|
|
|
|
t.Errorf("throttle msg without frameRef '%#v', wants '%#v'", throttleIdRef, frameRef)
|
|
|
|
}
|
2021-01-15 10:12:21 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range eventsType {
|
|
|
|
if !v {
|
|
|
|
t.Errorf("no %v event published for line %v", k, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 22:39:52 +00:00
|
|
|
func checkFrame(t *testing.T, msg *events.FrameMessage) {
|
2021-01-15 10:12:21 +00:00
|
|
|
if msg.GetId() == nil {
|
|
|
|
t.Error("frame msg has not Id")
|
|
|
|
}
|
|
|
|
if len(msg.Frame) < 10 {
|
|
|
|
t.Errorf("[%v] invalid frame image: %v", msg.Id, msg.GetFrame())
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 22:39:52 +00:00
|
|
|
func checkSteering(t *testing.T, msg *events.SteeringMessage, rawLine string) {
|
2021-01-15 10:12:21 +00:00
|
|
|
var input map[string]interface{}
|
|
|
|
err := json.Unmarshal([]byte(rawLine), &input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to parse input data '%v': %v", rawLine, err)
|
|
|
|
}
|
|
|
|
steering := input["steering_angle"].(float64)
|
|
|
|
expectedSteering := float32(steering)
|
|
|
|
|
2021-01-15 10:31:37 +00:00
|
|
|
if msg.GetSteering() != expectedSteering {
|
2021-01-15 10:12:21 +00:00
|
|
|
t.Errorf("invalid steering value: %f, wants %f", msg.GetSteering(), expectedSteering)
|
|
|
|
}
|
|
|
|
if msg.Confidence != 1.0 {
|
|
|
|
t.Errorf("invalid steering confidence: %f, wants %f", msg.Confidence, 1.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 22:39:52 +00:00
|
|
|
func checkThrottle(t *testing.T, msg *events.ThrottleMessage, rawLine string) {
|
2021-01-15 10:12:21 +00:00
|
|
|
var input map[string]interface{}
|
|
|
|
err := json.Unmarshal([]byte(rawLine), &input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to parse input data '%v': %v", rawLine, err)
|
|
|
|
}
|
|
|
|
throttle := input["throttle"].(float64)
|
|
|
|
expectedThrottle := float32(throttle)
|
|
|
|
|
2021-01-21 22:39:52 +00:00
|
|
|
if msg.Throttle != expectedThrottle {
|
|
|
|
t.Errorf("invalid throttle value: %f, wants %f", msg.Throttle, expectedThrottle)
|
2021-01-15 10:12:21 +00:00
|
|
|
}
|
|
|
|
if msg.Confidence != 1.0 {
|
|
|
|
t.Errorf("invalid throttle confidence: %f, wants %f", msg.Confidence, 1.0)
|
|
|
|
}
|
|
|
|
}
|