From 07429fa9a6e0e705b8e297bd04ff41786b4f7f7a Mon Sep 17 00:00:00 2001 From: yang-g Date: Wed, 29 Mar 2017 10:57:31 -0700 Subject: [PATCH] Update time_cmp to ignore tv_nsec when tv_sec is INT64 MAX or MIN --- src/core/lib/support/time.c | 2 +- test/core/support/time_test.c | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/core/lib/support/time.c b/src/core/lib/support/time.c index 614a068ddd3..c5f94d46f7b 100644 --- a/src/core/lib/support/time.c +++ b/src/core/lib/support/time.c @@ -42,7 +42,7 @@ int gpr_time_cmp(gpr_timespec a, gpr_timespec b) { int cmp = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec); GPR_ASSERT(a.clock_type == b.clock_type); - if (cmp == 0) { + if (cmp == 0 && a.tv_sec != INT64_MAX && a.tv_sec != INT64_MIN) { cmp = (a.tv_nsec > b.tv_nsec) - (a.tv_nsec < b.tv_nsec); } return cmp; diff --git a/test/core/support/time_test.c b/test/core/support/time_test.c index 73e43caa696..4cb36a788c1 100644 --- a/test/core/support/time_test.c +++ b/test/core/support/time_test.c @@ -256,11 +256,19 @@ static void test_similar(void) { } static void test_convert_extreme(void) { - gpr_timespec realtime_t = {INT64_MAX, 1, GPR_CLOCK_REALTIME}; - gpr_timespec monotime_t = - gpr_convert_clock_type(realtime_t, GPR_CLOCK_MONOTONIC); - GPR_ASSERT(monotime_t.tv_sec == realtime_t.tv_sec); - GPR_ASSERT(monotime_t.clock_type == GPR_CLOCK_MONOTONIC); + gpr_timespec realtime = {INT64_MAX, 1, GPR_CLOCK_REALTIME}; + gpr_timespec monotime = gpr_convert_clock_type(realtime, GPR_CLOCK_MONOTONIC); + GPR_ASSERT(monotime.tv_sec == realtime.tv_sec); + GPR_ASSERT(monotime.clock_type == GPR_CLOCK_MONOTONIC); +} + +static void test_cmp_extreme(void) { + gpr_timespec t1 = {INT64_MAX, 1, GPR_CLOCK_REALTIME}; + gpr_timespec t2 = {INT64_MAX, 2, GPR_CLOCK_REALTIME}; + GPR_ASSERT(gpr_time_cmp(t1, t2) == 0); + t1.tv_sec = INT64_MIN; + t2.tv_sec = INT64_MIN; + GPR_ASSERT(gpr_time_cmp(t1, t2) == 0); } int main(int argc, char *argv[]) { @@ -272,5 +280,6 @@ int main(int argc, char *argv[]) { test_sticky_infinities(); test_similar(); test_convert_extreme(); + test_cmp_extreme(); return 0; }