Skip to content

Commit

Permalink
Appease linter
Browse files Browse the repository at this point in the history
  • Loading branch information
kkufieta committed Dec 12, 2023
1 parent 1d44f78 commit 2fdffcc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 51 deletions.
50 changes: 19 additions & 31 deletions sensorprocess/movementsensorprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,45 +64,29 @@ func TestAddMovementSensorReadingInOnline(t *testing.T) {
}

t.Run("returns error when LinearAcceleration or AngularVelocity return an error, doesn't try to add IMU data", func(t *testing.T) {
lidarFrequencyHz := 10
movementSensorFrequencyHz := 10
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config,
lidarFrequencyHz, s.IMUWithErroringFunctions, movementSensorFrequencyHz)
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config, s.IMUWithErroringFunctions)
})

t.Run("returns error when Position or Orientation return an error, doesn't try to add odometer data", func(t *testing.T) {
lidarFrequencyHz := 10
movementSensorFrequencyHz := 10
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config,
lidarFrequencyHz, s.OdometerWithErroringFunctions, movementSensorFrequencyHz)
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config, s.OdometerWithErroringFunctions)
})

t.Run("returns error when LinearAcceleration, AngularVelocity, Position, and Orientation return an error, doesn't try to add movement sensor data", func(t *testing.T) {
lidarFrequencyHz := 10
movementSensorFrequencyHz := 10
t.Run("returns error when LinearAcceleration, AngularVelocity, Position, and Orientation return an error, "+
"doesn't try to add movement sensor data", func(t *testing.T) {
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config,
lidarFrequencyHz, s.MovementSensorBothIMUAndOdometerWithErroringFunctions, movementSensorFrequencyHz)
s.MovementSensorBothIMUAndOdometerWithErroringFunctions)
})

t.Run("returns error when IMU replay sensor timestamp is invalid, doesn't try to add sensor data", func(t *testing.T) {
lidarFrequencyHz := 10
movementSensorFrequencyHz := 10
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config,
lidarFrequencyHz, s.InvalidReplayIMU, movementSensorFrequencyHz)
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config, s.InvalidReplayIMU)
})

t.Run("returns error when odometer replay sensor timestamp is invalid, doesn't try to add sensor data", func(t *testing.T) {
lidarFrequencyHz := 10
movementSensorFrequencyHz := 10
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config,
lidarFrequencyHz, s.InvalidReplayOdometer, movementSensorFrequencyHz)
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config, s.InvalidReplayOdometer)
})

t.Run("returns error when replay movement sensor timestamp is invalid, doesn't try to add sensor data", func(t *testing.T) {
lidarFrequencyHz := 10
movementSensorFrequencyHz := 10
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config,
lidarFrequencyHz, s.InvalidReplayMovementSensorBothIMUAndOdometer, movementSensorFrequencyHz)
invalidAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, cf, config, s.InvalidReplayMovementSensorBothIMUAndOdometer)
})

t.Run("online replay IMU adds sensor reading once and ignores errors", func(t *testing.T) {
Expand All @@ -122,29 +106,33 @@ func TestAddMovementSensorReadingInOnline(t *testing.T) {
})

t.Run("online replay movement sensor adds sensor reading once and ignores errors", func(t *testing.T) {
imuCalls, odometerCalls := validAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, config, cf, s.ReplayMovementSensorBothIMUAndOdometer)
imuCalls, odometerCalls := validAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, config, cf,
s.ReplayMovementSensorBothIMUAndOdometer)
readingTime, err := time.Parse(time.RFC3339Nano, s.TestTimestamp)
test.That(t, err, test.ShouldBeNil)
test.That(t, imuCalls[0].currentReading.ReadingTime.Equal(readingTime), test.ShouldBeTrue)
test.That(t, odometerCalls[0].currentReading.ReadingTime.Equal(readingTime), test.ShouldBeTrue)
})

t.Run("online IMU adds sensor reading once and ignores errors", func(t *testing.T) {
imuCalls, odometerCalls := validAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, config, cf, s.GoodIMU)
imuCalls, odometerCalls := validAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, config, cf,
s.GoodIMU)
test.That(t, len(odometerCalls), test.ShouldBeZeroValue)
test.That(t, imuCalls[0].currentReading.ReadingTime.Before(imuCalls[1].currentReading.ReadingTime), test.ShouldBeTrue)
test.That(t, imuCalls[1].currentReading.ReadingTime.Before(imuCalls[2].currentReading.ReadingTime), test.ShouldBeTrue)
})

t.Run("online odometer adds sensor reading once and ignores errors", func(t *testing.T) {
imuCalls, odometerCalls := validAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, config, cf, s.GoodOdometer)
imuCalls, odometerCalls := validAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, config, cf,
s.GoodOdometer)
test.That(t, len(imuCalls), test.ShouldBeZeroValue)
test.That(t, odometerCalls[0].currentReading.ReadingTime.Before(odometerCalls[1].currentReading.ReadingTime), test.ShouldBeTrue)
test.That(t, odometerCalls[1].currentReading.ReadingTime.Before(odometerCalls[2].currentReading.ReadingTime), test.ShouldBeTrue)
})

t.Run("online movement sensor adds sensor reading once and ignores errors", func(t *testing.T) {
imuCalls, odometerCalls := validAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, config, cf, s.GoodMovementSensorBothIMUAndOdometer)
imuCalls, odometerCalls := validAddMovementSensorReadingInOnlineTestHelper(context.Background(), t, config, cf,
s.GoodMovementSensorBothIMUAndOdometer)
test.That(t, imuCalls[0].currentReading.ReadingTime.Before(imuCalls[1].currentReading.ReadingTime), test.ShouldBeTrue)
test.That(t, imuCalls[1].currentReading.ReadingTime.Before(imuCalls[2].currentReading.ReadingTime), test.ShouldBeTrue)
test.That(t, odometerCalls[0].currentReading.ReadingTime.Before(odometerCalls[1].currentReading.ReadingTime), test.ShouldBeTrue)
Expand Down Expand Up @@ -207,7 +195,7 @@ func TestTryAddMovementSensorReadingOnce(t *testing.T) {
}
}
config.MovementSensor = &injectMovementSensor
TestTryAddMovementSensorReadingOnceTestHelper(context.Background(), t, config, cf)
tryAddMovementSensorReadingOnceTestHelper(t, config, cf)
})

t.Run("odometer only supported", func(t *testing.T) {
Expand All @@ -218,7 +206,7 @@ func TestTryAddMovementSensorReadingOnce(t *testing.T) {
}
}
config.MovementSensor = &injectMovementSensor
TestTryAddMovementSensorReadingOnceTestHelper(context.Background(), t, config, cf)
tryAddMovementSensorReadingOnceTestHelper(t, config, cf)
})

t.Run("both imu and odometer supported", func(t *testing.T) {
Expand All @@ -229,7 +217,7 @@ func TestTryAddMovementSensorReadingOnce(t *testing.T) {
}
}
config.MovementSensor = &injectMovementSensor
TestTryAddMovementSensorReadingOnceTestHelper(context.Background(), t, config, cf)
tryAddMovementSensorReadingOnceTestHelper(t, config, cf)
})
}

Expand Down
20 changes: 14 additions & 6 deletions sensorprocess/sensorprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func TestStartOfflineSensorProcess(t *testing.T) {
expectedDataInsertions: []string{"lidar: 2", "odometer: 3", "lidar: 4", "odometer: 5"},
},
{
description: "if movement sensor data ends before lidar data ends, stop adding data once end of movement sensor dataset is reached",
description: "if movement sensor data ends before lidar data ends, stop adding data once end " +
"of movement sensor dataset is reached",
imuEnabled: true,
odometerEnabled: true,
lidarReadingTimeAddedMs: []int{2, 4, 6, 8, 10, 12},
Expand Down Expand Up @@ -204,7 +205,10 @@ func TestStartOfflineSensorProcess(t *testing.T) {
odometerEnabled: true,
lidarReadingTimeAddedMs: []int{1, 3, 5},
msReadingTimeAddedMs: []int{2, 3, 4, 6, 8, 10, 12},
expectedDataInsertions: []string{"lidar: 1", "odometer: 2", "imu: 2", "lidar: 3", "odometer: 3", "imu: 3", "odometer: 4", "imu: 4", "lidar: 5"},
expectedDataInsertions: []string{
"lidar: 1", "odometer: 2", "imu: 2", "lidar: 3", "odometer: 3",
"imu: 3", "odometer: 4", "imu: 4", "lidar: 5",
},
},
}

Expand Down Expand Up @@ -234,11 +238,13 @@ func TestStartOfflineSensorProcess(t *testing.T) {
if numMovementSensorData < len(tt.msReadingTimeAddedMs) {
if tt.odometerEnabled {
movementSensorReading.TimedOdometerResponse = &odometerReading
movementSensorReading.TimedOdometerResponse.ReadingTime = now.Add(time.Duration(tt.msReadingTimeAddedMs[numMovementSensorData]) * time.Millisecond)
odometerReadingTime := now.Add(time.Duration(tt.msReadingTimeAddedMs[numMovementSensorData]) * time.Millisecond)
movementSensorReading.TimedOdometerResponse.ReadingTime = odometerReadingTime
}
if tt.imuEnabled {
movementSensorReading.TimedIMUResponse = &imuReading
movementSensorReading.TimedIMUResponse.ReadingTime = now.Add(time.Duration(tt.msReadingTimeAddedMs[numMovementSensorData]) * time.Millisecond)
imuReadingTime := now.Add(time.Duration(tt.msReadingTimeAddedMs[numMovementSensorData]) * time.Millisecond)
movementSensorReading.TimedIMUResponse.ReadingTime = imuReadingTime
}
numMovementSensorData++
return movementSensorReading, nil
Expand Down Expand Up @@ -433,11 +439,13 @@ func TestGetInitialMovementSensorReading(t *testing.T) {
if numMovementSensorData < len(tt.msReadingTimeAddedMs) {
if tt.odometerEnabled {
movementSensorReading.TimedOdometerResponse = &odometerReading
movementSensorReading.TimedOdometerResponse.ReadingTime = now.Add(time.Duration(tt.msReadingTimeAddedMs[numMovementSensorData]) * time.Millisecond)
odometerReadingTime := now.Add(time.Duration(tt.msReadingTimeAddedMs[numMovementSensorData]) * time.Millisecond)
movementSensorReading.TimedOdometerResponse.ReadingTime = odometerReadingTime
}
if tt.imuEnabled {
movementSensorReading.TimedIMUResponse = &imuReading
movementSensorReading.TimedIMUResponse.ReadingTime = now.Add(time.Duration(float64(tt.msReadingTimeAddedMs[numMovementSensorData])+0.1) * time.Millisecond)
imuReadingTime := now.Add(time.Duration(float64(tt.msReadingTimeAddedMs[numMovementSensorData])+0.1) * time.Millisecond)
movementSensorReading.TimedIMUResponse.ReadingTime = imuReadingTime
}
numMovementSensorData++
return movementSensorReading, nil
Expand Down
26 changes: 12 additions & 14 deletions sensorprocess/testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (

"go.viam.com/rdk/logging"
"go.viam.com/rdk/spatialmath"
rdkutils "go.viam.com/rdk/utils"
"go.viam.com/test"

"github.com/viamrobotics/viam-cartographer/cartofacade"
s "github.com/viamrobotics/viam-cartographer/sensors"
"github.com/viamrobotics/viam-cartographer/sensors/inject"
rdkutils "go.viam.com/rdk/utils"
)

type addLidarReadingArgs struct {
Expand Down Expand Up @@ -280,13 +280,13 @@ func invalidAddMovementSensorReadingInOnlineTestHelper(
t *testing.T,
cartoFacadeMock cartofacade.Mock,
config Config,
lidarDataFrequencyHz int,
testMovementSensor s.TestSensor,
movementSensorDataFrequencyHz int,
) {
logger := logging.NewTestLogger(t)
lidarFrequencyHz := 10
movementSensorFrequencyHz := 10
movementSensor, err := s.NewMovementSensor(context.Background(), s.SetupDeps(s.NoLidar, testMovementSensor),
string(testMovementSensor), movementSensorDataFrequencyHz, logger)
string(testMovementSensor), movementSensorFrequencyHz, logger)
test.That(t, err, test.ShouldBeNil)

var imuCalls []addIMUReadingArgs
Expand Down Expand Up @@ -323,7 +323,7 @@ func invalidAddMovementSensorReadingInOnlineTestHelper(
config.CartoFacade = &cartoFacadeMock

injectLidar := inject.TimedLidar{}
injectLidar.DataFrequencyHzFunc = func() int { return lidarDataFrequencyHz }
injectLidar.DataFrequencyHzFunc = func() int { return lidarFrequencyHz }
config.Lidar = &injectLidar

config.MovementSensor = movementSensor
Expand Down Expand Up @@ -412,7 +412,8 @@ func validAddMovementSensorReadingUntilSuccessTestHelper(
}
}

config.tryAddMovementSensorReadingUntilSuccess(ctx, movementSensorReading)
err = config.tryAddMovementSensorReadingUntilSuccess(ctx, movementSensorReading)
test.That(t, err, test.ShouldBeNil)
if movementSensor.Properties().IMUSupported {
test.That(t, len(imuCalls), test.ShouldEqual, 3)
firstTimestamp := imuCalls[0].currentReading.ReadingTime
Expand All @@ -439,8 +440,7 @@ func validAddMovementSensorReadingUntilSuccessTestHelper(
}
}

func TestTryAddMovementSensorReadingOnceTestHelper(
ctx context.Context,
func tryAddMovementSensorReadingOnceTestHelper(
t *testing.T,
config Config,
cf cartofacade.Mock,
Expand All @@ -466,7 +466,6 @@ func TestTryAddMovementSensorReadingOnceTestHelper(
}

if config.MovementSensor.Properties().IMUSupported {

// In case that the odometer is also supported, let's assume it works fast and efficiently for all the
// following test cases
cf.AddOdometerReadingFunc = func(
Expand Down Expand Up @@ -586,11 +585,9 @@ func TestTryAddMovementSensorReadingOnceTestHelper(
test.That(t, timeToSleep, test.ShouldBeGreaterThan, 0)
test.That(t, timeToSleep, test.ShouldBeLessThanOrEqualTo, 1000/config.MovementSensor.DataFrequencyHz())
})

}

if config.MovementSensor.Properties().OdometerSupported {

// In case that the IMU is also supported, let's assume it works fast and efficiently for all the
// following test cases
cf.AddIMUReadingFunc = func(
Expand Down Expand Up @@ -652,7 +649,8 @@ func TestTryAddMovementSensorReadingOnceTestHelper(
test.That(t, timeToSleep, test.ShouldEqual, 0)
})

t.Run("when AddOdometerReading blocks for more than the date rate and returns an unexpected error, time to sleep is 0", func(t *testing.T) {
t.Run("when AddOdometerReading blocks for more than the date rate and returns an unexpected error, "+
"time to sleep is 0", func(t *testing.T) {
cf.AddOdometerReadingFunc = func(
ctx context.Context,
timeout time.Duration,
Expand Down Expand Up @@ -682,7 +680,8 @@ func TestTryAddMovementSensorReadingOnceTestHelper(
test.That(t, timeToSleep, test.ShouldBeLessThanOrEqualTo, 1000/config.MovementSensor.DataFrequencyHz())
})

t.Run("when AddOdometerReading are faster than the date rate and returns a lock error, time to sleep is <= date rate", func(t *testing.T) {
t.Run("when AddOdometerReading are faster than the date rate and returns a lock error, "+
"time to sleep is <= date rate", func(t *testing.T) {
cf.AddOdometerReadingFunc = func(
ctx context.Context,
timeout time.Duration,
Expand Down Expand Up @@ -712,6 +711,5 @@ func TestTryAddMovementSensorReadingOnceTestHelper(
test.That(t, timeToSleep, test.ShouldBeGreaterThan, 0)
test.That(t, timeToSleep, test.ShouldBeLessThanOrEqualTo, 1000/config.MovementSensor.DataFrequencyHz())
})

}
}

0 comments on commit 2fdffcc

Please sign in to comment.