proxygen
PromiseTest.cpp File Reference
#include <folly/futures/Future.h>
#include <folly/portability/GTest.h>
#include <memory>

Go to the source code of this file.

Macros

#define DOIT(CREATION_EXPR)
 
#define DOIT(CREATION_EXPR)
 
#define DOIT(STMT)
 
#define DOIT(STMT)
 
#define DOIT(STMT)
 
#define DOIT(CTOR, STMT)
 

Typedefs

typedef FutureException eggs_t
 

Functions

 TEST (Promise, makeEmpty)
 
 TEST (Promise, special)
 
 TEST (Promise, getSemiFuture)
 
 TEST (Promise, getFuture)
 
 TEST (Promise, setValueUnit)
 
 TEST (Promise, ctorPostconditionValid)
 
 TEST (Promise, ctorPostconditionInvalid)
 
 TEST (Promise, lacksPreconditionValid)
 
 TEST (Promise, hasPreconditionValid)
 
 TEST (Promise, hasPostconditionValid)
 
 TEST (Promise, hasPostconditionInvalid)
 
 TEST (Promise, setValueSemiFuture)
 
 TEST (Promise, setValue)
 
 TEST (Promise, setException)
 
 TEST (Promise, setWith)
 
 TEST (Promise, isFulfilled)
 
 TEST (Promise, isFulfilledWithFuture)
 
 TEST (Promise, brokenOnDelete)
 
 TEST (Promise, brokenPromiseHasTypeInfo)
 

Variables

static eggs_t eggs ("eggs")
 

Macro Definition Documentation

#define DOIT (   CREATION_EXPR)
Value:
do { \
auto p1 = (CREATION_EXPR); \
EXPECT_TRUE(p1.valid()); \
auto p2 = std::move(p1); \
EXPECT_FALSE(p1.valid()); \
EXPECT_TRUE(p2.valid()); \
} while (false)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862

Referenced by TEST().

#define DOIT (   CREATION_EXPR)
Value:
do { \
auto p1 = (CREATION_EXPR); \
EXPECT_FALSE(p1.valid()); \
auto p2 = std::move(p1); \
EXPECT_FALSE(p1.valid()); \
EXPECT_FALSE(p2.valid()); \
} while (false)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
#define DOIT (   STMT)
Value:
do { \
auto p = makeValid(); \
{ STMT; } \
} while (false)
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
#define DOIT (   STMT)
Value:
do { \
auto p = makeValid(); \
} while (false)
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
#define DOIT (   STMT)
Value:
do { \
auto p = makeValid(); \
EXPECT_TRUE(p.valid()); \
} while (false)
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define DOIT (   CTOR,
  STMT 
)
Value:
do { \
auto p = (CTOR); \
EXPECT_FALSE(p.valid()); \
} while (false)
#define EXPECT_NO_THROW(statement)
Definition: gtest.h:1845
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862

Typedef Documentation

Definition at line 26 of file PromiseTest.cpp.

Function Documentation

TEST ( Promise  ,
makeEmpty   
)

Definition at line 29 of file PromiseTest.cpp.

References EXPECT_TRUE, and folly::Promise< T >::makeEmpty().

29  {
30  auto p = Promise<int>::makeEmpty();
31  EXPECT_TRUE(p.isFulfilled());
32 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( Promise  ,
special   
)

Definition at line 34 of file PromiseTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, and folly::value().

34  {
35  EXPECT_FALSE(std::is_copy_constructible<Promise<int>>::value);
36  EXPECT_FALSE(std::is_copy_assignable<Promise<int>>::value);
37  EXPECT_TRUE(std::is_move_constructible<Promise<int>>::value);
38  EXPECT_TRUE(std::is_move_assignable<Promise<int>>::value);
39 }
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Promise  ,
getSemiFuture   
)

Definition at line 41 of file PromiseTest.cpp.

References EXPECT_FALSE, f, folly::Promise< T >::getSemiFuture(), and folly::futures::detail::FutureBase< T >::isReady().

41  {
42  Promise<int> p;
44  EXPECT_FALSE(f.isReady());
45 }
auto f
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Promise  ,
getFuture   
)

Definition at line 47 of file PromiseTest.cpp.

References EXPECT_FALSE, f, folly::Promise< T >::getFuture(), and folly::futures::detail::FutureBase< T >::isReady().

47  {
48  Promise<int> p;
49  Future<int> f = p.getFuture();
50  EXPECT_FALSE(f.isReady());
51 }
auto f
Future< T > getFuture()
Definition: Promise-inl.h:97
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Promise  ,
setValueUnit   
)

Definition at line 53 of file PromiseTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, folly::Promise< T >::makeEmpty(), and folly::Promise< T >::setValue().

53  {
54  Promise<Unit> p;
55  p.setValue();
56 }
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
TEST ( Promise  ,
ctorPostconditionValid   
)

Definition at line 71 of file PromiseTest.cpp.

References DOIT.

71  {
72  // Ctors/factories that promise valid -- postcondition: valid()
73 
74 #define DOIT(CREATION_EXPR) \
75  do { \
76  auto p1 = (CREATION_EXPR); \
77  EXPECT_TRUE(p1.valid()); \
78  auto p2 = std::move(p1); \
79  EXPECT_FALSE(p1.valid()); \
80  EXPECT_TRUE(p2.valid()); \
81  } while (false)
82 
83  DOIT(makeValid());
84  DOIT(Promise<int>());
85  DOIT(Promise<int>{});
88 
89 #undef DOIT
90 }
#define DOIT(CREATION_EXPR)
TEST ( Promise  ,
ctorPostconditionInvalid   
)

Definition at line 92 of file PromiseTest.cpp.

References DOIT.

92  {
93  // Ctors/factories that promise invalid -- postcondition: !valid()
94 
95 #define DOIT(CREATION_EXPR) \
96  do { \
97  auto p1 = (CREATION_EXPR); \
98  EXPECT_FALSE(p1.valid()); \
99  auto p2 = std::move(p1); \
100  EXPECT_FALSE(p1.valid()); \
101  EXPECT_FALSE(p2.valid()); \
102  } while (false)
103 
104  DOIT(makeInvalid());
106 
107 #undef DOIT
108 }
#define DOIT(CREATION_EXPR)
TEST ( Promise  ,
lacksPreconditionValid   
)

Definition at line 110 of file PromiseTest.cpp.

References folly::copy(), DOIT, and folly::gen::move.

110  {
111  // Ops that don't throw PromiseInvalid if !valid() --
112  // without precondition: valid()
113 
114 #define DOIT(STMT) \
115  do { \
116  auto p = makeValid(); \
117  { STMT; } \
118  copy(std::move(p)); \
119  EXPECT_NO_THROW(STMT); \
120  } while (false)
121 
122  // misc methods that don't require isValid()
123  DOIT(p.valid());
124  DOIT(p.isFulfilled());
125 
126  // move-ctor - move-copy to local, copy(), pass-by-move-value
127  DOIT(auto other = std::move(p));
128  DOIT(copy(std::move(p)));
129  DOIT(([](auto) {})(std::move(p)));
130 
131  // move-assignment into either {valid | invalid}
132  DOIT({
133  auto other = makeValid();
134  other = std::move(p);
135  });
136  DOIT({
137  auto other = makeInvalid();
138  other = std::move(p);
139  });
140 
141 #undef DOIT
142 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
#define DOIT(CREATION_EXPR)
TEST ( Promise  ,
hasPreconditionValid   
)

Definition at line 144 of file PromiseTest.cpp.

References DOIT.

144  {
145  // Ops that require validity; precondition: valid();
146  // throw PromiseInvalid if !valid()
147 
148 #define DOIT(STMT) \
149  do { \
150  auto p = makeValid(); \
151  EXPECT_NO_THROW(STMT); \
152  copy(std::move(p)); \
153  EXPECT_THROW(STMT, PromiseInvalid); \
154  } while (false)
155 
156  auto const except = std::logic_error("foo");
157  auto const ewrap = folly::exception_wrapper(except);
158 
159  DOIT(p.getSemiFuture());
160  DOIT(p.getFuture());
161  DOIT(p.setException(except));
162  DOIT(p.setException(ewrap));
163  DOIT(p.setInterruptHandler([](auto&) {}));
164  DOIT(p.setValue(42));
165  DOIT(p.setTry(Try<int>(42)));
166  DOIT(p.setTry(Try<int>(ewrap)));
167  DOIT(p.setWith([] { return 42; }));
168 
169 #undef DOIT
170 }
Definition: Try.h:51
#define DOIT(CREATION_EXPR)
TEST ( Promise  ,
hasPostconditionValid   
)

Definition at line 172 of file PromiseTest.cpp.

References DOIT.

172  {
173  // Ops that preserve validity -- postcondition: valid()
174 
175 #define DOIT(STMT) \
176  do { \
177  auto p = makeValid(); \
178  EXPECT_NO_THROW(STMT); \
179  EXPECT_TRUE(p.valid()); \
180  } while (false)
181 
182  auto const swallow = [](auto) {};
183 
184  DOIT(swallow(p.valid())); // p.valid() itself preserves validity
185  DOIT(swallow(p.isFulfilled()));
186 
187 #undef DOIT
188 }
#define DOIT(CREATION_EXPR)
TEST ( Promise  ,
hasPostconditionInvalid   
)

Definition at line 190 of file PromiseTest.cpp.

References DOIT, and folly::gen::move.

190  {
191  // Ops that consume *this -- postcondition: !valid()
192 
193 #define DOIT(CTOR, STMT) \
194  do { \
195  auto p = (CTOR); \
196  EXPECT_NO_THROW(STMT); \
197  EXPECT_FALSE(p.valid()); \
198  } while (false)
199 
200  // move-ctor of {valid|invalid}
201  DOIT(makeValid(), { auto other{std::move(p)}; });
202  DOIT(makeInvalid(), { auto other{std::move(p)}; });
203 
204  // move-assignment of {valid|invalid} into {valid|invalid}
205  DOIT(makeValid(), {
206  auto other = makeValid();
207  other = std::move(p);
208  });
209  DOIT(makeValid(), {
210  auto other = makeInvalid();
211  other = std::move(p);
212  });
213  DOIT(makeInvalid(), {
214  auto other = makeValid();
215  other = std::move(p);
216  });
217  DOIT(makeInvalid(), {
218  auto other = makeInvalid();
219  other = std::move(p);
220  });
221 
222  // pass-by-value of {valid|invalid}
223  DOIT(makeValid(), {
224  auto const byval = [](auto) {};
225  byval(std::move(p));
226  });
227  DOIT(makeInvalid(), {
228  auto const byval = [](auto) {};
229  byval(std::move(p));
230  });
231 
232 #undef DOIT
233 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define DOIT(CREATION_EXPR)
TEST ( Promise  ,
setValueSemiFuture   
)

Definition at line 235 of file PromiseTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, f, folly::Promise< T >::getSemiFuture(), folly::gen::move, name, ptr, folly::Promise< T >::setValue(), v, and folly::value().

235  {
236  Promise<int> fund;
237  auto ffund = fund.getSemiFuture();
238  fund.setValue(42);
239  EXPECT_EQ(42, ffund.value());
240 
241  struct Foo {
242  string name;
243  int value;
244  };
245 
246  Promise<Foo> pod;
247  auto fpod = pod.getSemiFuture();
248  Foo f = {"the answer", 42};
249  pod.setValue(f);
250  Foo f2 = fpod.value();
251  EXPECT_EQ(f.name, f2.name);
252  EXPECT_EQ(f.value, f2.value);
253 
254  pod = Promise<Foo>();
255  fpod = pod.getSemiFuture();
256  pod.setValue(std::move(f2));
257  Foo f3 = fpod.value();
258  EXPECT_EQ(f.name, f3.name);
259  EXPECT_EQ(f.value, f3.value);
260 
262  auto fmov = mov.getSemiFuture();
263  mov.setValue(std::make_unique<int>(42));
264  unique_ptr<int> ptr = std::move(fmov.value());
265  EXPECT_EQ(42, *ptr);
266 
268  auto fv = v.getSemiFuture();
269  v.setValue();
270  EXPECT_TRUE(fv.isReady());
271 }
void * ptr
auto f
auto v
Foo(std::atomic< int > &d)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const char * name
Definition: http_parser.c:437
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
TEST ( Promise  ,
setValue   
)

Definition at line 273 of file PromiseTest.cpp.

References EXPECT_EQ, EXPECT_TRUE, f, folly::Promise< T >::getFuture(), folly::gen::move, name, ptr, folly::Promise< T >::setValue(), v, and folly::value().

273  {
274  Promise<int> fund;
275  auto ffund = fund.getFuture();
276  fund.setValue(42);
277  EXPECT_EQ(42, ffund.value());
278 
279  struct Foo {
280  string name;
281  int value;
282  };
283 
284  Promise<Foo> pod;
285  auto fpod = pod.getFuture();
286  Foo f = {"the answer", 42};
287  pod.setValue(f);
288  Foo f2 = fpod.value();
289  EXPECT_EQ(f.name, f2.name);
290  EXPECT_EQ(f.value, f2.value);
291 
292  pod = Promise<Foo>();
293  fpod = pod.getFuture();
294  pod.setValue(std::move(f2));
295  Foo f3 = fpod.value();
296  EXPECT_EQ(f.name, f3.name);
297  EXPECT_EQ(f.value, f3.value);
298 
300  auto fmov = mov.getFuture();
301  mov.setValue(std::make_unique<int>(42));
302  unique_ptr<int> ptr = std::move(fmov.value());
303  EXPECT_EQ(42, *ptr);
304 
306  auto fv = v.getFuture();
307  v.setValue();
308  EXPECT_TRUE(fv.isReady());
309 }
void * ptr
auto f
auto v
Foo(std::atomic< int > &d)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const char * name
Definition: http_parser.c:437
Future< T > getFuture()
Definition: Promise-inl.h:97
static const char *const value
Definition: Conv.cpp:50
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
TEST ( Promise  ,
setException   
)

Definition at line 311 of file PromiseTest.cpp.

References eggs, EXPECT_THROW, f, folly::Promise< T >::getFuture(), and folly::Promise< T >::setException().

311  {
312  {
313  Promise<Unit> p;
314  auto f = p.getFuture();
315  p.setException(eggs);
316  EXPECT_THROW(f.value(), eggs_t);
317  }
318  {
319  Promise<Unit> p;
320  auto f = p.getFuture();
322  EXPECT_THROW(f.value(), eggs_t);
323  }
324 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
void setException(exception_wrapper ew)
Definition: Promise-inl.h:111
FutureException eggs_t
Definition: PromiseTest.cpp:26
static eggs_t eggs("eggs")
Future< T > getFuture()
Definition: Promise-inl.h:97
TEST ( Promise  ,
setWith   
)

Definition at line 326 of file PromiseTest.cpp.

References eggs, EXPECT_EQ, EXPECT_THROW, f, folly::Promise< T >::getFuture(), and folly::Promise< T >::setWith().

326  {
327  {
328  Promise<int> p;
329  auto f = p.getFuture();
330  p.setWith([] { return 42; });
331  EXPECT_EQ(42, f.value());
332  }
333  {
334  Promise<int> p;
335  auto f = p.getFuture();
336  p.setWith([]() -> int { throw eggs; });
337  EXPECT_THROW(f.value(), eggs_t);
338  }
339 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void setWith(F &&func)
Definition: Promise-inl.h:137
FutureException eggs_t
Definition: PromiseTest.cpp:26
static eggs_t eggs("eggs")
Future< T > getFuture()
Definition: Promise-inl.h:97
TEST ( Promise  ,
isFulfilled   
)

Definition at line 341 of file PromiseTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, folly::Promise< T >::isFulfilled(), and folly::Promise< T >::setValue().

341  {
342  Promise<int> p;
343 
345  p.setValue(42);
347 }
bool isFulfilled() const noexcept
Definition: Promise-inl.h:143
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Promise  ,
isFulfilledWithFuture   
)

Definition at line 349 of file PromiseTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, f, folly::Promise< T >::getFuture(), folly::Promise< T >::isFulfilled(), and folly::Promise< T >::setValue().

349  {
350  Promise<int> p;
351  auto f = p.getFuture(); // so core_ will become null
352 
354  p.setValue(42); // after here
356 }
auto f
bool isFulfilled() const noexcept
Definition: Promise-inl.h:143
Future< T > getFuture()
Definition: Promise-inl.h:97
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Promise  ,
brokenOnDelete   
)

Definition at line 358 of file PromiseTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, f, and folly::pushmi::detail::t.

358  {
359  auto p = std::make_unique<Promise<int>>();
360  auto f = p->getFuture();
361 
362  EXPECT_FALSE(f.isReady());
363 
364  p.reset();
365 
366  EXPECT_TRUE(f.isReady());
367 
368  auto t = f.getTry();
369 
370  EXPECT_TRUE(t.hasException<BrokenPromise>());
371 }
auto f
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Promise  ,
brokenPromiseHasTypeInfo   
)

Definition at line 373 of file PromiseTest.cpp.

References EXPECT_NE.

373  {
374  auto pInt = std::make_unique<Promise<int>>();
375  auto fInt = pInt->getFuture();
376 
377  auto pFloat = std::make_unique<Promise<float>>();
378  auto fFloat = pFloat->getFuture();
379 
380  pInt.reset();
381  pFloat.reset();
382 
383  auto whatInt = fInt.getTry().exception().what();
384  auto whatFloat = fFloat.getTry().exception().what();
385 
386  EXPECT_NE(whatInt, whatFloat);
387 }
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926

Variable Documentation

eggs_t eggs("eggs")
static

Referenced by TEST().