TEST(LinkedBlockingQueue, MixedTest) { BlockingQueue *queue = new LinkedBlockingQueue(5); SuperObject *dummy1 = new DummyObject(1); ASSERT_TRUE(queue->offer(dummy1)); ASSERT_TRUE(queue->offer(new DummyObject(2))); ASSERT_TRUE(queue->offer(new DummyObject(3))); ASSERT_TRUE(queue->offer(new DummyObject(4))); ASSERT_TRUE(queue->add(new DummyObject(5))); ASSERT_EQ(5, queue->size()); SuperObject *tmp1 = queue->take(); ASSERT_EQ(dummy1, tmp1); ASSERT_EQ(4, queue->size()); delete tmp1; SuperObject *tmp2 = queue->peek(); ASSERT_EQ(4, queue->size()); ASSERT_TRUE(tmp2 != NULL); delete queue->take(); ASSERT_EQ(3, queue->size()); delete queue->take(); ASSERT_EQ(2, queue->size()); delete queue->take(); ASSERT_EQ(1, queue->size()); SuperObject *tmp3 = queue->poll(3); ASSERT_TRUE(tmp3 != NULL); if (tmp3 != NULL) { delete tmp3; } ASSERT_EQ(0, queue->size()); ASSERT_EQ(NULL, queue->poll(3)); ASSERT_EQ(NULL, queue->peek()); delete queue; }
void consumer_poll(int id, BlockingQueue & queue) { int i = 0; while (i < 50) { int data; if (queue.poll(data, chrono::milliseconds(250))) { cout << "Consumer " << id << " poll " << data << endl; i++; continue; } cout << "Consumer " << id << " retry" << endl; } }