From b8147234df41cfe5997061dbf5e36f2e8cd822d6 Mon Sep 17 00:00:00 2001 From: Hans Fugal Date: Wed, 1 Jul 2015 18:21:28 -0700 Subject: [PATCH] minor Timekeeper bug Summary: Fix so `Timekeeper::at(now() - something)` works. Also introduce a test which explicitly tests when <= now codepath, which wasn't broken per se here, but which test also tickled this bug. Reviewed By: @jsedgwick Differential Revision: D2209166 --- folly/futures/Timekeeper.h | 5 ++++- folly/futures/test/TimekeeperTest.cpp | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/folly/futures/Timekeeper.h b/folly/futures/Timekeeper.h index 024b9eb7..8a78b8f2 100644 --- a/folly/futures/Timekeeper.h +++ b/folly/futures/Timekeeper.h @@ -46,6 +46,9 @@ template class Future; /// over Duration. This makes the code more legible and means you won't be /// unpleasantly surprised if we redefine Duration to microseconds, or /// something. +/// +/// timekeeper.after(std::chrono::duration_cast( +/// someNanoseconds)) class Timekeeper { public: virtual ~Timekeeper() = default; @@ -89,7 +92,7 @@ Future Timekeeper::at(std::chrono::time_point when) { return makeFuture(); } - return after(when - now); + return after(std::chrono::duration_cast(when - now)); } } // namespace folly diff --git a/folly/futures/test/TimekeeperTest.cpp b/folly/futures/test/TimekeeperTest.cpp index fdf147c5..eddf2e81 100644 --- a/folly/futures/test/TimekeeperTest.cpp +++ b/folly/futures/test/TimekeeperTest.cpp @@ -198,3 +198,16 @@ TEST(Timekeeper, onTimeoutPropagates) { EXPECT_TRUE(flag); } */ + +TEST_F(TimekeeperFixture, atBeforeNow) { + auto f = timeLord_->at(now() - too_long); + EXPECT_TRUE(f.isReady()); + EXPECT_FALSE(f.hasException()); +} + +TEST_F(TimekeeperFixture, howToCastDuration) { + // I'm not sure whether this rounds up or down but it's irrelevant for the + // purpose of this example. + auto f = timeLord_->after(std::chrono::duration_cast( + std::chrono::nanoseconds(1))); +} -- 2.34.1