Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDK-3545 - add sensor process #177

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion cartofacade/carto_facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,63 @@ type CartoFacade struct {
requestChan chan Request
}

// RequestInterface defines the functionality of a WorkItem.
// RequestInterface defines the functionality of a Request.
// It should not be used outside of this package but needs to be public for testing purposes.
type RequestInterface interface {
doWork(q *CartoFacade) (interface{}, error)
}

// Interface defines the functionality of a CartoFacade instance.
// It should not be used outside of this package but needs to be public for testing purposes.
type Interface interface {
request(
ctxParent context.Context,
requestType RequestType,
inputs map[RequestParamType]interface{}, timeout time.Duration,
) (interface{}, error)
start(
ctx context.Context,
activeBackgroundWorkers *sync.WaitGroup,
)

Initialize(
ctx context.Context,
timeout time.Duration,
activeBackgroundWorkers *sync.WaitGroup,
) error
Start(
ctx context.Context,
timeout time.Duration,
) error
Stop(
ctx context.Context,
timeout time.Duration,
) error
Terminate(
ctx context.Context,
timeout time.Duration,
) error
AddSensorReading(
ctx context.Context,
timeout time.Duration,
sensorName string,
currentReading []byte,
readingTimestamp time.Time,
) error
GetPosition(
ctx context.Context,
timeout time.Duration,
) (GetPosition, error)
GetInternalState(
ctx context.Context,
timeout time.Duration,
) ([]byte, error)
GetPointCloudMap(
ctx context.Context,
timeout time.Duration,
) ([]byte, error)
}

// Request defines all of the necessary pieces to call into the CGo API.
type Request struct {
responseChan chan Response
Expand Down
175 changes: 174 additions & 1 deletion cartofacade/carto_facade_mock.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Package cartofacade is used to mock a cartofacade.
package cartofacade

// RequestMock represents a fake instance of cartofacade.
import (
"context"
"sync"
"time"
)

// RequestMock represents a fake instance of a request.
type RequestMock struct {
Request
doWorkFunc func(cf *CartoFacade) (interface{}, error)
Expand All @@ -14,3 +20,170 @@ func (r *RequestMock) doWork(cf *CartoFacade) (interface{}, error) {
}
return r.doWorkFunc(cf)
}

// Mock represents a fake instance of cartofacade.
type Mock struct {
CartoFacade
requestFunc func(
ctxParent context.Context,
requestType RequestType,
inputs map[RequestParamType]interface{},
timeout time.Duration,
) (interface{}, error)
startFunc func(
ctx context.Context,
activeBackgroundWorkers *sync.WaitGroup,
)

InitializeFunc func(
ctx context.Context,
timeout time.Duration, activeBackgroundWorkers *sync.WaitGroup,
) error
StartFunc func(
ctx context.Context,
timeout time.Duration,
) error
StopFunc func(
ctx context.Context,
timeout time.Duration,
) error
TerminateFunc func(
ctx context.Context,
timeout time.Duration,
) error
AddSensorReadingFunc func(
ctx context.Context,
timeout time.Duration,
sensorName string,
currentReading []byte,
readingTimestamp time.Time,
) error
GetPositionFunc func(
ctx context.Context,
timeout time.Duration,
) (GetPosition, error)
GetInternalStateFunc func(
ctx context.Context,
timeout time.Duration,
) ([]byte, error)
GetPointCloudMapFunc func(
ctx context.Context,
timeout time.Duration,
) ([]byte, error)
}

// request calls the injected requestFunc or the real version.
func (cf *Mock) request(
ctxParent context.Context,
requestType RequestType,
inputs map[RequestParamType]interface{},
timeout time.Duration,
) (interface{}, error) {
if cf.requestFunc == nil {
return cf.CartoFacade.request(ctxParent, requestType, inputs, timeout)
}
return cf.requestFunc(ctxParent, requestType, inputs, timeout)
}

// start calls the injected startFunc or the real version.
func (cf *Mock) start(
ctx context.Context,
activeBackgroundWorkers *sync.WaitGroup,
) {
if cf.startFunc == nil {
cf.CartoFacade.start(ctx, activeBackgroundWorkers)
}
cf.startFunc(ctx, activeBackgroundWorkers)
}

// Initialize calls the injected InitializeFunc or the real version.
func (cf *Mock) Initialize(
ctx context.Context,
timeout time.Duration,
activeBackgroundWorkers *sync.WaitGroup,
) error {
if cf.InitializeFunc == nil {
return cf.CartoFacade.Initialize(ctx, timeout, activeBackgroundWorkers)
}
return cf.InitializeFunc(ctx, timeout, activeBackgroundWorkers)
}

// Start calls the injected StartFunc or the real version.
func (cf *Mock) Start(
ctx context.Context,
timeout time.Duration,
) error {
if cf.StartFunc == nil {
return cf.CartoFacade.Start(ctx, timeout)
}
return cf.StartFunc(ctx, timeout)
}

// Stop calls the Stop StopFunc or the real version.
func (cf *Mock) Stop(
ctx context.Context,
timeout time.Duration,
) error {
if cf.StopFunc == nil {
return cf.CartoFacade.Stop(ctx, timeout)
}
return cf.StopFunc(ctx, timeout)
}

// Terminate calls the injected TerminateFunc or the real version.
func (cf *Mock) Terminate(
ctx context.Context,
timeout time.Duration,
) error {
if cf.TerminateFunc == nil {
return cf.CartoFacade.Terminate(ctx, timeout)
}
return cf.TerminateFunc(ctx, timeout)
}

// AddSensorReading calls the injected AddSensorReadingFunc or the real version.
func (cf *Mock) AddSensorReading(
ctx context.Context,
timeout time.Duration,
sensorName string,
currentReading []byte,
readingTimestamp time.Time,
) error {
if cf.AddSensorReadingFunc == nil {
return cf.CartoFacade.AddSensorReading(ctx, timeout, sensorName, currentReading, readingTimestamp)
}
return cf.AddSensorReadingFunc(ctx, timeout, sensorName, currentReading, readingTimestamp)
}

// GetPosition calls the injected GetPositionFunc or the real version.
func (cf *Mock) GetPosition(
ctx context.Context,
timeout time.Duration,
) (GetPosition, error) {
if cf.GetPositionFunc == nil {
return cf.CartoFacade.GetPosition(ctx, timeout)
}
return cf.GetPositionFunc(ctx, timeout)
}

// GetInternalState calls the injected GetInternalStateFunc or the real version.
func (cf *Mock) GetInternalState(
ctx context.Context,
timeout time.Duration,
) ([]byte, error) {
if cf.GetInternalStateFunc == nil {
return cf.CartoFacade.GetInternalState(ctx, timeout)
}
return cf.GetInternalStateFunc(ctx, timeout)
}

// GetPointCloudMap calls the injected GetPointCloudMapFunc or the real version.
func (cf *Mock) GetPointCloudMap(
ctx context.Context,
timeout time.Duration,
) ([]byte, error) {
if cf.GetPointCloudMapFunc == nil {
return cf.CartoFacade.GetPointCloudMap(ctx, timeout)
}
return cf.GetPointCloudMapFunc(ctx, timeout)
}
10 changes: 7 additions & 3 deletions internal/testhelper/testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
testDialMaxTimeoutSec = 1
// TestTime can be used to test specific timestamps provided by a replay sensor.
TestTime = "2006-01-02T15:04:05.9999Z"
// BadTime can be used to represent something that should cause an error while parsing it as a time.
BadTime = "NOT A TIME"
)

// IntegrationLidarReleasePointCloudChan is the lidar pointcloud release
Expand Down Expand Up @@ -72,7 +74,9 @@ func SetupDeps(sensors []string) resource.Dependencies {
case "good_lidar":
deps[camera.Named(sensor)] = getGoodLidar()
case "replay_sensor":
deps[camera.Named(sensor)] = getReplaySensor()
deps[camera.Named(sensor)] = getReplaySensor(TestTime)
case "invalid_replay_sensor":
deps[camera.Named(sensor)] = getReplaySensor(BadTime)
case "invalid_sensor":
deps[camera.Named(sensor)] = getInvalidSensor()
case "gibberish":
Expand Down Expand Up @@ -103,12 +107,12 @@ func getGoodLidar() *inject.Camera {
return cam
}

func getReplaySensor() *inject.Camera {
func getReplaySensor(testTime string) *inject.Camera {
cam := &inject.Camera{}
cam.NextPointCloudFunc = func(ctx context.Context) (pointcloud.PointCloud, error) {
md := ctx.Value(contextutils.MetadataContextKey)
if mdMap, ok := md.(map[string][]string); ok {
mdMap[contextutils.TimeRequestedMetadataKey] = []string{TestTime}
mdMap[contextutils.TimeRequestedMetadataKey] = []string{testTime}
}
return pointcloud.New(), nil
}
Expand Down
Loading
Loading