From: Hans Fugal Date: Thu, 2 Jul 2015 01:21:28 +0000 (-0700) Subject: minor Timekeeper bug X-Git-Tag: v0.49.0~9 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=b8147234df41cfe5997061dbf5e36f2e8cd822d6;p=folly.git 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 --- 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))); +}