1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/common/tests/unit/task.cpp
Niklas Eiling 9ba78f1fe3 fix: move task to villas namespace
Signed-off-by: Niklas Eiling <niklas.eiling@eonerc.rwth-aachen.de>
2025-01-29 14:18:09 +01:00

72 lines
1.5 KiB
C++

/* Unit tests for periodic tasks.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <criterion/criterion.h>
#include <math.h>
#include <villas/task.hpp>
#include <villas/timing.hpp>
using namespace villas;
// cppcheck-suppress unknownMacro
TestSuite(task, .description = "Periodic timer tasks");
Test(task, rate, .timeout = 10) {
int runs = 10;
double rate = 5, waited;
struct timespec start, end;
Task task(CLOCK_MONOTONIC);
task.setRate(rate);
int i;
for (i = 0; i < runs; i++) {
clock_gettime(CLOCK_MONOTONIC, &start);
task.wait();
clock_gettime(CLOCK_MONOTONIC, &end);
waited = time_delta(&start, &end);
if (fabs(waited - 1.0 / rate) > 10e-3)
break;
}
if (i < runs)
cr_assert_float_eq(waited, 1.0 / rate, 1e-2,
"We slept for %f instead of %f secs in round %d", waited,
1.0 / rate, i);
}
Test(task, wait_until, .timeout = 5) {
int ret;
struct timespec start, end, diff, future;
Task task(CLOCK_REALTIME);
double waitfor = 3.423456789;
start = time_now();
diff = time_from_double(waitfor);
future = time_add(&start, &diff);
task.setNext(&future);
ret = task.wait();
end = time_now();
cr_assert_eq(ret, 1);
double waited = time_delta(&start, &end);
cr_assert_float_eq(waited, waitfor, 1e-2,
"We slept for %f instead of %f secs", waited, waitfor);
}