Precommit (#1)

* first commit

* cleanup
This commit is contained in:
tompzf
2025-11-04 13:28:06 +01:00
committed by GitHub
parent dba45dc636
commit 6ed4b1534e
898 changed files with 256340 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
# Define project (multiple EXEs)
project(TaskTimerTests VERSION 1.0 LANGUAGES CXX)
# TaskTimer executable
add_executable(ComponentTest_TaskTimer
task_timer_test.cpp
)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(ComponentTest_TaskTimer GTest::GTest ${CMAKE_THREAD_LIBS_INIT} stdc++fs)
if (WIN32)
target_link_libraries(ComponentTest_TaskTimer Winmm Ws2_32 Rpcrt4.lib)
else()
target_link_libraries(ComponentTest_TaskTimer rt ${CMAKE_DL_LIBS})
endif()
else()
target_link_libraries(ComponentTest_TaskTimer GTest::GTest Winmm Rpcrt4.lib)
endif()
# Add the tasktimer unittest
add_test(NAME ComponentTest_TaskTimer COMMAND ComponentTest_TaskTimer)
# Execute the test
add_custom_command(TARGET ComponentTest_TaskTimer POST_BUILD
COMMAND ${CMAKE_COMMAND} -E env TEST_EXECUTION_MODE=CMake "$<TARGET_FILE:ComponentTest_TaskTimer>" --gtest_output=xml:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ComponentTest_TaskTimer.xml
VERBATIM
)
# SimulationTaskTimer executable
add_executable(ComponentTest_Simulation_TaskTimer
task_simulation_timer_test.cpp
)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(ComponentTest_Simulation_TaskTimer GTest::GTest ${CMAKE_THREAD_LIBS_INIT} stdc++fs)
if (WIN32)
target_link_libraries(ComponentTest_Simulation_TaskTimer Winmm Ws2_32 Rpcrt4.lib)
else()
target_link_libraries(ComponentTest_Simulation_TaskTimer rt ${CMAKE_DL_LIBS})
endif()
else()
target_link_libraries(ComponentTest_Simulation_TaskTimer GTest::GTest Winmm Rpcrt4.lib)
endif()
# Add the tasktimer unittest
add_test(NAME ComponentTest_Simulation_TaskTimer COMMAND ComponentTest_Simulation_TaskTimer)
# Execute the test
add_custom_command(TARGET ComponentTest_Simulation_TaskTimer POST_BUILD
COMMAND ${CMAKE_COMMAND} -E env TEST_EXECUTION_MODE=CMake "$<TARGET_FILE:ComponentTest_Simulation_TaskTimer>" --gtest_output=xml:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ComponentTest_Simulation_TaskTimer.xml
VERBATIM
)
# copy the configuration to output folder 'config'
# and build dependencies
add_dependencies(ComponentTest_TaskTimer dependency_sdv_components)
add_dependencies(ComponentTest_TaskTimer task_timer)
add_dependencies(ComponentTest_Simulation_TaskTimer dependency_sdv_components)
add_dependencies(ComponentTest_Simulation_TaskTimer simulation_task_timer)
file (COPY ${PROJECT_SOURCE_DIR}/test_tt_config.toml DESTINATION ${CMAKE_BINARY_DIR}/tests/bin/config/)
file (COPY ${PROJECT_SOURCE_DIR}/test_simulation_tt_config.toml DESTINATION ${CMAKE_BINARY_DIR}/tests/bin/config/)

View File

@@ -0,0 +1,404 @@
#include "gtest/gtest.h"
#include <support/timer.h>
#include <chrono>
#include <thread>
#include <support/app_control.h>
#include "../../../global/process_watchdog.h"
#if defined(_WIN32) && defined(_UNICODE)
extern "C" int wmain(int argc, wchar_t* argv[])
#else
extern "C" int main(int argc, char* argv[])
#endif
{
CProcessWatchdog watchdog;
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
//test object which counts how many times the cb has been called
class CTestTask : public sdv::core::ITaskExecute, public sdv::IInterfaceAccess
{
public:
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(sdv::IInterfaceAccess)
SDV_INTERFACE_ENTRY(sdv::core::ITaskExecute)
END_SDV_INTERFACE_MAP()
std::atomic_bool m_bEnabled = true;
void Enable(bool bEnable) { m_bEnabled = bEnable; }
virtual void Execute() override
{
if (!m_bEnabled) return;
counter++;
}
uint32_t counter = 0;
};
TEST(TaskSimulationTimerTest, Initialization)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_simulation_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
appcontrol.SetRunningMode();
sdv::TInterfaceAccessPtr ptrDispatchService = sdv::core::GetObject("SimulationTaskTimerService");
sdv::core::ITaskTimer* pTimerSvc = ptrDispatchService.GetInterface<sdv::core::ITaskTimer>();
EXPECT_NE(pTimerSvc, nullptr);
appcontrol.Shutdown();
}
TEST(TaskSimulationTimerTest, BasicCounterTestInterface)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_simulation_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task;
sdv::core::CTaskTimer timer(3, &task);
EXPECT_TRUE(timer);
appcontrol.SetRunningMode();
// Get the simulation task timer service.
sdv::core::ITimerSimulationStep* pTimerSimulationStep = sdv::core::GetObject<sdv::core::ITimerSimulationStep>("SimulationTaskTimerService");
EXPECT_TRUE(pTimerSimulationStep);
uint32_t expected = 5;
uint32_t expectedAdditional = 11;
uint32_t loop = expected;
while (loop > 0) // 5 x 3 steps = 15 steps ==> 5 executions
{
loop--;
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
}
EXPECT_EQ(expected, task.counter);
loop = expected;
while (loop > 0) // 5 * 7 steps = 35 steps ==> 11 executions, 2 rest
{
loop--;
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
}
EXPECT_EQ(expected + expectedAdditional, task.counter);
pTimerSimulationStep->SimulationStep(1000); // one more step, one more execution
EXPECT_EQ(expected + expectedAdditional + 1, task.counter);
timer.Reset();
appcontrol.Shutdown();
}
TEST(TaskSimulationTimerTest, LargerTaskPeriodTest)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_simulation_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task150;
CTestTask task1150;
CTestTask task3100;
sdv::core::CTaskTimer timer150(150, &task150);
sdv::core::CTaskTimer timer1150(1150, &task1150);
sdv::core::CTaskTimer timer3100(3100, &task3100);
EXPECT_TRUE(timer150);
EXPECT_TRUE(timer1150);
EXPECT_TRUE(timer3100);
appcontrol.SetRunningMode();
// Get the simulation task timer service.
sdv::core::ITimerSimulationStep* pTimerSimulationStep = sdv::core::GetObject<sdv::core::ITimerSimulationStep>("SimulationTaskTimerService");
EXPECT_TRUE(pTimerSimulationStep);
uint32_t expected150 = 33;
uint32_t expected1150 = 4;
uint32_t expected3100 = 1;
uint32_t loop = 5000;
while (loop > 0) // 5 x 3 steps = 15 steps ==> 5 executions
{
loop--;
pTimerSimulationStep->SimulationStep(1000);
}
EXPECT_EQ(expected150, task150.counter);
EXPECT_EQ(expected1150, task1150.counter);
EXPECT_EQ(expected3100, task3100.counter);
loop = 7000;
while (loop > 0)
{
loop--;
pTimerSimulationStep->SimulationStep(1000);
}
EXPECT_EQ(expected150 + 47, task150.counter);
EXPECT_EQ(expected1150 + 6, task1150.counter);
EXPECT_EQ(expected3100 + 2, task3100.counter);
timer150.Reset();
timer1150.Reset();
timer3100.Reset();
appcontrol.Shutdown();
}
TEST(TaskSimulationTimerTest, MultipleTimerIdenticalTaskPeriodTest)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_simulation_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task1;
CTestTask task2;
CTestTask task3;
sdv::core::CTaskTimer timer1(3, &task1);
sdv::core::CTaskTimer timer2(3, &task2);
sdv::core::CTaskTimer timer3(3, &task3);
EXPECT_TRUE(timer1);
EXPECT_TRUE(timer2);
EXPECT_TRUE(timer3);
appcontrol.SetRunningMode();
// Get the simulation task timer service.
sdv::core::ITimerSimulationStep* pTimerSimulationStep = sdv::core::GetObject<sdv::core::ITimerSimulationStep>("SimulationTaskTimerService");
EXPECT_TRUE(pTimerSimulationStep);
uint32_t expected = 5;
uint32_t expectedAdditional = 11;
uint32_t loop = expected;
while (loop > 0) // 5 x 3 steps = 15 steps ==> 5 executions
{
loop--;
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
}
EXPECT_EQ(expected, task1.counter);
EXPECT_EQ(expected, task2.counter);
EXPECT_EQ(expected, task3.counter);
loop = expected;
while (loop > 0) // 5 * 7 steps = 35 steps ==> 11 executions, 2 rest
{
loop--;
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
}
EXPECT_EQ(expected + expectedAdditional, task1.counter);
EXPECT_EQ(expected + expectedAdditional, task2.counter);
EXPECT_EQ(expected + expectedAdditional, task3.counter);
pTimerSimulationStep->SimulationStep(1000); // one more step, one more execution
EXPECT_EQ(expected + expectedAdditional + 1, task1.counter);
EXPECT_EQ(expected + expectedAdditional + 1, task2.counter);
EXPECT_EQ(expected + expectedAdditional + 1, task3.counter);
timer1.Reset();
timer2.Reset();
timer3.Reset();
appcontrol.Shutdown();
}
TEST(TaskSimulationTimerTest, MultipleTimerDifferentTaskPeriodTest)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_simulation_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task2;
CTestTask task3;
CTestTask task4;
sdv::core::CTaskTimer timer2(2, &task2);
sdv::core::CTaskTimer timer3(3, &task3);
sdv::core::CTaskTimer timer4(4, &task4);
EXPECT_TRUE(timer2);
EXPECT_TRUE(timer3);
EXPECT_TRUE(timer4);
appcontrol.SetRunningMode();
// Get the simulation task timer service.
sdv::core::ITimerSimulationStep* pTimerSimulationStep = sdv::core::GetObject<sdv::core::ITimerSimulationStep>("SimulationTaskTimerService");
EXPECT_TRUE(pTimerSimulationStep);
uint32_t expected2 = 7;
uint32_t expected3 = 5;
uint32_t expected4 = 3;
uint32_t expectedAdditional2 = 18;
uint32_t expectedAdditional3 = 11;
uint32_t expectedAdditional4 = 9;
uint32_t loop = 5;
while (loop > 0) // 5 x 3 steps = 15 steps ==> 5 executions
{
loop--;
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
}
EXPECT_EQ(expected2, task2.counter);
EXPECT_EQ(expected3, task3.counter);
EXPECT_EQ(expected4, task4.counter);
loop = 5;
while (loop > 0)
{
loop--;
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
pTimerSimulationStep->SimulationStep(1000);
}
EXPECT_EQ(expected2 + expectedAdditional2, task2.counter);
EXPECT_EQ(expected3 + expectedAdditional3, task3.counter);
EXPECT_EQ(expected4 + expectedAdditional4, task4.counter);
pTimerSimulationStep->SimulationStep(1000);
EXPECT_EQ(expected2 + expectedAdditional2 + 0, task2.counter);
EXPECT_EQ(expected3 + expectedAdditional3 + 1, task3.counter);
EXPECT_EQ(expected4 + expectedAdditional4 + 0, task4.counter);
timer2.Reset();
timer3.Reset();
timer4.Reset();
appcontrol.Shutdown();
}
TEST(TaskSimulationTimerTest, MultipleTimerLargeSimulationStepTest)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_simulation_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task1;
CTestTask task150;
CTestTask task450;
sdv::core::CTaskTimer timer1(1, &task1);
sdv::core::CTaskTimer timer150(150, &task150);
sdv::core::CTaskTimer timer450(450, &task450);
EXPECT_TRUE(timer1);
EXPECT_TRUE(timer150);
EXPECT_TRUE(timer450);
appcontrol.SetRunningMode();
// Get the simulation task timer service.
sdv::core::ITimerSimulationStep* pTimerSimulationStep = sdv::core::GetObject<sdv::core::ITimerSimulationStep>("SimulationTaskTimerService");
EXPECT_TRUE(pTimerSimulationStep);
uint32_t expected1 = 480;
uint32_t expected150 = 3;
uint32_t expected450 = 1;
pTimerSimulationStep->SimulationStep(80000);
pTimerSimulationStep->SimulationStep(80000);
pTimerSimulationStep->SimulationStep(80000);
pTimerSimulationStep->SimulationStep(80000);
pTimerSimulationStep->SimulationStep(80000);
pTimerSimulationStep->SimulationStep(80000);
EXPECT_EQ(expected1, task1.counter);
EXPECT_EQ(expected150, task150.counter);
EXPECT_EQ(expected450, task450.counter);
timer1.Reset();
timer150.Reset();
timer450.Reset();
appcontrol.Shutdown();
}
TEST(TaskSimulationTimerTest, MultipleTimerLargeAndDifferentSimulationStepTest)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_simulation_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task1;
CTestTask task150;
CTestTask task450;
sdv::core::CTaskTimer timer1(1, &task1);
sdv::core::CTaskTimer timer150(150, &task150);
sdv::core::CTaskTimer timer450(450, &task450);
EXPECT_TRUE(timer1);
EXPECT_TRUE(timer150);
EXPECT_TRUE(timer450);
appcontrol.SetRunningMode();
// Get the simulation task timer service.
sdv::core::ITimerSimulationStep* pTimerSimulationStep = sdv::core::GetObject<sdv::core::ITimerSimulationStep>("SimulationTaskTimerService");
EXPECT_TRUE(pTimerSimulationStep);
uint32_t expected1 = 480;
uint32_t expected150 = 3;
uint32_t expected450 = 1;
pTimerSimulationStep->SimulationStep(67000);
pTimerSimulationStep->SimulationStep(99000);
pTimerSimulationStep->SimulationStep(72000);
pTimerSimulationStep->SimulationStep(64000);
pTimerSimulationStep->SimulationStep(85000);
pTimerSimulationStep->SimulationStep(93000);
EXPECT_EQ(expected1, task1.counter);
EXPECT_EQ(expected150, task150.counter);
EXPECT_EQ(expected450, task450.counter);
timer1.Reset();
timer150.Reset();
timer450.Reset();
appcontrol.Shutdown();
}

View File

@@ -0,0 +1,267 @@
#include "gtest/gtest.h"
#include <support/timer.h>
#include <chrono>
#include <thread>
#include <support/app_control.h>
#include "../../../global/process_watchdog.h"
static uint32_t TimeTolerance = 50; //tolerate a small delay for task execution
#if defined(_WIN32) && defined(_UNICODE)
extern "C" int wmain(int argc, wchar_t* argv[])
#else
extern "C" int main(int argc, char* argv[])
#endif
{
CProcessWatchdog watchdog;
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
//test object which counts how many times the cb has been called
class CTestTask : public sdv::core::ITaskExecute, public sdv::IInterfaceAccess
{
public:
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(sdv::IInterfaceAccess)
SDV_INTERFACE_ENTRY(sdv::core::ITaskExecute)
END_SDV_INTERFACE_MAP()
std::atomic_bool m_bEnabled = true;
void Enable(bool bEnable) { m_bEnabled = bEnable; }
virtual void Execute() override
{
if (!m_bEnabled) return;
std::unique_lock<std::mutex> lock(mtxTimes);
vecTimes.push_back(std::chrono::high_resolution_clock::now());
counter++;
}
double CalcAvrgTime()
{
if (vecTimes.empty()) return 0.0;
double dTotal = 0.0;
for (size_t n = 1; n < vecTimes.size(); n++)
dTotal += std::chrono::duration<double>(vecTimes[n] - vecTimes[n - 1]).count();
return dTotal / static_cast<double>(vecTimes.size() - 1);
}
std::atomic<uint64_t> counter = 0ull;
std::mutex mtxTimes;
std::vector<std::chrono::high_resolution_clock::time_point> vecTimes;
};
TEST(TaskTimerTest, Initialization)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
appcontrol.SetRunningMode();
sdv::TInterfaceAccessPtr ptrDispatchService = sdv::core::GetObject("TaskTimerService");
sdv::core::ITaskTimer* pTimerSvc = ptrDispatchService.GetInterface<sdv::core::ITaskTimer>();
EXPECT_NE(pTimerSvc, nullptr);
appcontrol.Shutdown();
}
TEST(TaskTimerTest, BasicCounterTestInterface)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task;
sdv::core::CTaskTimer timer(100, &task);
EXPECT_TRUE(timer);
appcontrol.SetRunningMode();
std::chrono::milliseconds sleepDuration (500 + TimeTolerance);
std::this_thread::sleep_for(sleepDuration);
timer.Reset();
EXPECT_EQ(5ull, task.counter);
sleepDuration = std::chrono::milliseconds(20 + TimeTolerance);
std::this_thread::sleep_for(sleepDuration);
EXPECT_EQ(5ull, task.counter);
appcontrol.Shutdown();
}
TEST(TaskTimerTest, BasicCounterTestFunction)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task;
sdv::core::CTaskTimer timer(100, [&]() {task.Execute(); });
EXPECT_TRUE(timer);
appcontrol.SetRunningMode();
std::chrono::milliseconds sleepDuration (500 + TimeTolerance);
std::this_thread::sleep_for(sleepDuration);
timer.Reset();
EXPECT_EQ(5ull, task.counter);
sleepDuration = std::chrono::milliseconds(20 + TimeTolerance);
std::this_thread::sleep_for(sleepDuration);
EXPECT_EQ(5ull, task.counter);
// 1ms buffer
if (0.101 <= task.CalcAvrgTime())
std::cout << __FILE__ << ":" << __LINE__ << ":" << "Warning" << std::endl <<
"Expected: (0.101) > (task.CalcAvrgTime()), actual: " <<
(0.101) << "vs" << task.CalcAvrgTime() << std::endl;
//EXPECT_GT(0.101, task.CalcAvrgTime());
if (0.099 >= task.CalcAvrgTime())
std::cout << __FILE__ << ":" << __LINE__ << ":" << "Warning" << std::endl <<
"Expected: (0.099) < (task.CalcAvrgTime()), actual: " <<
(0.099) << "vs" << task.CalcAvrgTime() << std::endl;
//EXPECT_LT(0.099, task.CalcAvrgTime());
appcontrol.Shutdown();
}
TEST(TaskTimerTest, LongPeriodTest)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task;
sdv::core::CTaskTimer timer(1500, &task);
EXPECT_TRUE(timer);
appcontrol.SetRunningMode();
std::chrono::milliseconds sleepDuration (3000 + TimeTolerance);
std::this_thread::sleep_for(sleepDuration);
timer.Reset();
EXPECT_EQ(2ull, task.counter);
// 1ms buffer
if (1.501 <= task.CalcAvrgTime())
std::cout << __FILE__ << ":" << __LINE__ << ":" << "Warning" << std::endl <<
"Expected: (1.501) > (task.CalcAvrgTime()), actual: " <<
(1.501) << "vs" << task.CalcAvrgTime() << std::endl;
//EXPECT_GT(1.501, task.CalcAvrgTime());
if (1.499 >= task.CalcAvrgTime())
std::cout << __FILE__ << ":" << __LINE__ << ":" << "Warning" << std::endl <<
"Expected: (1.499) < (task.CalcAvrgTime()), actual: " <<
(1.499) << "vs" << task.CalcAvrgTime() << std::endl;
//EXPECT_LT(1.499, task.CalcAvrgTime());
appcontrol.Shutdown();
}
TEST(TaskTimerTest, SpawnTaskTwice)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
CTestTask task;
sdv::core::CTaskTimer timer1(100, &task);
sdv::core::CTaskTimer timer2(100, &task);
EXPECT_TRUE(timer1);
EXPECT_TRUE(timer2);
appcontrol.SetRunningMode();
std::chrono::milliseconds sleepDuration (500 + TimeTolerance);
std::this_thread::sleep_for(sleepDuration);
//shutdown both tasks here
timer1.Reset();
timer2.Reset();
//check for counter - expectation: twice the count
EXPECT_EQ(10ull, task.counter);
appcontrol.Shutdown();
}
TEST(TaskTimerTest, MultiTaskTest)
{
sdv::app::CAppControl appcontrol;
bool bResult = appcontrol.Startup("");
EXPECT_TRUE(bResult);
appcontrol.SetConfigMode();
sdv::core::EConfigProcessResult eResult = appcontrol.LoadConfig("test_tt_config.toml");
EXPECT_EQ(eResult, sdv::core::EConfigProcessResult::successful);
uint32_t rguiPeriods[] = { 100, 133, 150, 250, 333, 50, 73, 120, 10, 415, 115, 60, 5/*, 1*/ };
const size_t nExtent = std::extent_v<decltype(rguiPeriods)>;
CTestTask rgtask[nExtent];
sdv::core::CTaskTimer rgtimer[nExtent];
for (size_t n = 0; n < nExtent; n++)
{
rgtask[n].Enable(false);
rgtimer[n] = sdv::core::CTaskTimer(rguiPeriods[n], rgtask + n);
EXPECT_TRUE(rgtimer[n]);
}
appcontrol.SetRunningMode();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
for (size_t n = 0; n < std::extent_v<decltype(rguiPeriods)>; n++)
{
rgtask[n].Enable(true);
}
std::chrono::milliseconds sleepDuration(1500 + TimeTolerance);
std::this_thread::sleep_for(sleepDuration);
// NOTE: If running in a virtual environment, the constraints cannot be kept.
for (size_t n = 0; n < nExtent; n++)
{
rgtimer[n].Reset();
EXPECT_FALSE(rgtimer[n]);
if (rgtask[n].counter < 1500 / rguiPeriods[n])
std::cout << __FILE__ << ":" << __LINE__ << ":" << "Warning" << std::endl <<
"Expected: (rgtask[n].counter) >= (1500 / (rguiPeriods[n]), actual: " << rgtask[n].counter << "vs" << (1500 / rguiPeriods[n]) << std::endl;
//EXPECT_GE(rgtask[n].counter, 1500 / rguiPeriods[n]);
// 1ms buffer
if ((static_cast<double>(rguiPeriods[n]) / 1000.0 + 0.001) <= rgtask[n].CalcAvrgTime())
std::cout << __FILE__ << ":" << __LINE__ << ":" << "Warning" << std::endl <<
"Expected: (static_cast<double>(rguiPeriods[n]) / 1000.0 + 0.001) > (rgtask[n].CalcAvrgTime()), actual: " <<
(static_cast<double>(rguiPeriods[n]) / 1000.0 + 0.001) << "vs" << rgtask[n].CalcAvrgTime() << std::endl;
//EXPECT_GT(static_cast<double>(rguiPeriods[n]) / 1000.0 + 0.001, rgtask[n].CalcAvrgTime());
if ((static_cast<double>(rguiPeriods[n]) / 1000.0 - 0.001) >= rgtask[n].CalcAvrgTime())
std::cout << __FILE__ << ":" << __LINE__ << ":" << "Warning" << std::endl <<
"Expected: (static_cast<double>(rguiPeriods[n]) / 1000.0 - 0.001) < (rgtask[n].CalcAvrgTime()), actual: " <<
(static_cast<double>(rguiPeriods[n]) / 1000.0 + 0.001) << "vs" << rgtask[n].CalcAvrgTime() << std::endl;
//EXPECT_LT(static_cast<double>(rguiPeriods[n]) / 1000.0 - 0.001, rgtask[n].CalcAvrgTime());
}
appcontrol.Shutdown();
}

View File

@@ -0,0 +1,7 @@
[Configuration]
Version = 100
[[Component]]
Path = "simulation_task_timer.sdv"
Class = "SimulationTaskTimerService"

View File

@@ -0,0 +1,6 @@
[Configuration]
Version = 100
[[Component]]
Path = "task_timer.sdv"
Class = "TaskTimerService"