TEST(AsyncQueue, TestConsumeNRemovesCorrectNumberOfElements) { AsyncQueue<int> q; q.push(1); q.push(2); q.push(3); ASSERT_EQ(3, q.size()); q.consumeN(2, [](int){}); ASSERT_EQ(1, q.size()); q.consumeN(1, [](int){}); ASSERT_EQ(0, q.size()); }
TEST(AsyncQueue, TestConsumeNBlocksUntilAllElementsArePushed) { AsyncQueue<int> q; // Run a separate thread to count how many items are consumed from the queue. std::atomic<int> calls = 0; std::atomic<bool> threadDone = false; std::thread t1([&]() { q.consumeN(5, [&calls] (int i) { calls++; }); threadDone = true; }); q.push(1); q.push(2); q.push(3); std::this_thread::sleep_for(std::chrono::milliseconds(250)); ASSERT_EQ(3, calls); ASSERT_FALSE(threadDone); q.push(4); q.push(5); std::this_thread::sleep_for(std::chrono::milliseconds(250)); ASSERT_EQ(5, calls); ASSERT_TRUE(threadDone); t1.join(); }
TEST(AsyncQueue, TestConsumeNConsumesAfterEmptyQueue) { AsyncQueue<int> q; q.push(1); q.push(2); std::atomic<bool> threadDone = false; std::thread t1([&]() { q.consumeN(3, [](int){}); threadDone = true; }); std::this_thread::sleep_for(std::chrono::milliseconds(250)); ASSERT_EQ(0, q.size()); ASSERT_EQ(false, threadDone); q.push(1); std::this_thread::sleep_for(std::chrono::milliseconds(250)); ASSERT_EQ(0, q.size()); ASSERT_EQ(true, threadDone); t1.join(); }