proxygen
EventCountTest.cpp File Reference
#include <folly/experimental/EventCount.h>
#include <algorithm>
#include <atomic>
#include <random>
#include <thread>
#include <vector>
#include <glog/logging.h>
#include <folly/Random.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

 TEST (EventCount, Simple)
 

Function Documentation

TEST ( EventCount  ,
Simple   
)

Definition at line 82 of file EventCountTest.cpp.

References count, EXPECT_EQ, ops, folly::randomNumberSeed(), and threads.

82  {
83  // We're basically testing for no deadlock.
84  static const size_t count = 300000;
85 
86  enum class Op {
87  UP,
88  DOWN,
89  };
90  std::vector<std::pair<Op, int>> ops;
91  std::mt19937 rnd(randomNumberSeed());
92  randomPartition(rnd, Op::UP, count, ops);
93  size_t uppers = ops.size();
94  randomPartition(rnd, Op::DOWN, count, ops);
95  size_t downers = ops.size() - uppers;
96  VLOG(1) << "Using " << ops.size() << " threads: uppers=" << uppers
97  << " downers=" << downers << " sem_count=" << count;
98 
99  std::shuffle(ops.begin(), ops.end(), std::mt19937(std::random_device()()));
100 
101  std::vector<std::thread> threads;
102  threads.reserve(ops.size());
103 
104  Semaphore sem;
105  for (auto& op : ops) {
106  int n = op.second;
107  if (op.first == Op::UP) {
108  auto fn = [&sem, n]() mutable {
109  while (n--) {
110  sem.up();
111  }
112  };
113  threads.push_back(std::thread(fn));
114  } else {
115  auto fn = [&sem, n]() mutable {
116  while (n--) {
117  sem.down();
118  }
119  };
120  threads.push_back(std::thread(fn));
121  }
122  }
123 
124  for (auto& thread : threads) {
125  thread.join();
126  }
127 
128  EXPECT_EQ(0, sem.value());
129 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::vector< std::thread::id > threads
const int ops
int * count
uint32_t randomNumberSeed()
Definition: Random.h:367