TEST(AsyncQueue, TestConsumeOneRemovesOne) { AsyncQueue<int> q; q.push(1); q.push(2); ASSERT_EQ(2, q.size()); q.consumeOne( [](int i) {}); ASSERT_EQ(1, q.size()); }
TEST(AsyncQueue, TestEmptyQueueBlocksConsumeOneUntilPushed) { 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.consumeOne([&calls] (int i) { calls++; }); }); 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()); }