proxygen
WhileDoTest.cpp File Reference
#include <memory>
#include <mutex>
#include <queue>
#include <folly/futures/Future.h>
#include <folly/futures/Promise.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Functions

void popAndFulfillPromise (std::queue< std::shared_ptr< Promise< Unit >>> &ps, std::mutex &ps_mutex)
 
std::function< Future< Unit >void)> makeThunk (std::queue< std::shared_ptr< Promise< Unit >>> &ps, int &interrupt, std::mutex &ps_mutex)
 
std::function< bool(void)> makePred (int &i)
 
 TEST (WhileDo, success)
 
 TEST (WhileDo, failure)
 
 TEST (WhileDo, interrupt)
 

Function Documentation

std::function<bool(void)> makePred ( int &  i)
inline

Definition at line 53 of file WhileDoTest.cpp.

References i.

Referenced by TEST().

53  {
54  return [&]() {
55  bool res = i < 3;
56  ++i;
57  return res;
58  };
59 }
std::function<Future<Unit>void)> makeThunk ( std::queue< std::shared_ptr< Promise< Unit >>> &  ps,
int &  interrupt,
std::mutex ps_mutex 
)
inline

Definition at line 37 of file WhileDoTest.cpp.

Referenced by TEST().

40  {
41  return [&]() mutable {
42  auto p = std::make_shared<Promise<Unit>>();
43  p->setInterruptHandler(
44  [&](exception_wrapper const& /* e */) { ++interrupt; });
45  ps_mutex.lock();
46  ps.push(p);
47  ps_mutex.unlock();
48 
49  return p->getFuture();
50  };
51 }
void popAndFulfillPromise ( std::queue< std::shared_ptr< Promise< Unit >>> &  ps,
std::mutex ps_mutex 
)
inline

Definition at line 27 of file WhileDoTest.cpp.

Referenced by TEST().

29  {
30  ps_mutex.lock();
31  auto p = ps.front();
32  ps.pop();
33  ps_mutex.unlock();
34  p->setValue();
35 }
TEST ( WhileDo  ,
success   
)

Definition at line 61 of file WhileDoTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, f, i, makePred(), makeThunk(), mutex, popAndFulfillPromise(), and folly::whileDo().

61  {
62  std::queue<std::shared_ptr<Promise<Unit>>> ps;
63  std::mutex ps_mutex;
64  int i = 0;
65  int interrupt = 0;
66  bool complete = false;
67  bool failure = false;
68 
69  auto pred = makePred(i);
70  auto thunk = makeThunk(ps, interrupt, ps_mutex);
71  auto f = folly::whileDo(pred, thunk)
72  .thenValue([&](auto&&) mutable { complete = true; })
73  .onError([&](FutureException& /* e */) { failure = true; });
74 
75  popAndFulfillPromise(ps, ps_mutex);
76  EXPECT_FALSE(complete);
77  EXPECT_FALSE(failure);
78 
79  popAndFulfillPromise(ps, ps_mutex);
80  EXPECT_FALSE(complete);
81  EXPECT_FALSE(failure);
82 
83  popAndFulfillPromise(ps, ps_mutex);
84  EXPECT_TRUE(f.isReady());
85  EXPECT_TRUE(complete);
86  EXPECT_FALSE(failure);
87 }
auto f
Future< Unit > whileDo(P &&predicate, F &&thunk)
Definition: Future-inl.h:2335
std::function< Future< Unit >void)> makeThunk(std::queue< std::shared_ptr< Promise< Unit >>> &ps, int &interrupt, std::mutex &ps_mutex)
Definition: WhileDoTest.cpp:37
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::mutex mutex
void popAndFulfillPromise(std::queue< std::shared_ptr< Promise< Unit >>> &ps, std::mutex &ps_mutex)
Definition: WhileDoTest.cpp:27
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
std::function< bool(void)> makePred(int &i)
Definition: WhileDoTest.cpp:53
TEST ( WhileDo  ,
failure   
)

Definition at line 89 of file WhileDoTest.cpp.

References eggs, EXPECT_FALSE, EXPECT_TRUE, f, i, makePred(), makeThunk(), mutex, popAndFulfillPromise(), and folly::whileDo().

89  {
90  std::queue<std::shared_ptr<Promise<Unit>>> ps;
91  std::mutex ps_mutex;
92  int i = 0;
93  int interrupt = 0;
94  bool complete = false;
95  bool failure = false;
96 
97  auto pred = makePred(i);
98  auto thunk = makeThunk(ps, interrupt, ps_mutex);
99  auto f = folly::whileDo(pred, thunk)
100  .thenValue([&](auto&&) mutable { complete = true; })
101  .onError([&](FutureException& /* e */) { failure = true; });
102 
103  popAndFulfillPromise(ps, ps_mutex);
104  EXPECT_FALSE(complete);
105  EXPECT_FALSE(failure);
106 
107  ps_mutex.lock();
108  auto p2 = ps.front();
109  ps.pop();
110  ps_mutex.unlock();
111  FutureException eggs("eggs");
112  p2->setException(eggs);
113 
114  EXPECT_TRUE(f.isReady());
115  EXPECT_FALSE(complete);
116  EXPECT_TRUE(failure);
117 }
auto f
Future< Unit > whileDo(P &&predicate, F &&thunk)
Definition: Future-inl.h:2335
std::function< Future< Unit >void)> makeThunk(std::queue< std::shared_ptr< Promise< Unit >>> &ps, int &interrupt, std::mutex &ps_mutex)
Definition: WhileDoTest.cpp:37
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static eggs_t eggs("eggs")
std::mutex mutex
void popAndFulfillPromise(std::queue< std::shared_ptr< Promise< Unit >>> &ps, std::mutex &ps_mutex)
Definition: WhileDoTest.cpp:27
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
std::function< bool(void)> makePred(int &i)
Definition: WhileDoTest.cpp:53
TEST ( WhileDo  ,
interrupt   
)

Definition at line 119 of file WhileDoTest.cpp.

References eggs, EXPECT_EQ, f, i, makePred(), makeThunk(), mutex, popAndFulfillPromise(), and folly::whileDo().

119  {
120  std::queue<std::shared_ptr<Promise<Unit>>> ps;
121  std::mutex ps_mutex;
122  int interrupt = 0;
123  bool complete = false;
124  bool failure = false;
125 
126  int i = 0;
127  auto pred = makePred(i);
128  auto thunk = makeThunk(ps, interrupt, ps_mutex);
129  auto f = folly::whileDo(pred, thunk)
130  .thenValue([&](auto&&) mutable { complete = true; })
131  .onError([&](FutureException& /* e */) { failure = true; });
132 
133  EXPECT_EQ(0, interrupt);
134 
135  FutureException eggs("eggs");
136  f.raise(eggs);
137 
138  for (int j = 1; j <= 3; ++j) {
139  EXPECT_EQ(1, interrupt);
140  popAndFulfillPromise(ps, ps_mutex);
141  }
142 }
auto f
Future< Unit > whileDo(P &&predicate, F &&thunk)
Definition: Future-inl.h:2335
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::function< Future< Unit >void)> makeThunk(std::queue< std::shared_ptr< Promise< Unit >>> &ps, int &interrupt, std::mutex &ps_mutex)
Definition: WhileDoTest.cpp:37
static eggs_t eggs("eggs")
std::mutex mutex
void popAndFulfillPromise(std::queue< std::shared_ptr< Promise< Unit >>> &ps, std::mutex &ps_mutex)
Definition: WhileDoTest.cpp:27
std::function< bool(void)> makePred(int &i)
Definition: WhileDoTest.cpp:53