Commit 49af015a authored by Felix Handte's avatar Felix Handte Committed by Facebook Github Bot

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
parent 46d6155c
...@@ -1067,6 +1067,10 @@ Future<T> Future<T>::within(Duration dur, E e, Timekeeper* tk) { ...@@ -1067,6 +1067,10 @@ Future<T> Future<T>::within(Duration dur, E e, Timekeeper* tk) {
std::atomic<bool> token {false}; std::atomic<bool> token {false};
}; };
if (this->isReady()) {
return std::move(*this);
}
std::shared_ptr<Timekeeper> tks; std::shared_ptr<Timekeeper> tks;
if (!tk) { if (!tk) {
tks = folly::detail::getTimekeeperSingleton(); tks = folly::detail::getTimekeeperSingleton();
......
...@@ -133,6 +133,14 @@ TEST(Timekeeper, onTimeout) { ...@@ -133,6 +133,14 @@ TEST(Timekeeper, onTimeout) {
EXPECT_TRUE(flag); 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) { TEST(Timekeeper, onTimeoutReturnsFuture) {
bool flag = false; bool flag = false;
makeFuture(42).delayed(10 * one_ms) makeFuture(42).delayed(10 * one_ms)
...@@ -177,9 +185,11 @@ TEST(Timekeeper, executor) { ...@@ -177,9 +185,11 @@ TEST(Timekeeper, executor) {
std::atomic<int> count{0}; std::atomic<int> count{0};
}; };
auto f = makeFuture(); Promise<Unit> p;
ExecutorTester tester; 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); EXPECT_EQ(2, tester.count);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment