From 7f47b00fca75bee477c8a2e3e8fc74a1cf7c743f Mon Sep 17 00:00:00 2001
From: Derek Mauro <dmauro@google.com>
Date: Mon, 13 Mar 2023 13:24:00 -0700
Subject: Synchronization: Change KernelTimeout to always store absolute
 timeouts, but when a relative timeout is provided, the timeout is an absolute
 timeout against a steady clock (when possible). This allows methods that
 return relative timeouts to automatically recompute the remaining duration,
 for instance, on suprious wakeups.

PiperOrigin-RevId: 516304139
Change-Id: I7d739cb50dd749eba5dba7ac6c34d18dc53703ed
---
 absl/synchronization/internal/kernel_timeout_test.cc | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

(limited to 'absl/synchronization/internal/kernel_timeout_test.cc')

diff --git a/absl/synchronization/internal/kernel_timeout_test.cc b/absl/synchronization/internal/kernel_timeout_test.cc
index 431ffcf4..a96f806f 100644
--- a/absl/synchronization/internal/kernel_timeout_test.cc
+++ b/absl/synchronization/internal/kernel_timeout_test.cc
@@ -62,9 +62,6 @@ TEST(KernelTimeout, FiniteTimes) {
     EXPECT_TRUE(t.is_absolute_timeout());
     EXPECT_FALSE(t.is_relative_timeout());
     EXPECT_EQ(absl::TimeFromTimespec(t.MakeAbsTimespec()), when);
-    // MakeRelativeTimespec() doesn't quite round trip when using an absolute
-    // time, but it should get pretty close. Past times are converted to zero
-    // durations.
     EXPECT_LE(
         absl::AbsDuration(absl::DurationFromTimespec(t.MakeRelativeTimespec()) -
                           std::max(duration, absl::ZeroDuration())),
@@ -201,7 +198,10 @@ TEST(KernelTimeout, FiniteDurations) {
     EXPECT_LE(absl::AbsDuration(absl::Now() + duration -
                                 absl::TimeFromTimespec(t.MakeAbsTimespec())),
               absl::Milliseconds(5));
-    EXPECT_EQ(absl::DurationFromTimespec(t.MakeRelativeTimespec()), duration);
+    EXPECT_LE(
+        absl::AbsDuration(absl::DurationFromTimespec(t.MakeRelativeTimespec()) -
+                          duration),
+        kTimingBound);
     EXPECT_LE(absl::AbsDuration(absl::Now() + duration -
                                 absl::FromUnixNanos(t.MakeAbsNanos())),
               absl::Milliseconds(5));
@@ -210,7 +210,9 @@ TEST(KernelTimeout, FiniteDurations) {
     EXPECT_LE(absl::AbsDuration(absl::Now() + duration -
                                 absl::FromChrono(t.ToChronoTimePoint())),
               kTimingBound);
-    EXPECT_EQ(absl::FromChrono(t.ToChronoDuration()), duration);
+    EXPECT_LE(
+        absl::AbsDuration(absl::FromChrono(t.ToChronoDuration()) - duration),
+        kTimingBound);
   }
 }
 
@@ -298,7 +300,6 @@ TEST(KernelTimeout, OverflowNanos) {
   int64_t limit = std::numeric_limits<int64_t>::max() - now_nanos;
   absl::Duration duration = absl::Nanoseconds(limit) + absl::Seconds(1);
   KernelTimeout t(duration);
-  EXPECT_TRUE(t.has_timeout());
   // Timeouts should still be far in the future.
   EXPECT_GT(absl::TimeFromTimespec(t.MakeAbsTimespec()),
             absl::Now() + absl::Hours(100000));
-- 
cgit v1.2.3