[time] Fix Timestamp minus arithmetic when rhs is InfPast or InfFuture (#36219)

modeled after absl/time

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes #36219

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36219 from yijiem:fix-infinities e102d7fe21
PiperOrigin-RevId: 621287985
pull/36237/head
Yijie Ma 12 months ago committed by Copybara-Service
parent 5c3400e8dc
commit d7c38ea9a1
  1. 6
      src/core/lib/gprpp/time.h
  2. 2
      src/core/load_balancing/weighted_round_robin/weighted_round_robin.cc
  3. 11
      test/core/gprpp/time_test.cc

@ -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()));

@ -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 {

@ -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) {

Loading…
Cancel
Save