|
|
|
/**
|
|
|
|
*
|
|
|
|
* \section COPYRIGHT
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 Software Radio Systems Limited
|
|
|
|
*
|
|
|
|
* By using this file, you agree to the terms and conditions set
|
|
|
|
* forth in the LICENSE file which can be found at the top level of
|
|
|
|
* the distribution.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "srsran/common/task_scheduler.h"
|
|
|
|
#include "srsran/common/test_common.h"
|
|
|
|
|
|
|
|
enum class task_result { null, internal, external, timer };
|
|
|
|
|
|
|
|
int test_task_scheduler_no_pool()
|
|
|
|
{
|
|
|
|
srsran::task_scheduler task_sched{5, 0};
|
|
|
|
task_result state = task_result::null;
|
|
|
|
|
|
|
|
// TEST: deferring task does not run the task until the next tic
|
|
|
|
task_sched.defer_task([&state]() { state = task_result::internal; });
|
|
|
|
TESTASSERT(state == task_result::null);
|
|
|
|
task_sched.run_pending_tasks();
|
|
|
|
TESTASSERT(state == task_result::internal);
|
|
|
|
|
|
|
|
// TEST: check delaying of task
|
|
|
|
state = task_result::null;
|
|
|
|
int dur = 5;
|
|
|
|
task_sched.defer_callback(dur, [&state]() { state = task_result::timer; });
|
|
|
|
for (int i = 0; i < dur; ++i) {
|
|
|
|
TESTASSERT(state == task_result::null);
|
|
|
|
task_sched.tic();
|
|
|
|
task_sched.run_pending_tasks();
|
|
|
|
}
|
|
|
|
TESTASSERT(state == task_result::timer);
|
|
|
|
|
|
|
|
// TEST: background task is run, despite there are no pool workers
|
|
|
|
state = task_result::null;
|
|
|
|
srsran::get_background_workers().push_task([&task_sched, &state]() {
|
|
|
|
task_sched.notify_background_task_result([&state]() { state = task_result::external; });
|
|
|
|
});
|
|
|
|
TESTASSERT(state == task_result::null);
|
|
|
|
while (state != task_result::external) {
|
|
|
|
task_sched.run_pending_tasks(); // runs notification
|
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
|
|
|
}
|
|
|
|
TESTASSERT(state == task_result::external);
|
|
|
|
|
|
|
|
return SRSRAN_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_task_scheduler_with_pool()
|
|
|
|
{
|
|
|
|
srsran::task_scheduler task_sched{5, 2};
|
|
|
|
task_result state = task_result::null;
|
|
|
|
|
|
|
|
srsran::get_background_workers().push_task([&task_sched, &state]() {
|
|
|
|
task_sched.notify_background_task_result([&state]() { state = task_result::external; });
|
|
|
|
});
|
|
|
|
TESTASSERT(state == task_result::null);
|
|
|
|
while (state != task_result::external) {
|
|
|
|
task_sched.run_pending_tasks(); // runs notification
|
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
|
|
|
}
|
|
|
|
|
|
|
|
return SRSRAN_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
TESTASSERT(test_task_scheduler_no_pool() == SRSRAN_SUCCESS);
|
|
|
|
TESTASSERT(test_task_scheduler_with_pool() == SRSRAN_SUCCESS);
|
|
|
|
}
|