From 8a8767014e221e18c4023c2864918db4e84435cd Mon Sep 17 00:00:00 2001 From: Hannes Roth Date: Sat, 18 Jul 2015 11:58:52 -0700 Subject: [PATCH] Revert: (Wangle) within should raise TimedOut() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Summary: This reverts commit 956351018a495af89575526536af7e7c0bb285aa. Reviewed By: @​labrams Differential Revision: D2258219 --- folly/futures/Future-inl.h | 33 +++++++++------------- folly/futures/FutureException.h | 5 ---- folly/futures/test/InterruptTest.cpp | 42 ---------------------------- 3 files changed, 13 insertions(+), 67 deletions(-) diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index d79fea34..380b0069 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -855,41 +855,34 @@ template Future Future::within(Duration dur, E e, Timekeeper* tk) { struct Context { - Context(E ex, Future&& f) - : exception(std::move(ex)), afterFuture(std::move(f)), promise() {} + Context(E ex) : exception(std::move(ex)), promise() {} E exception; - Future afterFuture; - Future thisFuture; Promise promise; std::atomic token {false}; }; + auto ctx = std::make_shared(std::move(e)); if (!tk) { tk = folly::detail::getTimekeeperSingleton(); } - auto ctx = std::make_shared(std::move(e), tk->after(dur)); + tk->after(dur) + .then([ctx](Try const& t) { + if (ctx->token.exchange(true) == false) { + if (t.hasException()) { + ctx->promise.setException(std::move(t.exception())); + } else { + ctx->promise.setException(std::move(ctx->exception)); + } + } + }); - ctx->thisFuture = this->then([ctx](Try&& t) mutable { - // "this" completed first, cancel "after" - ctx->afterFuture.raise(CancelTimer()); + this->then([ctx](Try&& t) { if (ctx->token.exchange(true) == false) { ctx->promise.setTry(std::move(t)); } }); - ctx->afterFuture.then([ctx](Try const& t) mutable { - // "after" completed first, cancel "this" - ctx->thisFuture.raise(TimedOut()); - if (ctx->token.exchange(true) == false) { - if (t.hasException()) { - ctx->promise.setException(std::move(t.exception())); - } else { - ctx->promise.setException(std::move(ctx->exception)); - } - } - }); - return ctx->promise.getFuture().via(getExecutor()); } diff --git a/folly/futures/FutureException.h b/folly/futures/FutureException.h index c5dbf158..1e004dcf 100644 --- a/folly/futures/FutureException.h +++ b/folly/futures/FutureException.h @@ -91,11 +91,6 @@ class TimedOut : public FutureException { TimedOut() : FutureException("Timed out") {} }; -class CancelTimer : public FutureException { - public: - CancelTimer() : FutureException("Timer should be cancelled") {} -}; - class PredicateDoesNotObtain : public FutureException { public: PredicateDoesNotObtain() : FutureException("Predicate does not obtain") {} diff --git a/folly/futures/test/InterruptTest.cpp b/folly/futures/test/InterruptTest.cpp index 7d34106f..99f8b696 100644 --- a/folly/futures/test/InterruptTest.cpp +++ b/folly/futures/test/InterruptTest.cpp @@ -18,7 +18,6 @@ #include #include -#include using namespace folly; @@ -73,44 +72,3 @@ TEST(Interrupt, secondInterruptNoop) { f.cancel(); EXPECT_EQ(1, count); } - -TEST(Interrupt, withinTimedOut) { - Promise p; - Baton<> done; - p.setInterruptHandler([&](const exception_wrapper& e) { done.post(); }); - p.getFuture().within(std::chrono::milliseconds(1)); - // Give it 100ms to time out and call the interrupt handler - auto t = std::chrono::steady_clock::now() + std::chrono::milliseconds(100); - EXPECT_TRUE(done.timed_wait(t)); -} - -class DummyTimeKeeper : public Timekeeper { - public: - explicit DummyTimeKeeper() : interrupted() {} - - Future after(Duration) override { - promise.setInterruptHandler( - [this](const exception_wrapper& e) { - EXPECT_THROW(e.throwException(), CancelTimer); - interrupted.post(); - } - ); - return promise.getFuture(); - } - - Baton<> interrupted; - - private: - Promise promise; -}; - -TEST(Interrupt, withinCancelTimer) { - DummyTimeKeeper tk; - Promise p; - Baton<> done; - p.getFuture().within(std::chrono::milliseconds(10), TimedOut(), &tk); - p.setValue(1); // this should cancel the timer - // Give it 100ms to interrupt the timer Future - auto t = std::chrono::steady_clock::now() + std::chrono::milliseconds(100); - EXPECT_TRUE(tk.interrupted.timed_wait(t)); -} -- 2.34.1