Promise::isFulfilled()
authorHans Fugal <fugalh@fb.com>
Wed, 27 May 2015 01:10:15 +0000 (18:10 -0700)
committerPavlo Kushnir <pavlo@fb.com>
Thu, 28 May 2015 00:53:02 +0000 (17:53 -0700)
Summary: See task

Test Plan: runtests

Reviewed By: jsedgwick@fb.com

Subscribers: exa, folly-diffs@, jsedgwick, yfeldblum, chalfant

FB internal diff: D2101549

Tasks: 7225286

Signature: t1:2101549:1432688294:5fb9d7370c38c1392429a09ba48b131cac16647d

folly/futures/Promise-inl.h
folly/futures/Promise.h
folly/futures/test/FutureTest.cpp

index 7019b72014598fd65c393e46fb94e1bb9f574a7e..245b0d6fee6325472c670f818b92dbbd30a5b8b2 100644 (file)
@@ -130,4 +130,12 @@ void Promise<T>::setWith(F&& func) {
   setTry(makeTryWith(std::forward<F>(func)));
 }
 
+template <class T>
+bool Promise<T>::isFulfilled() {
+  if (core_) {
+    return core_->hasResult();
+  }
+  return true;
+}
+
 }
index 0ee979ea440f9705fbfead76837696611c0201de..27b20bd830f816898a8ffface7b2805fab46d5b3 100644 (file)
@@ -99,6 +99,8 @@ public:
   template <class F>
   void setWith(F&& func);
 
+  bool isFulfilled();
+
 private:
   typedef typename Future<T>::corePtr corePtr;
 
index f6c295e0a75ce8db6791c15a20defef9eb256985..58619d6938e1e96bca762ab4ddf38d96eeba6aa9 100644 (file)
@@ -1914,3 +1914,20 @@ TEST(Map, Basic) {
 
   EXPECT_TRUE(collect(fs2).isReady());
 }
+
+TEST(Promise, isFulfilled) {
+  Promise<int> p;
+
+  EXPECT_FALSE(p.isFulfilled());
+  p.setValue(42);
+  EXPECT_TRUE(p.isFulfilled());
+}
+
+TEST(Promise, isFulfilledWithFuture) {
+  Promise<int> p;
+  auto f = p.getFuture(); // so core_ will become null
+
+  EXPECT_FALSE(p.isFulfilled());
+  p.setValue(42); // after here
+  EXPECT_TRUE(p.isFulfilled());
+}