2019-12-27 14:38:12 +00:00
|
|
|
package part
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cyrilix/robocar-base/testtools"
|
2020-01-01 19:33:41 +00:00
|
|
|
"github.com/cyrilix/robocar-protobuf/go/events"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"sync"
|
2019-12-27 14:38:12 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-12-27 16:42:10 +00:00
|
|
|
func TestDefaultThrottle(t *testing.T) {
|
|
|
|
oldRegister := registerCallbacks
|
2020-01-01 19:33:41 +00:00
|
|
|
oldPublish := publish
|
|
|
|
defer func() {
|
2019-12-27 16:42:10 +00:00
|
|
|
registerCallbacks = oldRegister
|
2020-01-01 19:33:41 +00:00
|
|
|
publish = oldPublish
|
2019-12-27 16:42:10 +00:00
|
|
|
}()
|
|
|
|
registerCallbacks = func(p *ThrottlePart) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:33:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-27 14:38:12 +00:00
|
|
|
throttleTopic := "topic/throttle"
|
2019-12-27 16:42:10 +00:00
|
|
|
driveModeTopic := "topic/driveMode"
|
|
|
|
rcThrottleTopic := "topic/rcThrottle"
|
|
|
|
|
2020-01-01 19:33:41 +00:00
|
|
|
minValue := float32(0.56)
|
2019-12-27 14:38:12 +00:00
|
|
|
|
2020-01-01 19:33:41 +00:00
|
|
|
p := NewPart(nil, throttleTopic, driveModeTopic, rcThrottleTopic, minValue, 1., 200)
|
2019-12-27 16:42:10 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
2020-01-01 19:33:41 +00:00
|
|
|
driveMode events.DriveModeMessage
|
|
|
|
rcThrottle events.ThrottleMessage
|
|
|
|
expectedThrottle events.ThrottleMessage
|
2019-12-27 16:42:10 +00:00
|
|
|
}{
|
2020-01-01 19:33:41 +00:00
|
|
|
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.3, Confidence: 1.0}},
|
|
|
|
{events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}},
|
|
|
|
{events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: minValue, Confidence: 1.0}},
|
|
|
|
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.5, Confidence: 1.0}},
|
|
|
|
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.4, Confidence: 1.0}},
|
|
|
|
{events.DriveModeMessage{DriveMode: events.DriveMode_USER}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}, events.ThrottleMessage{Throttle: 0.6, Confidence: 1.0}},
|
2019-12-27 16:42:10 +00:00
|
|
|
}
|
2019-12-27 14:38:12 +00:00
|
|
|
|
|
|
|
go p.Start()
|
2020-01-01 19:33:41 +00:00
|
|
|
defer func() { close(p.cancel) }()
|
2019-12-27 14:38:12 +00:00
|
|
|
|
2019-12-27 16:42:10 +00:00
|
|
|
for _, c := range cases {
|
2019-12-27 14:38:12 +00:00
|
|
|
|
2020-01-01 19:33:41 +00:00
|
|
|
p.onDriveMode(nil, testtools.NewFakeMessageFromProtobuf(driveModeTopic, &c.driveMode))
|
|
|
|
p.onRCThrottle(nil, testtools.NewFakeMessageFromProtobuf(rcThrottleTopic, &c.rcThrottle))
|
2019-12-27 14:38:12 +00:00
|
|
|
|
2019-12-27 16:42:10 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
for i := 3; i >= 0; i-- {
|
|
|
|
|
2020-01-01 19:33:41 +00:00
|
|
|
var msg events.ThrottleMessage
|
|
|
|
muEventsPublished.Lock()
|
|
|
|
err := proto.Unmarshal(eventsPublished[throttleTopic], &msg)
|
2019-12-27 16:42:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to unmarshall response: %v", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
2020-01-01 19:33:41 +00:00
|
|
|
muEventsPublished.Unlock()
|
2019-12-27 16:42:10 +00:00
|
|
|
|
2020-01-01 19:33:41 +00:00
|
|
|
if msg.GetThrottle() != c.expectedThrottle.GetThrottle() {
|
|
|
|
t.Errorf("bad msg value for mode %v: %v, wants %v", c.driveMode, msg.GetThrottle(), c.expectedThrottle.GetThrottle())
|
2019-12-27 16:42:10 +00:00
|
|
|
}
|
2020-01-01 19:33:41 +00:00
|
|
|
if msg.GetConfidence() != 1. {
|
|
|
|
t.Errorf("bad throtlle confidence: %v, wants %v", msg.GetConfidence(), 1.)
|
2019-12-27 16:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|
|
|
|
}
|