From 49af015a1d7e8c91d7ba6e78de462b8e28b0c706 Mon Sep 17 00:00:00 2001 From: Felix Handte Date: Fri, 29 Sep 2017 12:41:24 -0700 Subject: [PATCH] Short-Circuit within() When Future Is Already Complete Summary: As title. No sense adding a timeout to the timeout manager when the future is already complete. Reviewed By: yfeldblum Differential Revision: D5933144 fbshipit-source-id: 4d1bbd866c47ccee6bd0518cbe063afc1d34cbca --- folly/futures/Future-inl.h | 4 ++++ folly/futures/test/TimekeeperTest.cpp | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index fa39dc84..3f2f54fb 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -1067,6 +1067,10 @@ Future Future::within(Duration dur, E e, Timekeeper* tk) { std::atomic token {false}; }; + if (this->isReady()) { + return std::move(*this); + } + std::shared_ptr tks; if (!tk) { tks = folly::detail::getTimekeeperSingleton(); diff --git a/folly/futures/test/TimekeeperTest.cpp b/folly/futures/test/TimekeeperTest.cpp index 08624dad..bc3a8aed 100644 --- a/folly/futures/test/TimekeeperTest.cpp +++ b/folly/futures/test/TimekeeperTest.cpp @@ -133,6 +133,14 @@ TEST(Timekeeper, onTimeout) { EXPECT_TRUE(flag); } +TEST(Timekeeper, onTimeoutComplete) { + bool flag = false; + makeFuture(42) + .onTimeout(zero_ms, [&]{ flag = true; return -1; }) + .get(); + EXPECT_FALSE(flag); +} + TEST(Timekeeper, onTimeoutReturnsFuture) { bool flag = false; makeFuture(42).delayed(10 * one_ms) @@ -177,9 +185,11 @@ TEST(Timekeeper, executor) { std::atomic count{0}; }; - auto f = makeFuture(); + Promise p; ExecutorTester tester; - f.via(&tester).within(one_ms).then([&](){}).wait(); + auto f = p.getFuture().via(&tester).within(one_ms).then([&](){}); + p.setValue(); + f.wait(); EXPECT_EQ(2, tester.count); } -- 2.34.1