proxygen
ProducerConsumerQueueTest.cpp File Reference
#include <folly/ProducerConsumerQueue.h>
#include <atomic>
#include <chrono>
#include <memory>
#include <thread>
#include <vector>
#include <glog/logging.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 TEST (PCQ, QueueCorrectness)
 
 TEST (PCQ, PerfTest)
 
 TEST (PCQ, Destructor)
 
 TEST (PCQ, EmptyFull)
 
 TEST (PCQ, Capacity)
 

Function Documentation

TEST ( PCQ  ,
QueueCorrectness   
)

Definition at line 234 of file ProducerConsumerQueueTest.cpp.

234  {
235  correctnessTestType<std::string, true>("string (front+pop)");
236  correctnessTestType<std::string>("string");
237  correctnessTestType<int>("int");
238  correctnessTestType<unsigned long long>("unsigned long long");
239 }
TEST ( PCQ  ,
PerfTest   
)

Definition at line 241 of file ProducerConsumerQueueTest.cpp.

241  {
242  perfTestType<std::string, true>("string (front+pop)");
243  perfTestType<std::string>("string");
244  perfTestType<int>("int");
245  perfTestType<unsigned long long>("unsigned long long");
246 }
TEST ( PCQ  ,
Destructor   
)

Definition at line 248 of file ProducerConsumerQueueTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, i, folly::ProducerConsumerQueue< T >::read(), and folly::ProducerConsumerQueue< T >::write().

248  {
249  // Test that orphaned elements in a ProducerConsumerQueue are
250  // destroyed.
251  {
253  for (int i = 0; i < 10; ++i) {
254  EXPECT_TRUE(queue.write(DtorChecker()));
255  }
256 
257  EXPECT_EQ(DtorChecker::numInstances, 10);
258 
259  {
260  DtorChecker ignore;
261  EXPECT_TRUE(queue.read(ignore));
262  EXPECT_TRUE(queue.read(ignore));
263  }
264 
265  EXPECT_EQ(DtorChecker::numInstances, 8);
266  }
267 
268  EXPECT_EQ(DtorChecker::numInstances, 0);
269 
270  // Test the same thing in the case that the queue write pointer has
271  // wrapped, but the read one hasn't.
272  {
274  for (int i = 0; i < 3; ++i) {
275  EXPECT_TRUE(queue.write(DtorChecker()));
276  }
277  EXPECT_EQ(DtorChecker::numInstances, 3);
278  {
279  DtorChecker ignore;
280  EXPECT_TRUE(queue.read(ignore));
281  }
282  EXPECT_EQ(DtorChecker::numInstances, 2);
283  EXPECT_TRUE(queue.write(DtorChecker()));
284  EXPECT_EQ(DtorChecker::numInstances, 3);
285  }
286  EXPECT_EQ(DtorChecker::numInstances, 0);
287 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( PCQ  ,
EmptyFull   
)

Definition at line 289 of file ProducerConsumerQueueTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::ProducerConsumerQueue< T >::isEmpty(), folly::ProducerConsumerQueue< T >::isFull(), folly::ProducerConsumerQueue< T >::sizeGuess(), and folly::ProducerConsumerQueue< T >::write().

289  {
291  EXPECT_TRUE(queue.isEmpty());
292  EXPECT_FALSE(queue.isFull());
293 
294  EXPECT_TRUE(queue.write(1));
295  EXPECT_FALSE(queue.isEmpty());
296  EXPECT_FALSE(queue.isFull());
297 
298  EXPECT_TRUE(queue.write(2));
299  EXPECT_FALSE(queue.isEmpty());
300  EXPECT_TRUE(queue.isFull()); // Tricky: full after 2 writes, not 3.
301 
302  EXPECT_FALSE(queue.write(3));
303  EXPECT_EQ(queue.sizeGuess(), 2);
304 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( PCQ  ,
Capacity   
)

Definition at line 306 of file ProducerConsumerQueueTest.cpp.

References folly::ProducerConsumerQueue< T >::capacity(), and EXPECT_EQ.

306  {
308  EXPECT_EQ(queue.capacity(), 2); // PCQ max size is buffer size - 1.
309 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922