From 66047c6ab0f4b6cbd083d76021cfafef3f8e4c7a Mon Sep 17 00:00:00 2001 From: Jeroen Vaelen Date: Fri, 13 Mar 2015 14:10:55 -0700 Subject: [PATCH] extend future api with Future::poll Summary: Added folly::Future::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 to construct a new Try that is wrapped in an Optional>. 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 | 10 ++++++++ folly/futures/Future.h | 6 +++++ folly/futures/test/SugarTest.cpp | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 folly/futures/test/SugarTest.cpp diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index 12dbabb9..077a633e 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -343,6 +344,15 @@ Try& Future::getTry() { return core_->getTry(); } +template +Optional> Future::poll() { + Optional> o; + if (core_->ready()) { + o = std::move(core_->getTry()); + } + return o; +} + template template inline Future Future::via(Executor* executor) && { diff --git a/folly/futures/Future.h b/folly/futures/Future.h index 2b81407f..10bdc45a 100644 --- a/folly/futures/Future.h +++ b/folly/futures/Future.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -253,6 +254,11 @@ class Future { /** A reference to the Try of the value */ Try& getTry(); + /// If the promise has been fulfilled, return an Optional with the Try. + /// Otherwise return an empty Optional. + /// Note that this moves the Try out. + Optional> 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 index 00000000..152fe546 --- /dev/null +++ b/folly/futures/test/SugarTest.cpp @@ -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 + +#include + +using namespace folly; + +TEST(Sugar, pollReady) { + Promise p; + auto f = p.getFuture(); + p.setValue(42); + EXPECT_EQ(42, f.poll().value().value()); +} + +TEST(SUGAR, pollNotReady) { + Promise p; + auto f = p.getFuture(); + EXPECT_FALSE(f.poll().hasValue()); +} + +TEST(SUGAR, pollException) { + Promise p; + auto f = p.getFuture(); + p.fulfil([] { throw std::runtime_error("Runtime"); }); + EXPECT_TRUE(f.poll().value().hasException()); +} -- 2.34.1