minor Timekeeper bug
authorHans Fugal <fugalh@fb.com>
Thu, 2 Jul 2015 01:21:28 +0000 (18:21 -0700)
committerSara Golemon <sgolemon@fb.com>
Thu, 2 Jul 2015 18:03:13 +0000 (11:03 -0700)
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
folly/futures/test/TimekeeperTest.cpp

index 024b9eb7b3e65052a88271665de022f20fb9a584..8a78b8f2a393c93dbf4713e5fe5f21a480e68dcd 100644 (file)
@@ -46,6 +46,9 @@ template <class> 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<Duration>(
+///      someNanoseconds))
 class Timekeeper {
  public:
   virtual ~Timekeeper() = default;
@@ -89,7 +92,7 @@ Future<Unit> Timekeeper::at(std::chrono::time_point<Clock> when) {
     return makeFuture();
   }
 
-  return after(when - now);
+  return after(std::chrono::duration_cast<Duration>(when - now));
 }
 
 } // namespace folly
index fdf147c5e7e252589e0e2b76be1ec1f16a901a98..eddf2e811739db0e7f187f476130c64108d24d10 100644 (file)
@@ -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<Duration>(
+      std::chrono::nanoseconds(1)));
+}