2019-12-27 14:38:12 +00:00
|
|
|
package part
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-12-27 16:42:10 +00:00
|
|
|
"github.com/cyrilix/robocar-base/mqttdevice"
|
2019-12-27 14:38:12 +00:00
|
|
|
"github.com/cyrilix/robocar-base/testtools"
|
2019-12-27 16:42:10 +00:00
|
|
|
"github.com/cyrilix/robocar-base/types"
|
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
|
|
|
|
defer func(){
|
|
|
|
registerCallbacks = oldRegister
|
|
|
|
}()
|
|
|
|
registerCallbacks = func(p *ThrottlePart) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
|
2019-12-27 14:38:12 +00:00
|
|
|
minValue := 0.56
|
|
|
|
|
|
|
|
pub := testtools.NewFakePublisher()
|
2019-12-27 16:42:10 +00:00
|
|
|
p := NewPart(nil, pub, throttleTopic, driveModeTopic, rcThrottleTopic, minValue, 1., 200)
|
|
|
|
|
|
|
|
cases := []struct {
|
2020-01-01 18:36:22 +00:00
|
|
|
driveMode string
|
|
|
|
rcThrottle types.Throttle
|
|
|
|
expectedThrottle types.Throttle
|
2019-12-27 16:42:10 +00:00
|
|
|
}{
|
2020-01-01 18:36:22 +00:00
|
|
|
{types.ToString(types.DriveModeUser), types.Throttle{Value: 0.3, Confidence: 1.0},types.Throttle{Value: 0.3, Confidence: 1.0}},
|
|
|
|
{types.ToString(types.DriveModePilot), types.Throttle{Value: 0.5, Confidence: 1.0}, types.Throttle{Value: minValue, Confidence: 1.0}},
|
|
|
|
{types.ToString(types.DriveModePilot), types.Throttle{Value: 0.4, Confidence: 1.0}, types.Throttle{Value: minValue, Confidence: 1.0}},
|
|
|
|
{types.ToString(types.DriveModeUser), types.Throttle{Value: 0.5, Confidence: 1.0}, types.Throttle{Value: 0.5, Confidence: 1.0}},
|
|
|
|
{types.ToString(types.DriveModeUser), types.Throttle{Value: 0.4, Confidence: 1.0}, types.Throttle{Value: 0.4, Confidence: 1.0}},
|
|
|
|
{types.ToString(types.DriveModeUser), types.Throttle{Value: 0.6, Confidence: 1.0}, types.Throttle{Value: 0.6, Confidence: 1.0}},
|
2019-12-27 16:42:10 +00:00
|
|
|
}
|
2019-12-27 14:38:12 +00:00
|
|
|
|
|
|
|
go p.Start()
|
2019-12-27 16:42:10 +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
|
|
|
|
2019-12-27 16:42:10 +00:00
|
|
|
p.onDriveMode(nil, testtools.NewFakeMessage(driveModeTopic, mqttdevice.NewMqttValue(c.driveMode)))
|
|
|
|
p.onRCThrottle(nil, testtools.NewFakeMessage(rcThrottleTopic, mqttdevice.NewMqttValue(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-- {
|
|
|
|
|
|
|
|
mqttValue := pub.PublishedEvent(throttleTopic)
|
|
|
|
var throttle types.Throttle
|
|
|
|
err := json.Unmarshal(mqttValue, &throttle)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unable to unmarshall response: %v", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
2020-01-01 18:36:22 +00:00
|
|
|
if throttle != c.expectedThrottle {
|
2019-12-27 16:42:10 +00:00
|
|
|
t.Errorf("bad throttle value for mode %v: %v, wants %v", c.driveMode, throttle.Value, c.expectedThrottle)
|
|
|
|
}
|
|
|
|
if throttle.Confidence != 1. {
|
|
|
|
t.Errorf("bad throtlle confidence: %v, wants %v", throttle.Confidence, 1.)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
2019-12-27 14:38:12 +00:00
|
|
|
}
|
|
|
|
}
|