proxygen
CallOnceTest.cpp File Reference
#include <deque>
#include <mutex>
#include <thread>
#include <folly/portability/GTest.h>
#include <folly/synchronization/CallOnce.h>

Go to the source code of this file.

Functions

template<typename CallOnceFunc >
void bm_impl (CallOnceFunc &&fn, size_t iters)
 
 TEST (FollyCallOnce, Simple)
 
 TEST (FollyCallOnce, Exception)
 
 TEST (FollyCallOnce, Stress)
 

Variables

static size_t const kNumThreads = 16
 

Function Documentation

template<typename CallOnceFunc >
void bm_impl ( CallOnceFunc &&  fn,
size_t  iters 
)

Definition at line 27 of file CallOnceTest.cpp.

References i, kNumThreads, folly::pushmi::detail::t, and threads.

Referenced by TEST().

27  {
28  std::deque<std::thread> threads;
29  for (size_t i = 0u; i < kNumThreads; ++i) {
30  threads.emplace_back([&fn, iters] {
31  for (size_t j = 0u; j < iters; ++j) {
32  fn();
33  }
34  });
35  }
36  for (std::thread& t : threads) {
37  t.join();
38  }
39 }
static size_t const kNumThreads
std::vector< std::thread::id > threads
TEST ( FollyCallOnce  ,
Simple   
)

Definition at line 41 of file CallOnceTest.cpp.

References ASSERT_EQ, folly::call_once(), and flag.

41  {
43  auto fn = [&](int* outp) { ++*outp; };
44  int out = 0;
45  folly::call_once(flag, fn, &out);
46  folly::call_once(flag, fn, &out);
47  ASSERT_EQ(1, out);
48 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
static once_flag flag
Definition: Random.cpp:75
FOLLY_ALWAYS_INLINE void call_once(basic_once_flag< Mutex, Atom > &flag, F &&f, Args &&...args)
Definition: CallOnce.h:56
TEST ( FollyCallOnce  ,
Exception   
)

Definition at line 50 of file CallOnceTest.cpp.

References folly::call_once(), EXPECT_EQ, EXPECT_THROW, and flag.

50  {
51  struct ExpectedException {};
53  size_t numCalls = 0;
56  flag,
57  [&] {
58  ++numCalls;
59  throw ExpectedException();
60  }),
61  ExpectedException);
62  EXPECT_EQ(1, numCalls);
63  folly::call_once(flag, [&] { ++numCalls; });
64  EXPECT_EQ(2, numCalls);
65 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static once_flag flag
Definition: Random.cpp:75
FOLLY_ALWAYS_INLINE void call_once(basic_once_flag< Mutex, Atom > &flag, F &&f, Args &&...args)
Definition: CallOnce.h:56
TEST ( FollyCallOnce  ,
Stress   
)

Definition at line 67 of file CallOnceTest.cpp.

References ASSERT_EQ, bm_impl(), folly::call_once(), flag, and i.

67  {
68  for (int i = 0; i < 100; ++i) {
70  int out = 0;
71  bm_impl([&] { folly::call_once(flag, [&] { ++out; }); }, 100);
72  ASSERT_EQ(1, out);
73  }
74 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
static once_flag flag
Definition: Random.cpp:75
FOLLY_ALWAYS_INLINE void call_once(basic_once_flag< Mutex, Atom > &flag, F &&f, Args &&...args)
Definition: CallOnce.h:56
void bm_impl(CallOnceFunc &&fn, size_t iters)

Variable Documentation

size_t const kNumThreads = 16
static

Definition at line 24 of file CallOnceTest.cpp.

Referenced by BENCHMARK(), BENCHMARK_RELATIVE(), bm_impl(), TEST(), and TEST_F().