proxygen
SerialExecutorTest.cpp File Reference

Go to the source code of this file.

Functions

void SimpleTest (std::shared_ptr< folly::Executor > const &parent)
 
 TEST (SerialExecutor, Simple)
 
 TEST (SerialExecutor, SimpleInline)
 
 TEST (SerialExecutor, Afterlife)
 
void RecursiveAddTest (std::shared_ptr< folly::Executor > const &parent)
 
 TEST (SerialExecutor, RecursiveAdd)
 
 TEST (SerialExecutor, RecursiveAddInline)
 
 TEST (SerialExecutor, ExecutionThrows)
 

Function Documentation

void RecursiveAddTest ( std::shared_ptr< folly::Executor > const &  parent)

Definition at line 106 of file SerialExecutorTest.cpp.

References burnMs(), folly::pushmi::executor, EXPECT_EQ, folly::getKeepAliveToken(), i, folly::Baton< MayBlock, Atom >::post(), values(), and folly::Baton< MayBlock, Atom >::wait().

Referenced by TEST().

106  {
107  auto executor =
108  SerialExecutor::create(folly::getKeepAliveToken(parent.get()));
109 
110  folly::Baton<> finished_baton;
111 
112  std::vector<int> values;
113  std::vector<int> expected = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
114 
115  int i = 0;
116  std::function<void()> lambda = [&] {
117  if (i < 10) {
118  // make this extra vulnerable to concurrent execution
119  values.push_back(0);
120  burnMs(10);
121  values.back() = i;
122  executor->add(lambda);
123  } else if (i < 12) {
124  // Below we will post this lambda three times to the executor. When
125  // executed, the lambda will re-post itself during the first ten
126  // executions. Afterwards we do nothing twice (this else-branch), and
127  // then on the 13th execution signal that we are finished.
128  } else {
129  finished_baton.post();
130  }
131  ++i;
132  };
133 
134  executor->add(lambda);
135  executor->add(lambda);
136  executor->add(lambda);
137 
138  // wait until last task has executed
139  finished_baton.wait();
140 
141  EXPECT_EQ(expected, values);
142 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
void post() noexcept
Definition: Baton.h:123
static Func burnMs(uint64_t ms)
Executor::KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:200
folly::Function< void()> parent
Definition: AtFork.cpp:34
std::vector< int > values(1'000)
void SimpleTest ( std::shared_ptr< folly::Executor > const &  parent)

Definition at line 34 of file SerialExecutorTest.cpp.

References burnMs(), folly::pushmi::executor, EXPECT_EQ, folly::getKeepAliveToken(), i, folly::Baton< MayBlock, Atom >::post(), values(), and folly::Baton< MayBlock, Atom >::wait().

Referenced by TEST().

34  {
35  auto executor =
36  SerialExecutor::create(folly::getKeepAliveToken(parent.get()));
37 
38  std::vector<int> values;
39  std::vector<int> expected;
40 
41  for (int i = 0; i < 20; ++i) {
42  executor->add([i, &values] {
43  // make this extra vulnerable to concurrent execution
44  values.push_back(0);
45  burnMs(10);
46  values.back() = i;
47  });
48  expected.push_back(i);
49  }
50 
51  // wait until last task has executed
52  folly::Baton<> finished_baton;
53  executor->add([&finished_baton] { finished_baton.post(); });
54  finished_baton.wait();
55 
56  EXPECT_EQ(expected, values);
57 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
void post() noexcept
Definition: Baton.h:123
static Func burnMs(uint64_t ms)
Executor::KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:200
folly::Function< void()> parent
Definition: AtFork.cpp:34
std::vector< int > values(1'000)
TEST ( SerialExecutor  ,
Simple   
)

Definition at line 59 of file SerialExecutorTest.cpp.

References SimpleTest().

59  {
60  SimpleTest(std::make_shared<folly::CPUThreadPoolExecutor>(4));
61 }
void SimpleTest(std::shared_ptr< folly::Executor > const &parent)
TEST ( SerialExecutor  ,
SimpleInline   
)

Definition at line 62 of file SerialExecutorTest.cpp.

References SimpleTest().

62  {
63  SimpleTest(std::make_shared<folly::InlineExecutor>());
64 }
void SimpleTest(std::shared_ptr< folly::Executor > const &parent)
TEST ( SerialExecutor  ,
Afterlife   
)

Definition at line 69 of file SerialExecutorTest.cpp.

References burnMs(), folly::pushmi::executor, EXPECT_EQ, folly::getKeepAliveToken(), i, folly::Baton< MayBlock, Atom >::post(), folly::Baton< MayBlock, Atom >::reset(), values(), and folly::Baton< MayBlock, Atom >::wait().

69  {
70  auto cpu_executor = std::make_shared<folly::CPUThreadPoolExecutor>(4);
71  auto executor =
72  SerialExecutor::create(folly::getKeepAliveToken(cpu_executor.get()));
73 
74  // block executor until we call start_baton.post()
75  folly::Baton<> start_baton;
76  executor->add([&start_baton] { start_baton.wait(); });
77 
78  std::vector<int> values;
79  std::vector<int> expected;
80 
81  for (int i = 0; i < 20; ++i) {
82  executor->add([i, &values] {
83  // make this extra vulnerable to concurrent execution
84  values.push_back(0);
85  burnMs(10);
86  values.back() = i;
87  });
88  expected.push_back(i);
89  }
90 
91  folly::Baton<> finished_baton;
92  executor->add([&finished_baton] { finished_baton.post(); });
93 
94  // destroy SerialExecutor
95  executor.reset();
96 
97  // now kick off the tasks
98  start_baton.post();
99 
100  // wait until last task has executed
101  finished_baton.wait();
102 
103  EXPECT_EQ(expected, values);
104 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
void post() noexcept
Definition: Baton.h:123
static Func burnMs(uint64_t ms)
Executor::KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:200
void reset() noexcept
Definition: Baton.h:96
std::vector< int > values(1'000)
TEST ( SerialExecutor  ,
RecursiveAdd   
)

Definition at line 144 of file SerialExecutorTest.cpp.

References RecursiveAddTest().

144  {
145  RecursiveAddTest(std::make_shared<folly::CPUThreadPoolExecutor>(4));
146 }
void RecursiveAddTest(std::shared_ptr< folly::Executor > const &parent)
TEST ( SerialExecutor  ,
RecursiveAddInline   
)

Definition at line 147 of file SerialExecutorTest.cpp.

References RecursiveAddTest().

147  {
148  RecursiveAddTest(std::make_shared<folly::InlineExecutor>());
149 }
void RecursiveAddTest(std::shared_ptr< folly::Executor > const &parent)
TEST ( SerialExecutor  ,
ExecutionThrows   
)

Definition at line 151 of file SerialExecutorTest.cpp.

References folly::pushmi::executor.

151  {
152  auto executor = SerialExecutor::create();
153 
154  // an empty Func will throw std::bad_function_call when invoked,
155  // but SerialExecutor should catch that exception
156  executor->add(folly::Func{});
157 }
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor