Short-Circuit within() When Future Is Already Complete
authorFelix Handte <felixh@fb.com>
Fri, 29 Sep 2017 19:41:24 +0000 (12:41 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 29 Sep 2017 19:55:35 +0000 (12:55 -0700)
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
folly/futures/test/TimekeeperTest.cpp

index fa39dc84987a81a3bac8156719c9285b1716065a..3f2f54fbf6e015149d161c4dcfb5b55ce666c777 100644 (file)
@@ -1067,6 +1067,10 @@ Future<T> Future<T>::within(Duration dur, E e, Timekeeper* tk) {
     std::atomic<bool> token {false};
   };
 
+  if (this->isReady()) {
+    return std::move(*this);
+  }
+
   std::shared_ptr<Timekeeper> tks;
   if (!tk) {
     tks = folly::detail::getTimekeeperSingleton();
index 08624dad2b47db7d71a8f7ddd5bdbfce91247ba7..bc3a8aed219b94779c6fd64dd69d9b9b9dd5d2cd 100644 (file)
@@ -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<int> count{0};
   };
 
-  auto f = makeFuture();
+  Promise<Unit> 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);
 }