Skip to content

Commit

Permalink
improve SteadyTimer tests
Browse files Browse the repository at this point in the history
instead of checking when the callback was actually called,
check for when it was added to the callback queue.

This *should* make the test more reliable.
  • Loading branch information
flixr authored and dirk-thomas committed Aug 10, 2017
1 parent 83b7a53 commit 8b13dbb
Showing 1 changed file with 4 additions and 27 deletions.
31 changes: 4 additions & 27 deletions test/test_roscpp/test/src/timer_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,15 @@ class SteadyTimerHelper
void callback(const SteadyTimerEvent& e)
{
bool first = last_call_.isZero();
SteadyTime last_call = last_call_;
last_call_ = SteadyTime::now();
SteadyTime start = last_call_;
last_call_ = e.current_real;

if (!first)
{
double time_error = start.toSec() - expected_next_call_.toSec();
double time_error = e.current_real.toSec() - e.current_expected.toSec();
// Strict check if called early, loose check if called late.
// The timed wait could be delayed due to scheduling/resources.
if (time_error > 1.0 || time_error < -0.01)
if (time_error > 0.2 || time_error < -0.01)
{
ROS_ERROR("Call came at wrong time (%f vs. %f)", expected_next_call_.toSec(), start.toSec());
ROS_ERROR("Call came at wrong time (expected: %f, actual %f)", e.current_expected.toSec(), e.current_real.toSec());
failed_ = true;
}
}
Expand Down Expand Up @@ -125,30 +122,12 @@ class SteadyTimerHelper
setPeriod(p, true);
}
}
else
{
// If this call was very delayed (beyond the next period), the timer will be
// scheduled to call back immediately (next expected is set to the current time)
expected_next_call_ = std::max(e.current_expected + expected_period_, start);
}

SteadyTime end = SteadyTime::now();
last_duration_ = end - start;

++total_calls_;
}

void setPeriod(const WallDuration p, bool reset=false)
{
if(reset)
{
expected_next_call_ = SteadyTime::now() + p;
}
else
{
expected_next_call_ = last_call_ + p;
}

timer_.setPeriod(p, reset);
expected_period_ = p;
}
Expand All @@ -161,9 +140,7 @@ class SteadyTimerHelper
}

SteadyTime last_call_;
SteadyTime expected_next_call_;
WallDuration expected_period_;
WallDuration last_duration_;

bool failed_;

Expand Down

0 comments on commit 8b13dbb

Please sign in to comment.