proxygen
TimesTest.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 (Times, success)
 
 TEST (Times, failure)
 
 TEST (Times, interrupt)
 

Function Documentation

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

Definition at line 53 of file TimesTest.cpp.

References i.

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 TimesTest.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 TimesTest.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 ( Times  ,
success   
)

Definition at line 61 of file TimesTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, f, makeThunk(), mutex, popAndFulfillPromise(), and folly::times().

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

Definition at line 87 of file TimesTest.cpp.

References eggs, EXPECT_FALSE, EXPECT_TRUE, f, makeThunk(), mutex, popAndFulfillPromise(), and folly::times().

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

Definition at line 115 of file TimesTest.cpp.

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

115  {
116  std::queue<std::shared_ptr<Promise<Unit>>> ps;
117  std::mutex ps_mutex;
118  int interrupt = 0;
119  bool complete = false;
120  bool failure = false;
121 
122  auto thunk = makeThunk(ps, interrupt, ps_mutex);
123  auto f = folly::times(3, thunk)
124  .thenValue([&](auto&&) mutable { complete = true; })
125  .onError([&](FutureException& /* e */) { failure = true; });
126 
127  EXPECT_EQ(0, interrupt);
128 
129  FutureException eggs("eggs");
130  f.raise(eggs);
131 
132  for (int i = 1; i <= 3; ++i) {
133  EXPECT_EQ(1, interrupt);
134  popAndFulfillPromise(ps, ps_mutex);
135  }
136 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static eggs_t eggs("eggs")
std::mutex mutex
std::function< Future< Unit >void)> makeThunk(std::queue< std::shared_ptr< Promise< Unit >>> &ps, int &interrupt, std::mutex &ps_mutex)
Definition: TimesTest.cpp:37
Future< Unit > times(const int n, F &&thunk)
Definition: Future-inl.h:2348
void popAndFulfillPromise(std::queue< std::shared_ptr< Promise< Unit >>> &ps, std::mutex &ps_mutex)
Definition: TimesTest.cpp:27