@@ -342,6 +342,70 @@ TEST(Sensor_TEST, SetRateZeroService)
342342 EXPECT_FLOAT_EQ (20.0 , sensor.UpdateRate ());
343343}
344344
345+ // ////////////////////////////////////////////////
346+ TEST_F (SensorUpdate, NextDataUpdateTime)
347+ {
348+ // Create sensor.
349+ sdf::Sensor sdfSensor;
350+ sdfSensor.SetName (kSensorName );
351+ sdfSensor.SetTopic (kSensorTopic );
352+ sdfSensor.SetUpdateRate (1 );
353+
354+ std::unique_ptr<Sensor> sensor = std::make_unique<TestSensor>();
355+ sensor->Load (sdfSensor);
356+
357+ {
358+ std::chrono::steady_clock::duration now = std::chrono::seconds (0 );
359+ std::chrono::steady_clock::duration next = std::chrono::seconds (1 );
360+ EXPECT_TRUE (sensor->Update (now, false ));
361+ EXPECT_EQ (next.count (), sensor->NextDataUpdateTime ().count ());
362+ }
363+
364+ {
365+ std::chrono::steady_clock::duration now = std::chrono::seconds (1 );
366+ std::chrono::steady_clock::duration next = std::chrono::seconds (2 );
367+ EXPECT_TRUE (sensor->Update (now, false ));
368+ EXPECT_EQ (next.count (), sensor->NextDataUpdateTime ().count ());
369+ }
370+
371+ {
372+ // set the next data update time into the future, so we no longer
373+ // expect an update to happen
374+ std::chrono::steady_clock::duration now = std::chrono::seconds (2 );
375+ std::chrono::steady_clock::duration next = std::chrono::seconds (5 );
376+ sensor->SetNextDataUpdateTime (next);
377+ EXPECT_FALSE (sensor->Update (now, false ));
378+ EXPECT_EQ (next.count (), sensor->NextDataUpdateTime ().count ());
379+ }
380+
381+ {
382+ // Force has no impact on the next update time
383+ std::chrono::steady_clock::duration now = std::chrono::seconds (3 );
384+ std::chrono::steady_clock::duration next = std::chrono::seconds (5 );
385+ EXPECT_TRUE (sensor->Update (now, true ));
386+ EXPECT_EQ (next.count (), sensor->NextDataUpdateTime ().count ());
387+ }
388+
389+ {
390+ // When that time point is reached, the update happens
391+ std::chrono::steady_clock::duration now = std::chrono::seconds (5 );
392+ std::chrono::steady_clock::duration next = std::chrono::seconds (6 );
393+ EXPECT_TRUE (sensor->Update (now, false ));
394+ EXPECT_EQ (next.count (), sensor->NextDataUpdateTime ().count ());
395+ }
396+
397+ {
398+ // Jump backwards in time
399+ std::chrono::steady_clock::duration now = std::chrono::seconds (5 );
400+ std::chrono::steady_clock::duration next = std::chrono::seconds (1 );
401+ sensor->SetNextDataUpdateTime (next);
402+ EXPECT_TRUE (sensor->Update (now, false ));
403+
404+ // The next update should be the first dt past the current time
405+ std::chrono::steady_clock::duration newNext = std::chrono::seconds (6 );
406+ EXPECT_EQ (newNext.count (), sensor->NextDataUpdateTime ().count ());
407+ }
408+ }
345409
346410// ////////////////////////////////////////////////
347411int main (int argc, char **argv)
0 commit comments