extend future api with Future<T>::poll
authorJeroen Vaelen <jeroenv@fb.com>
Fri, 13 Mar 2015 21:10:55 +0000 (14:10 -0700)
committerAndre Azevedo <aap@fb.com>
Wed, 18 Mar 2015 03:18:46 +0000 (20:18 -0700)
Summary:
Added folly::Future<T>::poll() function with the following semantics:
poll() checks if the promise has been fulfilled, if so it will extract the value of the resulting Try<T> to construct a new Try<T> that is wrapped in an Optional<Try<T>>. If the promise has not been fulfilled an empty Optional is returned.

Test Plan: Added two tests to SugarTest.cpp: one where the promise has been fulfilled and one where it hasn't.

Reviewed By: hannesr@fb.com

Subscribers: trunkagent, folly-diffs@, jsedgwick, arturg, yfeldblum

FB internal diff: D1909480

Tasks: 6166875

Signature: t1:1909480:1426280247:6b239c0f1a15c25ce7664afb4bfb3899993c5775

folly/futures/Future-inl.h
folly/futures/Future.h
folly/futures/test/SugarTest.cpp [new file with mode: 0644]

index 12dbabb949331aae8c212b618c116051c04537b5..077a633e9bd8d13146031cf2d72b175094d879cf 100644 (file)
@@ -20,6 +20,7 @@
 #include <thread>
 
 #include <folly/Baton.h>
+#include <folly/Optional.h>
 #include <folly/futures/detail/Core.h>
 #include <folly/futures/Timekeeper.h>
 
@@ -343,6 +344,15 @@ Try<T>& Future<T>::getTry() {
   return core_->getTry();
 }
 
+template <class T>
+Optional<Try<T>> Future<T>::poll() {
+  Optional<Try<T>> o;
+  if (core_->ready()) {
+    o = std::move(core_->getTry());
+  }
+  return o;
+}
+
 template <class T>
 template <typename Executor>
 inline Future<T> Future<T>::via(Executor* executor) && {
index 2b81407f55c50a6dc347907fd9b1d11660060a65..10bdc45ae94df80e63f752e25b0f02aca6750ce7 100644 (file)
@@ -23,6 +23,7 @@
 #include <type_traits>
 #include <vector>
 
+#include <folly/Optional.h>
 #include <folly/MoveWrapper.h>
 #include <folly/futures/Deprecated.h>
 #include <folly/futures/DrivableExecutor.h>
@@ -253,6 +254,11 @@ class Future {
   /** A reference to the Try of the value */
   Try<T>& getTry();
 
+  /// If the promise has been fulfilled, return an Optional with the Try<T>.
+  /// Otherwise return an empty Optional.
+  /// Note that this moves the Try<T> out.
+  Optional<Try<T>> poll();
+
   /// Block until the future is fulfilled. Returns the value (moved out), or
   /// throws the exception. The future must not already have a callback.
   T get();
diff --git a/folly/futures/test/SugarTest.cpp b/folly/futures/test/SugarTest.cpp
new file mode 100644 (file)
index 0000000..152fe54
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2015 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <folly/futures/Future.h>
+
+using namespace folly;
+
+TEST(Sugar, pollReady) {
+  Promise<int> p;
+  auto f = p.getFuture();
+  p.setValue(42);
+  EXPECT_EQ(42, f.poll().value().value());
+}
+
+TEST(SUGAR, pollNotReady) {
+  Promise<int> p;
+  auto f = p.getFuture();
+  EXPECT_FALSE(f.poll().hasValue());
+}
+
+TEST(SUGAR, pollException) {
+  Promise<void> p;
+  auto f = p.getFuture();
+  p.fulfil([] { throw std::runtime_error("Runtime"); });
+  EXPECT_TRUE(f.poll().value().hasException());
+}