Skip to content

Commit

Permalink
test: add tests for thread_safe_queue::clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
samangh committed Aug 14, 2024
1 parent a3f5480 commit 2ccde50
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/source/thread_safe_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,38 @@ TEST_CASE("Ensure insert and pop works with thread contention") {
CHECK_NE(res2, res3);
CHECK_NE(res3, res1);
}

TEST_CASE("Ensure clear() works and returns correct count") {
// create a synchronization barrier to ensure our threads have started before executing code to
// clear the queue

// here, we check that:
// - the queue is cleared
// - that clear() return the correct number

std::barrier barrier(3);
std::atomic<size_t> removed_count{0};

dp::thread_safe_queue<int> queue;
{
std::jthread t1([&queue, &barrier, &removed_count] {
queue.push_front(1);
barrier.arrive_and_wait();
removed_count = queue.clear();
barrier.arrive_and_wait();
});
std::jthread t2([&queue, &barrier] {
queue.push_front(2);
barrier.arrive_and_wait();
barrier.arrive_and_wait();
});
std::jthread t3([&queue, &barrier] {
queue.push_front(3);
barrier.arrive_and_wait();
barrier.arrive_and_wait();
});
}

CHECK(queue.empty());
CHECK_EQ(removed_count, 3);
}

0 comments on commit 2ccde50

Please sign in to comment.