Skip to content

Commit

Permalink
add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LoreMoretti committed Oct 4, 2024
1 parent e52512e commit 2461257
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/System/tests/PeriodicThreadTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ class Thread : public PeriodicThread
}
};

class ThreadWithDeadlineMiss : public PeriodicThread
{
protected:
bool threadInit() override
{
return true;
}
bool run() override
{
BipedalLocomotion::clock().sleepFor(waitTime);
return true;
}

private:
std::chrono::nanoseconds waitTime;

public:
void setWaitTime(std::chrono::nanoseconds time)
{
waitTime = time;
}
};

TEST_CASE("Test Periodic Thread", "[PeriodicThread]")
{
BipedalLocomotion::System::ClockBuilder::setFactory(
Expand Down Expand Up @@ -140,10 +163,44 @@ TEST_CASE("Test Periodic Thread", "[PeriodicThreadNotAllowed]")
// try to set the policy
REQUIRE(!thread.setPolicy(SCHED_OTHER, 0));

// try to set the maximum number of accepted deadline miss
REQUIRE(!thread.setMaximumNumberOfAcceptedDeadlineMiss(0));

// try to enable early wake up
REQUIRE(!thread.enableEarlyWakeUp());

// stop the thread
thread.stop();
BipedalLocomotion::clock().sleepFor(period);

// try to resume the thread
REQUIRE(!thread.resume());
}

TEST_CASE("Test Periodic Thread", "[PeriodicThreadDeadlineMiss]")
{
auto period = 50ms;

// create
auto thread = ThreadWithDeadlineMiss();

// set the period
REQUIRE(thread.setPeriod(period));

// set the wait time in the run function to trigger a deadline miss
thread.setWaitTime(2 * period);

// set the maximum number of accepted deadline miss
REQUIRE(thread.setMaximumNumberOfAcceptedDeadlineMiss(2));

// start the thread
REQUIRE(thread.start());

BipedalLocomotion::clock().sleepFor(10 * period);

// check if the thread is stopped
REQUIRE(!thread.isRunning());

// check the number of deadline miss
REQUIRE(thread.getNumberOfDeadlineMiss() == 3);
}

0 comments on commit 2461257

Please sign in to comment.