X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Ftest%2FProducerConsumerQueueTest.cpp;h=bf28dcb41c56115386d041f13e370581aa8a4d3c;hp=d322f0327f4902a15f490b32a09eb915d803c519;hb=c51f93205176b9bb3792a1c8bedcf5228ce669ac;hpb=27494a20393fa45072e7d526d358835f3abe312a diff --git a/folly/test/ProducerConsumerQueueTest.cpp b/folly/test/ProducerConsumerQueueTest.cpp index d322f032..bf28dcb4 100644 --- a/folly/test/ProducerConsumerQueueTest.cpp +++ b/folly/test/ProducerConsumerQueueTest.cpp @@ -34,11 +34,11 @@ template struct TestTraits { }; template<> struct TestTraits { - int limit() const { return 1 << 21; } + int limit() const { return 1 << 22; } std::string generate() const { return std::string(12, ' '); } }; -template +template struct PerfTest { typedef typename QueueType::value_type T; @@ -68,9 +68,17 @@ struct PerfTest { } void consumer() { - while (!done_) { - T data; - queue_.read(data); + /*static*/ if (Pop) { + while (!done_) { + if (queue_.frontPtr()) { + queue_.popFront(); + } + } + } else { + while (!done_) { + T data; + queue_.read(data); + } } } @@ -85,15 +93,16 @@ template void doTest(const char* name) { (*t)(); } -template void perfTestType(const char* type) { +template +void perfTestType(const char* type) { const size_t size = 0xfffe; LOG(INFO) << "Type: " << type; - doTest,size> >( + doTest,size,Pop> >( "ProducerConsumerQueue"); } -template +template struct CorrectnessTest { typedef typename QueueType::value_type T; @@ -125,7 +134,39 @@ struct CorrectnessTest { } void consumer() { - for (auto& expect : testData_) { + if (Pop) { + consumerPop(); + } else { + consumerRead(); + } + } + + void consumerPop() { + for (auto expect : testData_) { + again: + T* data; + if (!(data = queue_.frontPtr())) { + if (done_) { + // Try one more read; unless there's a bug in the queue class + // there should still be more data sitting in the queue even + // though the producer thread exited. + if (!(data = queue_.frontPtr())) { + EXPECT_TRUE(0 && "Finished too early ..."); + return; + } + } else { + goto again; + } + } else { + queue_.popFront(); + } + + EXPECT_EQ(*data, expect); + } + } + + void consumerRead() { + for (auto expect : testData_) { again: T data; if (!queue_.read(data)) { @@ -151,9 +192,10 @@ struct CorrectnessTest { std::atomic done_; }; -template void correctnessTestType(const std::string& type) { +template +void correctnessTestType(const std::string& type) { LOG(INFO) << "Type: " << type; - doTest,0xfffe> >( + doTest,0xfffe,Pop> >( "ProducerConsumerQueue"); } @@ -171,12 +213,14 @@ int DtorChecker::numInstances = 0; ////////////////////////////////////////////////////////////////////// TEST(PCQ, QueueCorrectness) { + correctnessTestType("string (front+pop)"); correctnessTestType("string"); correctnessTestType("int"); correctnessTestType("unsigned long long"); } TEST(PCQ, PerfTest) { + perfTestType("string (front+pop)"); perfTestType("string"); perfTestType("int"); perfTestType("unsigned long long");