proxygen
ExceptionCounterTest.cpp File Reference
#include <condition_variable>
#include <mutex>
#include <sstream>
#include <stdexcept>
#include <thread>
#include <folly/experimental/exception_tracer/ExceptionCounterLib.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

class  MyException
 

Functions

void bar ()
 
void foo ()
 
void baz ()
 
template<typename F >
void throwAndCatch (F f)
 
 TEST (ExceptionCounter, oneThread)
 
 TEST (ExceptionCounter, testClearExceptionStatistics)
 
 TEST (ExceptionCounter, testDifferentStacks)
 
 TEST (ExceptionCounter, multyThreads)
 

Function Documentation

void bar ( )

Definition at line 29 of file ExceptionCounterTest.cpp.

29  {
30  throw std::runtime_error("hello");
31 }
void baz ( )

Definition at line 37 of file ExceptionCounterTest.cpp.

References foo().

Referenced by TEST().

37  {
38  foo();
39 }
void foo()
void foo ( )

Definition at line 33 of file ExceptionCounterTest.cpp.

References MyException::MyException().

Referenced by baz().

33  {
34  throw MyException();
35 }
TEST ( ExceptionCounter  ,
oneThread   
)

Definition at line 53 of file ExceptionCounterTest.cpp.

References count, EXPECT_EQ, folly::exception_tracer::getExceptionStatistics(), i, deadlock::info(), and throwAndCatch().

53  {
55 
56  // Use volatile to prevent loop unrolling (it screws up stack frame grouping).
57  for (volatile int i = 0; i < 10; ++i) {
59  }
60 
61  auto stats = getExceptionStatistics();
62  EXPECT_EQ(stats.size(), 2);
63  EXPECT_EQ(stats[0].count, 10);
64  EXPECT_EQ(stats[1].count, 1);
65  EXPECT_EQ(*(stats[0].info.type), typeid(std::runtime_error));
66  EXPECT_EQ(*(stats[1].info.type), typeid(MyException));
67 }
def info()
Definition: deadlock.py:447
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
int * count
std::vector< ExceptionStats > getExceptionStatistics()
void throwAndCatch(F f)
TEST ( ExceptionCounter  ,
testClearExceptionStatistics   
)

Definition at line 69 of file ExceptionCounterTest.cpp.

References EXPECT_EQ, folly::exception_tracer::getExceptionStatistics(), and throwAndCatch().

69  {
71  auto stats = getExceptionStatistics();
72  EXPECT_EQ(stats.size(), 1);
73  stats = getExceptionStatistics();
74  EXPECT_EQ(stats.size(), 0);
75 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::vector< ExceptionStats > getExceptionStatistics()
void throwAndCatch(F f)
TEST ( ExceptionCounter  ,
testDifferentStacks   
)

Definition at line 77 of file ExceptionCounterTest.cpp.

References baz(), EXPECT_EQ, folly::exception_tracer::getExceptionStatistics(), and throwAndCatch().

77  {
80  auto stats = getExceptionStatistics();
81  EXPECT_EQ(stats.size(), 2);
82 }
void baz()
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::vector< ExceptionStats > getExceptionStatistics()
void throwAndCatch(F f)
TEST ( ExceptionCounter  ,
multyThreads   
)

Definition at line 84 of file ExceptionCounterTest.cpp.

References count, EXPECT_EQ, folly::exception_tracer::getExceptionStatistics(), i, kNumThreads, folly::detail::lock(), mutex, folly::pushmi::detail::t, threads, and throwAndCatch().

84  {
85  constexpr size_t kNumIterations = 10000;
86  constexpr size_t kNumThreads = 10;
87  std::vector<std::thread> threads;
88  threads.resize(kNumThreads);
89 
90  std::mutex preparedMutex;
91  std::mutex finishedMutex;
92  std::condition_variable preparedBarrier;
93  std::condition_variable finishedBarrier;
94  int preparedThreads = 0;
95  bool finished = false;
96 
97  for (auto& t : threads) {
98  t = std::thread([&]() {
99  for (size_t i = 0; i < kNumIterations; ++i) {
101  }
102 
103  {
104  std::unique_lock<std::mutex> lock(preparedMutex);
105  ++preparedThreads;
106  preparedBarrier.notify_one();
107  }
108 
109  std::unique_lock<std::mutex> lock(finishedMutex);
110  finishedBarrier.wait(lock, [&]() { return finished; });
111  });
112  }
113 
114  {
115  std::unique_lock<std::mutex> lock(preparedMutex);
116  preparedBarrier.wait(
117  lock, [&]() { return preparedThreads == kNumThreads; });
118  }
119 
120  auto stats = getExceptionStatistics();
121  EXPECT_EQ(stats.size(), 1);
122  EXPECT_EQ(stats[0].count, kNumIterations * kNumThreads);
123 
124  {
125  std::unique_lock<std::mutex> lock(finishedMutex);
126  finished = true;
127  finishedBarrier.notify_all();
128  }
129 
130  for (auto& t : threads) {
131  t.join();
132  }
133 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static size_t const kNumThreads
std::vector< std::thread::id > threads
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
int * count
std::vector< ExceptionStats > getExceptionStatistics()
std::mutex mutex
void throwAndCatch(F f)
template<typename F >
void throwAndCatch ( f)

Definition at line 45 of file ExceptionCounterTest.cpp.

References f.

Referenced by TEST().

45  {
46  try {
47  f();
48  } catch (...) {
49  // ignore
50  }
51 }
auto f