From: Felix Handte Date: Fri, 29 Sep 2017 19:41:24 +0000 (-0700) Subject: Short-Circuit within() When Future Is Already Complete X-Git-Tag: v2017.10.02.00~2 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=49af015a1d7e8c91d7ba6e78de462b8e28b0c706;p=folly.git 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 --- 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); }