From c1bd7145c3f0dcde7e4dc4681a5b287046f683db Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Thu, 1 Dec 2016 12:43:06 -0800 Subject: [PATCH] Future::getTryVia Summary: [Folly] `Future::getTryVia`. We have `waitVia` and `getVia` and `getTry`, but `getTryVia` was missing. Reviewed By: fugalh Differential Revision: D4254972 fbshipit-source-id: 403e1a3496ad5dfc93c4c55ab75a83bcc89e6c64 --- folly/futures/Future-inl.h | 5 +++++ folly/futures/Future.h | 5 +++++ folly/futures/test/ViaTest.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index 1cd4035e..61ea4ba8 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -415,6 +415,11 @@ Try& Future::getTry() { return core_->getTry(); } +template +Try& Future::getTryVia(DrivableExecutor* e) { + return waitVia(e).getTry(); +} + template Optional> Future::poll() { Optional> o; diff --git a/folly/futures/Future.h b/folly/futures/Future.h index 7563507d..23cd5a96 100644 --- a/folly/futures/Future.h +++ b/folly/futures/Future.h @@ -129,6 +129,11 @@ class Future { /** A reference to the Try of the value */ Try& getTry(); + /// Call e->drive() repeatedly until the future is fulfilled. Examples + /// of DrivableExecutor include EventBase and ManualExecutor. Returns a + /// reference to the Try of the value. + Try& getTryVia(DrivableExecutor* e); + /// If the promise has been fulfilled, return an Optional with the Try. /// Otherwise return an empty Optional. /// Note that this moves the Try out. diff --git a/folly/futures/test/ViaTest.cpp b/folly/futures/test/ViaTest.cpp index 2914cb2a..69bb9679 100644 --- a/folly/futures/test/ViaTest.cpp +++ b/folly/futures/test/ViaTest.cpp @@ -400,6 +400,32 @@ TEST(Via, getVia) { } } +TEST(Via, getTryVia) { + { + // non-void + ManualExecutor x; + auto f = via(&x).then([] { return 23; }); + EXPECT_FALSE(f.isReady()); + EXPECT_EQ(23, f.getTryVia(&x).value()); + } + + { + // void + ManualExecutor x; + auto f = via(&x).then(); + EXPECT_FALSE(f.isReady()); + auto t = f.getTryVia(&x); + EXPECT_TRUE(t.hasValue()); + } + + { + DummyDrivableExecutor x; + auto f = makeFuture(23); + EXPECT_EQ(23, f.getTryVia(&x).value()); + EXPECT_FALSE(x.ran); + } +} + TEST(Via, waitVia) { { ManualExecutor x; -- 2.34.1