diff --git a/src/core/lib/gprpp/time.h b/src/core/lib/gprpp/time.h index 65aa968bc8e..c0f8a3e567e 100644 --- a/src/core/lib/gprpp/time.h +++ b/src/core/lib/gprpp/time.h @@ -322,6 +322,12 @@ inline Timestamp operator-(Timestamp lhs, Duration rhs) { inline Timestamp operator+(Duration lhs, Timestamp rhs) { return rhs + lhs; } inline Duration operator-(Timestamp lhs, Timestamp rhs) { + if (rhs == Timestamp::InfPast() && lhs != Timestamp::InfPast()) { + return Duration::Infinity(); + } + if (rhs == Timestamp::InfFuture() && lhs != Timestamp::InfFuture()) { + return Duration::NegativeInfinity(); + } return Duration::Milliseconds( time_detail::MillisAdd(lhs.milliseconds_after_process_epoch(), -rhs.milliseconds_after_process_epoch())); diff --git a/src/core/load_balancing/weighted_round_robin/weighted_round_robin.cc b/src/core/load_balancing/weighted_round_robin/weighted_round_robin.cc index 66d5b03325b..8e28f6b2c3b 100644 --- a/src/core/load_balancing/weighted_round_robin/weighted_round_robin.cc +++ b/src/core/load_balancing/weighted_round_robin/weighted_round_robin.cc @@ -213,7 +213,7 @@ class WeightedRoundRobin final : public LoadBalancingPolicy { Mutex mu_; float weight_ ABSL_GUARDED_BY(&mu_) = 0; Timestamp non_empty_since_ ABSL_GUARDED_BY(&mu_) = Timestamp::InfFuture(); - Timestamp last_update_time_ ABSL_GUARDED_BY(&mu_) = Timestamp::InfPast(); + Timestamp last_update_time_ ABSL_GUARDED_BY(&mu_) = Timestamp::InfFuture(); }; class WrrEndpointList final : public EndpointList { diff --git a/test/core/gprpp/time_test.cc b/test/core/gprpp/time_test.cc index c0509c035ee..db554ac68cb 100644 --- a/test/core/gprpp/time_test.cc +++ b/test/core/gprpp/time_test.cc @@ -30,6 +30,17 @@ TEST(TimestampTest, Infinities) { Timestamp::InfFuture()); EXPECT_EQ(Timestamp::InfPast() + Duration::Milliseconds(1), Timestamp::InfPast()); + EXPECT_EQ(Timestamp::Now() - Timestamp::InfPast(), Duration::Infinity()); + EXPECT_EQ(Timestamp::Now() - Timestamp::InfFuture(), + Duration::NegativeInfinity()); + EXPECT_EQ(Timestamp::InfPast() - Timestamp::InfPast(), + Duration::NegativeInfinity()); + EXPECT_EQ(Timestamp::InfFuture() - Timestamp::InfPast(), + Duration::Infinity()); + EXPECT_EQ(Timestamp::InfFuture() - Timestamp::InfFuture(), + Duration::Infinity()); + EXPECT_EQ(Timestamp::InfPast() - Timestamp::InfFuture(), + Duration::NegativeInfinity()); } TEST(TimestampTest, ToString) {