Skip to content

Commit

Permalink
[Tests/unit/core] : Fix build 'test_threadpool'. (rdkcentral#1568)
Browse files Browse the repository at this point in the history
- '5d54a0b25 ‘ and 'bef366339' removed ‘Runs’ and ‘Active’, and, introduced ‘Snapshot’.

Co-authored-by: Pierre Wielders <[email protected]>
  • Loading branch information
msieben and pwielders authored Apr 22, 2024
1 parent d60aed5 commit c0bfab6
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Tests/unit/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ add_executable(${TEST_RUNNER_NAME}
test_textfragment.cpp
test_textreader.cpp
test_thread.cpp
#test_threadpool.cpp
test_threadpool.cpp
test_time.cpp
#test_timer.cpp
test_tristate.cpp
Expand Down
141 changes: 93 additions & 48 deletions Tests/unit/core/test_threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class ThreadPoolTester : public EventControl, public JobControl<ThreadPoolTester
ThreadPoolTester& operator=(const ThreadPoolTester&) = delete;
ThreadPoolTester(const uint8_t count, const uint32_t stackSize, const uint32_t queueSize)
: JobControl(*this)
, ThreadPool(count, stackSize, queueSize, &_dispatcher, &_scheduler)
, ThreadPool(count, stackSize, queueSize, &_dispatcher, &_scheduler, nullptr, nullptr)
, _queueSize(queueSize)
, _dispatcher()
, _scheduler(*this)
Expand Down Expand Up @@ -425,16 +425,18 @@ TEST(Core_ThreadPool, CheckMinion_ProcessJob)

Core::ProxyType<Core::IDispatch> job = Core::ProxyType<Core::IDispatch>(Core::ProxyType<TestJob<MinionTester>>::Create(minion, TestJob<MinionTester>::INITIATED, 500));
EXPECT_EQ(static_cast<TestJob<MinionTester>&>(*job).GetStatus(), TestJob<MinionTester>::INITIATED);
EXPECT_EQ(minion.IsActive(), false);
minion.Submit(job);
EXPECT_EQ(minion.IsActive(), false);
minion.Run();

EXPECT_EQ(minion.WaitForJobEvent(job, MaxJobWaitTime * 2), Core::ERROR_NONE);
minion.NotifyReady(job);
minion.Shutdown();
EXPECT_EQ(static_cast<TestJob<MinionTester>&>(*job).GetStatus(), TestJob<MinionTester>::COMPLETED);
EXPECT_EQ(minion.Runs(), 1u);

WPEFramework::Core::ThreadPool::Metadata info;
minion.Info(info);
EXPECT_EQ(info.Runs, 1u);

job.Release();
}
TEST(Core_ThreadPool, CheckMinion_ProcessJob_CheckActiveStateInBetweenDispatch)
Expand All @@ -455,7 +457,11 @@ TEST(Core_ThreadPool, CheckMinion_ProcessJob_CheckActiveStateInBetweenDispatch)
minion.Shutdown();

EXPECT_EQ(static_cast<TestJob<MinionTester>&>(*job).GetStatus(), TestJob<MinionTester>::COMPLETED);
EXPECT_EQ(minion.Runs(), 1u);

WPEFramework::Core::ThreadPool::Metadata info;
minion.Info(info);
EXPECT_EQ(info.Runs, 1u);

job.Release();
}
TEST(Core_ThreadPool, CheckMinion_CancelJob_BeforeProcessing)
Expand All @@ -472,7 +478,11 @@ TEST(Core_ThreadPool, CheckMinion_CancelJob_BeforeProcessing)
minion.Revoke(job);
EXPECT_EQ(minion.IsActive(), false);
EXPECT_EQ(static_cast<TestJob<MinionTester>&>(*job).GetStatus(), TestJob<MinionTester>::CANCELED);
EXPECT_EQ(minion.Runs(), 0u);

WPEFramework::Core::ThreadPool::Metadata info;
minion.Info(info);
EXPECT_EQ(info.Runs, 0u);

job.Release();
}
TEST(Core_ThreadPool, CheckMinion_CancelJob_WhileProcessing)
Expand All @@ -497,7 +507,11 @@ TEST(Core_ThreadPool, CheckMinion_CancelJob_WhileProcessing)
minion.Shutdown();
EXPECT_EQ(minion.IsActive(), false);
EXPECT_EQ(static_cast<TestJob<MinionTester>&>(*job).GetStatus(), TestJob<MinionTester>::COMPLETED);
EXPECT_EQ(minion.Runs(), 1u);

WPEFramework::Core::ThreadPool::Metadata info;
minion.Info(info);
EXPECT_EQ(info.Runs, 1u);

job.Release();
}
TEST(Core_ThreadPool, CheckMinion_CancelJob_WhileProcessing_ByAddingWaitOnTheDispatcher)
Expand All @@ -516,7 +530,11 @@ TEST(Core_ThreadPool, CheckMinion_CancelJob_WhileProcessing_ByAddingWaitOnTheDis
EXPECT_EQ(minion.IsActive(), true);
minion.NotifyReady(job);
EXPECT_EQ(static_cast<TestJob<MinionTester>&>(*job).GetStatus(), TestJob<MinionTester>::COMPLETED);
EXPECT_EQ(minion.Runs(), 1u);

WPEFramework::Core::ThreadPool::Metadata info;
minion.Info(info);
EXPECT_EQ(info.Runs, 1u);

minion.Shutdown();
usleep(100);
EXPECT_EQ(minion.IsActive(), false);
Expand Down Expand Up @@ -555,7 +573,9 @@ TEST(Core_ThreadPool, CheckMinion_ProcessMultipleJobs)
EXPECT_EQ(static_cast<TestJob<MinionTester>&>(*job).GetStatus(), TestJob<MinionTester>::COMPLETED);
}

EXPECT_EQ(minion.Runs(), 6u);
WPEFramework::Core::ThreadPool::Metadata info;
minion.Info(info);
EXPECT_EQ(info.Runs, 6u);

for (auto& job: jobs) {
job.Release();
Expand Down Expand Up @@ -607,7 +627,9 @@ TEST(Core_ThreadPool, CheckMinion_ProcessMultipleJobs_CancelInBetween)

minion.Shutdown();

EXPECT_EQ(minion.Runs(), queueSize);
WPEFramework::Core::ThreadPool::Metadata info;
minion.Info(info);
EXPECT_EQ(info.Runs, queueSize);

for (auto& job: jobs) {
job.Release();
Expand All @@ -622,16 +644,13 @@ TEST(Core_ThreadPool, CheckThreadPool_ProcessJob)
ThreadPoolTester threadPool(threadCount, 0, queueSize);
Core::ProxyType<Core::IDispatch> job = Core::ProxyType<Core::IDispatch>(Core::ProxyType<TestJob<ThreadPoolTester>>::Create(threadPool, TestJob<ThreadPoolTester>::INITIATED, 500));
EXPECT_EQ(static_cast<TestJob<ThreadPoolTester>&>(*job).GetStatus(), TestJob<ThreadPoolTester>::INITIATED);
EXPECT_EQ(threadPool.Active(), false);
threadPool.Submit(job, 0);
EXPECT_EQ(threadPool.Pending(), queueSize);
EXPECT_EQ(threadPool.QueueIsEmpty(), false);
EXPECT_EQ(threadPool.Active(), false);
threadPool.Run();
while(threadPool.QueueIsEmpty() != true) {
__asm__ volatile("nop");
}
EXPECT_EQ(threadPool.Active(), true);
EXPECT_EQ(threadPool.WaitForJobEvent(job, MaxJobWaitTime), Core::ERROR_NONE);
EXPECT_EQ(threadPool.Pending(), 0u);
threadPool.Stop();
Expand All @@ -648,15 +667,12 @@ TEST(Core_ThreadPool, CheckThreadPool_RevokeJob)

Core::ProxyType<Core::IDispatch> job = Core::ProxyType<Core::IDispatch>(Core::ProxyType<TestJob<ThreadPoolTester>>::Create(threadPool, TestJob<ThreadPoolTester>::INITIATED));
EXPECT_EQ(static_cast<TestJob<ThreadPoolTester>&>(*job).GetStatus(), TestJob<ThreadPoolTester>::INITIATED);
EXPECT_EQ(threadPool.Active(), false);
threadPool.Submit(job, 0);
EXPECT_EQ(threadPool.Pending(), queueSize);
threadPool.Revoke(job, 0);
EXPECT_EQ(threadPool.Pending(), 0u);
EXPECT_EQ(threadPool.QueueIsEmpty(), true);
EXPECT_EQ(threadPool.Active(), false);
threadPool.Run();
EXPECT_EQ(threadPool.Active(), false);
threadPool.Stop();

EXPECT_EQ(static_cast<TestJob<ThreadPoolTester>&>(*job).GetStatus(), TestJob<ThreadPoolTester>::INITIATED);
Expand All @@ -670,17 +686,14 @@ TEST(Core_ThreadPool, CheckThreadPool_CancelJob_WhileProcessing)

Core::ProxyType<Core::IDispatch> job = Core::ProxyType<Core::IDispatch>(Core::ProxyType<TestJob<ThreadPoolTester>>::Create(threadPool, TestJob<ThreadPoolTester>::INITIATED, 1000));
EXPECT_EQ(static_cast<TestJob<ThreadPoolTester>&>(*job).GetStatus(), TestJob<ThreadPoolTester>::INITIATED);
EXPECT_EQ(threadPool.Active(), false);
threadPool.Submit(job, 0);
EXPECT_EQ(threadPool.Pending(), queueSize);
EXPECT_EQ(threadPool.QueueIsEmpty(), false);
EXPECT_EQ(threadPool.Active(), false);
threadPool.Run();
EXPECT_EQ(threadPool.QueueIsEmpty(), false);
while(threadPool.QueueIsEmpty() != true) {
__asm__ volatile("nop");
}
EXPECT_EQ(threadPool.Active(), true);
threadPool.Revoke(job, 0);
EXPECT_EQ(threadPool.Pending(), 0u);
EXPECT_EQ(threadPool.WaitForJobEvent(job, MaxJobWaitTime * 3), Core::ERROR_NONE);
Expand Down Expand Up @@ -721,11 +734,15 @@ void CheckThreadPool_ProcessMultipleJobs(const uint8_t threadCount, const uint8_
EXPECT_EQ(static_cast<TestJob<ThreadPoolTester>&>(*job).GetStatus(), TestJob<ThreadPoolTester>::COMPLETED);
}

uint32_t counters[threadCount] = {0};
uint8_t totalRuns = 0;
threadPool.Runs(threadCount, counters);

WPEFramework::Core::ThreadPool::Metadata info[threadCount];
std::vector<string> jobsStrings;

threadPool.Snapshot(threadCount, info, jobsStrings);

for (uint8_t i = 0; i < threadCount; ++i) {
totalRuns += counters[i];
totalRuns += info[i].Runs;
}
EXPECT_EQ(totalRuns, static_cast<uint32_t>(queueSize + additionalJobs));

Expand Down Expand Up @@ -800,14 +817,18 @@ TEST(Core_ThreadPool, CheckThreadPool_ProcessMultipleJobs_CancelInBetween)
}
}

uint32_t counters[threadCount] = {0};
threadPool.Runs(threadCount, counters);
uint8_t totalRuns = 0;

WPEFramework::Core::ThreadPool::Metadata info[threadCount];
std::vector<string> jobsStrings;

threadPool.Snapshot(threadCount, info, jobsStrings);

for (uint8_t i = 0; i < threadCount; ++i) {
totalRuns += counters[i];
totalRuns += info[i].Runs;
}

EXPECT_EQ(totalRuns, queueSize + additionalJobs - 1);

threadPool.Stop();

for (auto& job: jobs) {
Expand Down Expand Up @@ -854,13 +875,17 @@ void CheckThreadPool_ProcessMultipleJobs_CancelInBetween_WithMultiplePool(const
}

uint8_t totalRuns = 0;
uint32_t counters[threadCount] = {0};
threadPool.Runs(threadCount, counters);

WPEFramework::Core::ThreadPool::Metadata info[threadCount];
std::vector<string> jobsStrings;

threadPool.Snapshot(threadCount, info, jobsStrings);

for (uint8_t i = 0; i < threadCount; ++i) {
totalRuns += counters[i];
totalRuns += info[i].Runs;
}

EXPECT_EQ(totalRuns, queueSize + additionalJobs);

threadPool.Stop();

for (auto& job: jobs) {
Expand Down Expand Up @@ -1002,13 +1027,17 @@ void CheckThreadPool_JobType_Submit_Using_Idle(const uint8_t threadCount, const
}

uint8_t totalRuns = 0;
uint32_t counters[threadCount] = {0};
threadPool.Runs(threadCount, counters);

WPEFramework::Core::ThreadPool::Metadata info[threadCount];
std::vector<string> jobsStrings;

threadPool.Snapshot(threadCount, info, jobsStrings);

for (uint8_t i = 0; i < threadCount; ++i) {
totalRuns += counters[i];
totalRuns += info[i].Runs;
}

EXPECT_EQ(totalRuns, queueSize + additionalJobs - cancelJobsCount);

threadPool.Stop();

for (auto& job: jobs) {
Expand Down Expand Up @@ -1114,13 +1143,17 @@ void CheckThreadPool_JobType_Submit_Using_Submit(const uint8_t threadCount, cons
}

uint8_t totalRuns = 0;
uint32_t counters[threadCount] = {0};
threadPool.Runs(threadCount, counters);

WPEFramework::Core::ThreadPool::Metadata info[threadCount];
std::vector<string> jobsStrings;

threadPool.Snapshot(threadCount, info, jobsStrings);

for (uint8_t i = 0; i < threadCount; ++i) {
totalRuns += counters[i];
totalRuns += info[i].Runs;
}

EXPECT_EQ(totalRuns, queueSize + additionalJobs - cancelJobsCount);

threadPool.Stop();

for (auto& job: jobs) {
Expand Down Expand Up @@ -1221,13 +1254,17 @@ void CheckThreadPool_JobType_Submit_Using_Reschedule(const uint8_t threadCount,
}

uint8_t totalRuns = 0;
uint32_t counters[threadCount] = {0};
threadPool.Runs(threadCount, counters);

WPEFramework::Core::ThreadPool::Metadata info[threadCount];
std::vector<string> jobsStrings;

threadPool.Snapshot(threadCount, info, jobsStrings);

for (uint8_t i = 0; i < threadCount; ++i) {
totalRuns += counters[i];
totalRuns += info[i].Runs;
}

EXPECT_EQ(totalRuns, (queueSize + additionalJobs - cancelJobsCount) * 2);

threadPool.Stop();

for (auto& job: jobs) {
Expand Down Expand Up @@ -1314,10 +1351,14 @@ TEST(Core_ThreadPool, CheckThreadPool_JobType_Reschedule_AfterSubmit)
}

uint8_t totalRuns = 0;
uint32_t counters[threadCount] = {0};
threadPool.Runs(threadCount, counters);

WPEFramework::Core::ThreadPool::Metadata info[threadCount];
std::vector<string> jobsStrings;

threadPool.Snapshot(threadCount, info, jobsStrings);

for (uint8_t i = 0; i < threadCount; ++i) {
totalRuns += counters[i];
totalRuns += info[i].Runs;
}
EXPECT_EQ(totalRuns, (queueSize + additionalJobs) * 2);

Expand Down Expand Up @@ -1373,10 +1414,14 @@ TEST(Core_ThreadPool, CheckThreadPool_JobType_Reschedule_AfterIdle)
}

uint8_t totalRuns = 0;
uint32_t counters[threadCount] = {0};
threadPool.Runs(threadCount, counters);

WPEFramework::Core::ThreadPool::Metadata info[threadCount];
std::vector<string> jobsStrings;

threadPool.Snapshot(threadCount, info, jobsStrings);

for (uint8_t i = 0; i < threadCount; ++i) {
totalRuns += counters[i];
totalRuns += info[i].Runs;
}
EXPECT_EQ(totalRuns, (queueSize + additionalJobs) * 2);

Expand Down

0 comments on commit c0bfab6

Please sign in to comment.