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;
	}
	TEST(LinkedBlockingQueue, OrderingTest) {
		BlockingQueue *queue = new LinkedBlockingQueue();
		ASSERT_TRUE(queue->offer(new DummyObject(1)));
		ASSERT_TRUE(queue->offer(new DummyObject(2)));
		ASSERT_TRUE(queue->offer(new DummyObject(3)));
		DummyObject *ret1 = dynamic_cast<DummyObject *>(queue->take());
		ASSERT_TRUE(ret1 != NULL);
		ASSERT_EQ(1, ret1->getData());
		delete ret1;
		DummyObject *ret2 = dynamic_cast<DummyObject *>(queue->take());
		ASSERT_TRUE(ret2 != NULL);
		ASSERT_EQ(2, ret2->getData());
		delete ret2;

		ASSERT_TRUE(queue->offer(new DummyObject(4)));

		DummyObject *ret3 = dynamic_cast<DummyObject *>(queue->take());
		ASSERT_TRUE(ret3 != NULL);
		ASSERT_EQ(3, ret3->getData());
		delete ret3;

		DummyObject *ret4 = dynamic_cast<DummyObject *>(queue->take());
		ASSERT_TRUE(ret4 != NULL);
		ASSERT_EQ(4, ret4->getData());
		delete ret4;
		ASSERT_EQ(0, queue->size());
		delete queue;
	}
	TEST(LinkedBlockingQueue, OfferTest) {

		BlockingQueue *queue = new LinkedBlockingQueue(5);
		ASSERT_TRUE(queue->offer(new DummyObject(1)));
		ASSERT_TRUE(queue->offer(new DummyObject(2)));
		ASSERT_TRUE(queue->offer(new DummyObject(3)));
		ASSERT_TRUE(queue->offer(new DummyObject(4)));
		ASSERT_TRUE(queue->offer(new DummyObject(5)));
		SuperObject *dummy1 = new DummyObject(6);
		ASSERT_FALSE(queue->offer(dummy1, 3));
		ASSERT_FALSE(queue->offer(dummy1));
		delete queue;
		delete dummy1;
	}
示例#4
0
void producer_offer(int id, BlockingQueue & queue) {
    int i = 0;
    while (i < 75) {
        int data = i;
        if (queue.offer(data, chrono::milliseconds(100))) {
            cout << "Producer " << id << " offer " << data << endl;
            i++;
            continue;
        }
        cout << "Producer " << id << " retry" << endl;
    }
}
	TEST(LinkedBlockingQueue, PeekAndRemoveTest) {

		BlockingQueue *queue = new LinkedBlockingQueue();
		SuperObject *dummy1 = new DummyObject(1);
		ASSERT_TRUE(queue->offer(dummy1));
		ASSERT_EQ(dummy1, queue->peek());

		ASSERT_TRUE(queue->contains(dummy1));
		ASSERT_TRUE(queue->remove(dummy1));
		ASSERT_FALSE(queue->remove(dummy1));
		ASSERT_FALSE(queue->contains(dummy1));
		ASSERT_EQ(0, queue->size());

		delete queue;
		delete dummy1;
	}