Commit 66047c6a authored by Jeroen Vaelen's avatar Jeroen Vaelen Committed by Andre Azevedo

extend future api with Future<T>::poll

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
parent aae7c5b3
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <thread> #include <thread>
#include <folly/Baton.h> #include <folly/Baton.h>
#include <folly/Optional.h>
#include <folly/futures/detail/Core.h> #include <folly/futures/detail/Core.h>
#include <folly/futures/Timekeeper.h> #include <folly/futures/Timekeeper.h>
...@@ -343,6 +344,15 @@ Try<T>& Future<T>::getTry() { ...@@ -343,6 +344,15 @@ Try<T>& Future<T>::getTry() {
return core_->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 <class T>
template <typename Executor> template <typename Executor>
inline Future<T> Future<T>::via(Executor* executor) && { inline Future<T> Future<T>::via(Executor* executor) && {
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#include <folly/Optional.h>
#include <folly/MoveWrapper.h> #include <folly/MoveWrapper.h>
#include <folly/futures/Deprecated.h> #include <folly/futures/Deprecated.h>
#include <folly/futures/DrivableExecutor.h> #include <folly/futures/DrivableExecutor.h>
...@@ -253,6 +254,11 @@ class Future { ...@@ -253,6 +254,11 @@ class Future {
/** A reference to the Try of the value */ /** A reference to the Try of the value */
Try<T>& getTry(); 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 /// Block until the future is fulfilled. Returns the value (moved out), or
/// throws the exception. The future must not already have a callback. /// throws the exception. The future must not already have a callback.
T get(); T get();
......
/*
* 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());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment