[Base] Fix semaphore test

- Was using `Sleep()`
- Replaced with atomic value and bool flag
This commit is contained in:
Joel Linn 2021-05-18 16:07:17 +02:00 committed by Rick Gibbed
parent 2d9462f02b
commit 4d0d3f3ad4
1 changed files with 10 additions and 4 deletions

View File

@ -406,9 +406,13 @@ TEST_CASE("Wait on Semaphore", "Semaphore") {
sem = Semaphore::Create(5, 5);
Sleep(10ms);
// Occupy the semaphore with 5 threads
auto func = [&sem] {
std::atomic<int> wait_count(0);
volatile bool threads_terminate(false);
auto func = [&sem, &wait_count, &threads_terminate] {
auto res = Wait(sem.get(), false, 100ms);
Sleep(500ms);
wait_count++;
while (!threads_terminate) {
}
if (res == WaitResult::kSuccess) {
sem->Release(1, nullptr);
}
@ -417,12 +421,14 @@ TEST_CASE("Wait on Semaphore", "Semaphore") {
std::thread(func), std::thread(func), std::thread(func),
std::thread(func), std::thread(func),
};
// Give threads time to acquire semaphore
Sleep(10ms);
// Wait for threads to finish semaphore calls
while (wait_count != 5) {
}
// Attempt to acquire full semaphore with current (6th) thread
result = Wait(sem.get(), false, 20ms);
REQUIRE(result == WaitResult::kTimeout);
// Give threads time to release semaphore
threads_terminate = true;
for (auto& t : threads) {
t.join();
}