TEST(AsyncQueue, TestConsumeAllEmptiesQueue) { AsyncQueue<int> q; q.push(1); q.push(2); ASSERT_EQ(2, q.size()); q.consumeAll( [](int i) {}); EXPECT_EQ(0, q.size()); }
TEST(AsyncQueue, TestEmptyQueueBlocksConsumeAllUntilPushed) { AsyncQueue<int> q; // run a separate thread to count how many items are consumed from the queue. std::atomic<int> calls = 0; std::thread t1([&] () { q.consumeAll([&calls] (int i) { calls++; }); }); // wait for 250 ms. Should be enougth to ensure no items have been consumed by the other thread std::this_thread::sleep_for(std::chrono::milliseconds(250)); ASSERT_EQ(0, calls); // push something onto the queue and wait for consumer t1 to complete. q.push(1); t1.join(); ASSERT_EQ(1, calls); ASSERT_EQ(0, q.size()); }