proxygen
ExecutorTest.cpp File Reference

Go to the source code of this file.

Classes

class  CrappyExecutor
 
class  DoNothingExecutor
 

Functions

 TEST (ManualExecutor, runIsStable)
 
 TEST (ManualExecutor, drainIsNotStable)
 
 TEST (ManualExecutor, scheduleDur)
 
 TEST (ManualExecutor, laterThingsDontBlockEarlierOnes)
 
 TEST (ManualExecutor, orderWillNotBeQuestioned)
 
 TEST (ManualExecutor, evenWhenYouSkipAheadEventsRunInProperOrder)
 
 TEST (ManualExecutor, clockStartsAt0)
 
 TEST (ManualExecutor, scheduleAbs)
 
 TEST (ManualExecutor, advanceTo)
 
 TEST (ManualExecutor, advanceBack)
 
 TEST (ManualExecutor, advanceNeg)
 
 TEST (ManualExecutor, waitForDoesNotDeadlock)
 
 TEST (ManualExecutor, getViaDoesNotDeadlock)
 
 TEST (ManualExecutor, clear)
 
 TEST (ManualExecutor, drainsOnDestruction)
 
 TEST (Executor, InlineExecutor)
 
 TEST (Executor, QueuedImmediateExecutor)
 
 TEST (Executor, Runnable)
 
 TEST (Executor, ThrowableThen)
 
 TEST (Executor, CrappyExecutor)
 
 TEST (Executor, DoNothingExecutor)
 

Function Documentation

TEST ( ManualExecutor  ,
runIsStable   
)

Definition at line 29 of file ExecutorTest.cpp.

References folly::ManualExecutor::add(), folly::ManualExecutor::clear(), count, EXPECT_EQ, folly::ManualExecutor::run(), and x.

29  {
31  size_t count = 0;
32  auto f1 = [&]() { count++; };
33  auto f2 = [&]() {
34  x.add(f1);
35  x.add(f1);
36  };
37  x.add(f2);
38  x.run();
39  EXPECT_EQ(count, 0);
40 
41  // ManualExecutor's destructor drains, so explicitly clear the two added by
42  // f2.
43  EXPECT_EQ(2, x.clear());
44 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
int * count
void add(Func) override
TEST ( ManualExecutor  ,
drainIsNotStable   
)

Definition at line 46 of file ExecutorTest.cpp.

References folly::ManualExecutor::add(), count, folly::ManualExecutor::drain(), EXPECT_EQ, and x.

46  {
48  size_t count = 0;
49  auto f1 = [&]() { count++; };
50  auto f2 = [&]() {
51  x.add(f1);
52  x.add(f1);
53  };
54  x.add(f2);
55  x.drain();
56  EXPECT_EQ(count, 2);
57 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
int * count
void add(Func) override
TEST ( ManualExecutor  ,
scheduleDur   
)

Definition at line 59 of file ExecutorTest.cpp.

References folly::ManualExecutor::advance(), count, EXPECT_EQ, folly::ManualExecutor::run(), folly::ScheduledExecutor::schedule(), and x.

59  {
61  size_t count = 0;
62  std::chrono::milliseconds dur{10};
63  x.schedule([&] { count++; }, dur);
64  EXPECT_EQ(count, 0);
65  x.run();
66  EXPECT_EQ(count, 0);
67  x.advance(dur / 2);
68  EXPECT_EQ(count, 0);
69  x.advance(dur / 2);
70  EXPECT_EQ(count, 1);
71 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
void schedule(Func &&a)
Alias for add() (for Rx consistency)
void advance(Duration const &dur)
int * count
TEST ( ManualExecutor  ,
laterThingsDontBlockEarlierOnes   
)

Definition at line 73 of file ExecutorTest.cpp.

References folly::ManualExecutor::advance(), EXPECT_FALSE, EXPECT_TRUE, folly::gen::first, folly::ScheduledExecutor::schedule(), and x.

73  {
75  auto first = false;
76  std::chrono::milliseconds dur{10};
77  x.schedule([&] { first = true; }, dur);
78  x.schedule([] {}, 2 * dur);
80  x.advance(dur);
82 }
const int x
void schedule(Func &&a)
Alias for add() (for Rx consistency)
void advance(Duration const &dur)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
constexpr detail::First first
Definition: Base-inl.h:2553
TEST ( ManualExecutor  ,
orderWillNotBeQuestioned   
)

Definition at line 84 of file ExecutorTest.cpp.

References folly::ManualExecutor::advance(), EXPECT_FALSE, EXPECT_TRUE, folly::gen::first, folly::ScheduledExecutor::schedule(), and x.

84  {
86  auto first = false;
87  auto second = false;
88  std::chrono::milliseconds dur{10};
89  x.schedule([&] { first = true; }, dur);
90  x.schedule([&] { second = true; }, 2 * dur);
92  EXPECT_FALSE(second);
93  x.advance(dur);
95  EXPECT_FALSE(second);
96  x.advance(dur);
98  EXPECT_TRUE(second);
99 }
const int x
void schedule(Func &&a)
Alias for add() (for Rx consistency)
void advance(Duration const &dur)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
constexpr detail::First first
Definition: Base-inl.h:2553
TEST ( ManualExecutor  ,
evenWhenYouSkipAheadEventsRunInProperOrder   
)

Definition at line 101 of file ExecutorTest.cpp.

References folly::ManualExecutor::advance(), counter, EXPECT_EQ, folly::gen::first, folly::ScheduledExecutor::schedule(), and x.

101  {
103  auto counter = 0;
104  auto first = 0;
105  auto second = 0;
106  std::chrono::milliseconds dur{10};
107  x.schedule([&] { first = ++counter; }, dur);
108  x.schedule([&] { second = ++counter; }, 2 * dur);
109  EXPECT_EQ(first, 0);
110  EXPECT_EQ(second, 0);
111  x.advance(3 * dur);
112  EXPECT_EQ(first, 1);
113  EXPECT_EQ(second, 2);
114 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
void schedule(Func &&a)
Alias for add() (for Rx consistency)
void advance(Duration const &dur)
std::atomic< int > counter
constexpr detail::First first
Definition: Base-inl.h:2553
TEST ( ManualExecutor  ,
clockStartsAt0   
)

Definition at line 116 of file ExecutorTest.cpp.

References EXPECT_EQ, folly::ManualExecutor::now(), and x.

116  {
118  EXPECT_EQ(x.now(), x.now().min());
119 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
TimePoint now() override
Get this executor&#39;s notion of time. Must be threadsafe.
TEST ( ManualExecutor  ,
scheduleAbs   
)

Definition at line 121 of file ExecutorTest.cpp.

References folly::ManualExecutor::advance(), count, EXPECT_EQ, folly::ManualExecutor::now(), folly::ManualExecutor::scheduleAt(), and x.

121  {
123  size_t count = 0;
124  x.scheduleAt([&] { count++; }, x.now() + std::chrono::milliseconds(10));
125  EXPECT_EQ(count, 0);
126  x.advance(std::chrono::milliseconds(10));
127  EXPECT_EQ(count, 1);
128 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void scheduleAt(Func &&f, TimePoint const &t) override
const int x
void advance(Duration const &dur)
TimePoint now() override
Get this executor&#39;s notion of time. Must be threadsafe.
int * count
TEST ( ManualExecutor  ,
advanceTo   
)

Definition at line 130 of file ExecutorTest.cpp.

References folly::ManualExecutor::advanceTo(), count, EXPECT_EQ, now(), folly::ManualExecutor::scheduleAt(), and x.

130  {
132  size_t count = 0;
133  x.scheduleAt([&] { count++; }, std::chrono::steady_clock::now());
134  EXPECT_EQ(count, 0);
136  EXPECT_EQ(count, 1);
137 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void scheduleAt(Func &&f, TimePoint const &t) override
std::chrono::steady_clock::time_point now()
const int x
void advanceTo(TimePoint const &t)
int * count
TEST ( ManualExecutor  ,
advanceBack   
)

Definition at line 139 of file ExecutorTest.cpp.

References folly::ManualExecutor::advance(), folly::ManualExecutor::advanceTo(), count, EXPECT_EQ, folly::ManualExecutor::now(), folly::ScheduledExecutor::schedule(), and x.

139  {
141  size_t count = 0;
142  x.advance(std::chrono::microseconds(5));
143  x.schedule([&] { count++; }, std::chrono::microseconds(6));
144  EXPECT_EQ(count, 0);
145  x.advanceTo(x.now() - std::chrono::microseconds(1));
146  EXPECT_EQ(count, 0);
147 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
void schedule(Func &&a)
Alias for add() (for Rx consistency)
void advance(Duration const &dur)
void advanceTo(TimePoint const &t)
TimePoint now() override
Get this executor&#39;s notion of time. Must be threadsafe.
int * count
TEST ( ManualExecutor  ,
advanceNeg   
)

Definition at line 149 of file ExecutorTest.cpp.

References folly::ManualExecutor::advance(), count, EXPECT_EQ, folly::ScheduledExecutor::schedule(), and x.

149  {
151  size_t count = 0;
152  x.advance(std::chrono::microseconds(5));
153  x.schedule([&] { count++; }, std::chrono::microseconds(6));
154  EXPECT_EQ(count, 0);
155  x.advance(std::chrono::microseconds(-1));
156  EXPECT_EQ(count, 0);
157 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
void schedule(Func &&a)
Alias for add() (for Rx consistency)
void advance(Duration const &dur)
int * count
TEST ( ManualExecutor  ,
waitForDoesNotDeadlock   
)

Definition at line 159 of file ExecutorTest.cpp.

References f, folly::makeFuture(), folly::Baton< MayBlock, Atom >::post(), folly::ManualExecutor::run(), folly::pushmi::detail::t, folly::via(), folly::Baton< MayBlock, Atom >::wait(), and folly::ManualExecutor::waitFor().

159  {
160  ManualExecutor east, west;
161  folly::Baton<> baton;
162  auto f = makeFuture()
163  .via(&east)
164  .then([](Try<Unit>) { return makeFuture(); })
165  .via(&west);
166  std::thread t([&] {
167  baton.post();
168  west.waitFor(f);
169  });
170  baton.wait();
171  east.run();
172  t.join();
173 }
auto f
void waitFor(F const &f)
makeProgress until this Future is ready.
PUSHMI_INLINE_VAR constexpr detail::via_fn via
Definition: via.h:166
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
Definition: Try.h:51
void post() noexcept
Definition: Baton.h:123
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( ManualExecutor  ,
getViaDoesNotDeadlock   
)

Definition at line 175 of file ExecutorTest.cpp.

References f, folly::makeFuture(), folly::Baton< MayBlock, Atom >::post(), folly::ManualExecutor::run(), folly::pushmi::detail::t, folly::via(), and folly::Baton< MayBlock, Atom >::wait().

175  {
176  ManualExecutor east, west;
177  folly::Baton<> baton;
178  auto f = makeFuture()
179  .via(&east)
180  .then([](Try<Unit>) { return makeFuture(); })
181  .via(&west);
182  std::thread t([&] {
183  baton.post();
184  f.getVia(&west);
185  });
186  baton.wait();
187  east.run();
188  t.join();
189 }
auto f
PUSHMI_INLINE_VAR constexpr detail::via_fn via
Definition: via.h:166
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
Definition: Try.h:51
void post() noexcept
Definition: Baton.h:123
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( ManualExecutor  ,
clear   
)

Definition at line 191 of file ExecutorTest.cpp.

References folly::ManualExecutor::add(), folly::ManualExecutor::advance(), folly::ManualExecutor::clear(), count, EXPECT_EQ, folly::ManualExecutor::now(), folly::ManualExecutor::run(), folly::ManualExecutor::scheduleAt(), and x.

191  {
193  size_t count = 0;
194  x.add([&] { ++count; });
195  x.scheduleAt([&] { ++count; }, x.now() + std::chrono::milliseconds(10));
196  EXPECT_EQ(0, count);
197 
198  x.clear();
199  x.advance(std::chrono::milliseconds(10));
200  x.run();
201  EXPECT_EQ(0, count);
202 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void scheduleAt(Func &&f, TimePoint const &t) override
const int x
void advance(Duration const &dur)
TimePoint now() override
Get this executor&#39;s notion of time. Must be threadsafe.
int * count
void add(Func) override
TEST ( ManualExecutor  ,
drainsOnDestruction   
)

Definition at line 204 of file ExecutorTest.cpp.

References folly::ManualExecutor::add(), count, EXPECT_EQ, and x.

204  {
205  size_t count = 0;
206  {
208  x.add([&] { ++count; });
209  }
210  EXPECT_EQ(1, count);
211 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
int * count
void add(Func) override
TEST ( Executor  ,
InlineExecutor   
)

Definition at line 213 of file ExecutorTest.cpp.

References folly::InlineExecutor::add(), counter, EXPECT_EQ, and x.

213  {
215  size_t counter = 0;
216  x.add([&] {
217  x.add([&] {
218  EXPECT_EQ(counter, 0);
219  counter++;
220  });
221  EXPECT_EQ(counter, 1);
222  counter++;
223  });
224  EXPECT_EQ(counter, 2);
225 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
std::atomic< int > counter
void add(Func f) override
TEST ( Executor  ,
QueuedImmediateExecutor   
)

Definition at line 227 of file ExecutorTest.cpp.

References folly::QueuedImmediateExecutor::add(), counter, EXPECT_EQ, and x.

227  {
229  size_t counter = 0;
230  x.add([&] {
231  x.add([&] {
232  EXPECT_EQ(1, counter);
233  counter++;
234  });
235  EXPECT_EQ(0, counter);
236  counter++;
237  });
238  EXPECT_EQ(2, counter);
239 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
std::atomic< int > counter
TEST ( Executor  ,
Runnable   
)

Definition at line 241 of file ExecutorTest.cpp.

References folly::InlineExecutor::add(), counter, EXPECT_EQ, f, and x.

241  {
243  size_t counter = 0;
244  struct Runnable {
245  std::function<void()> fn;
246  void operator()() {
247  fn();
248  }
249  };
250  Runnable f;
251  f.fn = [&] { counter++; };
252  x.add(f);
253  EXPECT_EQ(counter, 1);
254 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
std::atomic< int > counter
void add(Func f) override
TEST ( Executor  ,
ThrowableThen   
)

Definition at line 256 of file ExecutorTest.cpp.

References EXPECT_THROW, f, and x.

256  {
258  auto f = Future<Unit>().thenValue(
259  [](auto&&) { throw std::runtime_error("Faildog"); });
260 
261  /*
262  auto f = Future<Unit>().via(&x).then([](){
263  throw std::runtime_error("Faildog");
264  });*/
265  EXPECT_THROW(f.value(), std::exception);
266 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
const int x
TEST ( Executor  ,
CrappyExecutor   
)

Definition at line 275 of file ExecutorTest.cpp.

References EXPECT_STREQ, EXPECT_TRUE, f, flag, folly::via(), and x.

275  {
277  bool flag = false;
278  auto f = folly::via(&x).onError([&](std::runtime_error& e) {
279  EXPECT_STREQ("bad", e.what());
280  flag = true;
281  });
282  EXPECT_TRUE(flag);
283 }
auto f
const int x
static once_flag flag
Definition: Random.cpp:75
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
auto via(Executor *x, Func &&func) -> Future< typename isFutureOrSemiFuture< decltype(std::declval< Func >()())>::Inner >
Definition: Future-inl.h:1290
TEST ( Executor  ,
DoNothingExecutor   
)

Definition at line 295 of file ExecutorTest.cpp.

References DoNothingExecutor::add(), EXPECT_FALSE, EXPECT_THROW, EXPECT_TRUE, f, folly::gen::move, folly::via(), and x.

295  {
297 
298  // Submit future callback to DoNothingExecutor
299  auto f = folly::via(&x).thenValue([](auto&&) { return 42; });
300 
301  // Callback function is stored in DoNothingExecutor, but not executed.
302  EXPECT_FALSE(f.isReady());
303 
304  // Destroy the function stored in DoNothingExecutor. The future callback
305  // will never get executed.
306  x.add({});
307 
308  EXPECT_TRUE(f.isReady());
310 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const int x
void add(Func f) override
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
auto via(Executor *x, Func &&func) -> Future< typename isFutureOrSemiFuture< decltype(std::declval< Func >()())>::Inner >
Definition: Future-inl.h:1290
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862