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

Improve steady timer tests #1132

Merged
merged 2 commits into from
Aug 11, 2017
Merged
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
61 changes: 11 additions & 50 deletions test/test_roscpp/test/src/timer_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,16 @@ 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)
// Yes, this is very loose, but must pass in high-load, containerized/virtualized, contentious environments.
if (time_error > 5.0 || 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 +123,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 +141,7 @@ class SteadyTimerHelper
}

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

bool failed_;

Expand Down Expand Up @@ -389,15 +367,16 @@ class WallTimerHelper
void callback(const WallTimerEvent& e)
{
bool first = last_call_.isZero();
WallTime last_call = last_call_;
last_call_ = WallTime::now();
WallTime start = last_call_;
last_call_ = e.current_real;

if (!first)
{
if (fabsf(expected_next_call_.toSec() - start.toSec()) > 0.1f)
double time_error = e.current_real.toSec() - e.current_expected.toSec();
// Strict check if called early, loose check if called late.
// Yes, this is very loose, but must pass in high-load, containerized/virtualized, contentious environments.
if (time_error > 5.0 || 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 @@ -437,28 +416,12 @@ class WallTimerHelper
setPeriod(p, true);
}
}
else
{
expected_next_call_ = e.current_expected + expected_period_;
}

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

++total_calls_;
}

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

timer_.setPeriod(p, reset);
expected_period_ = p;
}
Expand All @@ -471,9 +434,7 @@ class WallTimerHelper
}

WallTime last_call_;
WallTime expected_next_call_;
WallDuration expected_period_;
WallDuration last_duration_;

bool failed_;

Expand Down