[Base] Check for correct thread in HResTimer tests

This commit is contained in:
Joel Linn 2022-03-01 23:41:57 +01:00 committed by Rick Gibbed
parent fb741db2fe
commit e0f34b97fb

View file

@ -180,12 +180,21 @@ TEST_CASE("HighResolutionTimer") {
// Smaller values are not as precise and fail the test // Smaller values are not as precise and fail the test
const auto wait_time = 500ms; const auto wait_time = 500ms;
const Thread* timer_thread = nullptr;
// Time the actual sleep duration // Time the actual sleep duration
{ {
const auto interval = 50ms; const auto interval = 50ms;
std::atomic<uint64_t> counter(0); std::atomic<uint64_t> counter(0);
auto start = std::chrono::steady_clock::now(); auto start = std::chrono::steady_clock::now();
auto cb = [&counter] { ++counter; }; auto cb = [&counter, &timer_thread] {
if (counter == 0) {
timer_thread = Thread::GetCurrentThread();
} else {
REQUIRE(Thread::GetCurrentThread() == timer_thread);
}
++counter;
};
auto pTimer = HighResolutionTimer::CreateRepeating(interval, cb); auto pTimer = HighResolutionTimer::CreateRepeating(interval, cb);
Sleep(wait_time); Sleep(wait_time);
pTimer.reset(); pTimer.reset();
@ -206,8 +215,14 @@ TEST_CASE("HighResolutionTimer") {
std::atomic<uint64_t> counter1(0); std::atomic<uint64_t> counter1(0);
std::atomic<uint64_t> counter2(0); std::atomic<uint64_t> counter2(0);
auto start = std::chrono::steady_clock::now(); auto start = std::chrono::steady_clock::now();
auto cb1 = [&counter1] { ++counter1; }; auto cb1 = [&counter1, timer_thread] {
auto cb2 = [&counter2] { ++counter2; }; ++counter1;
REQUIRE(Thread::GetCurrentThread() == timer_thread);
};
auto cb2 = [&counter2, timer_thread] {
++counter2;
REQUIRE(Thread::GetCurrentThread() == timer_thread);
};
auto pTimer1 = HighResolutionTimer::CreateRepeating(interval1, cb1); auto pTimer1 = HighResolutionTimer::CreateRepeating(interval1, cb1);
auto pTimer2 = HighResolutionTimer::CreateRepeating(interval2, cb2); auto pTimer2 = HighResolutionTimer::CreateRepeating(interval2, cb2);
Sleep(wait_time); Sleep(wait_time);