From 1aba869e9e3046766cab8744a028a20a8af62dd2 Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 28 Mar 2017 21:41:02 -0700 Subject: [PATCH 1/2] Make convert clock_type consistent with add/sub when dealing with extreme values --- src/core/lib/support/time.c | 12 +++--------- test/core/support/time_test.c | 9 +++++++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/core/lib/support/time.c b/src/core/lib/support/time.c index 5a7d043aed8..614a068ddd3 100644 --- a/src/core/lib/support/time.c +++ b/src/core/lib/support/time.c @@ -244,15 +244,9 @@ gpr_timespec gpr_convert_clock_type(gpr_timespec t, gpr_clock_type clock_type) { return t; } - if (t.tv_nsec == 0) { - if (t.tv_sec == INT64_MAX) { - t.clock_type = clock_type; - return t; - } - if (t.tv_sec == INT64_MIN) { - t.clock_type = clock_type; - return t; - } + if (t.tv_sec == INT64_MAX || t.tv_sec == INT64_MIN) { + t.clock_type = clock_type; + return t; } if (clock_type == GPR_TIMESPAN) { diff --git a/test/core/support/time_test.c b/test/core/support/time_test.c index e9ca08d0419..73e43caa696 100644 --- a/test/core/support/time_test.c +++ b/test/core/support/time_test.c @@ -255,6 +255,14 @@ static void test_similar(void) { gpr_time_from_micros(10, GPR_TIMESPAN))); } +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); +} + int main(int argc, char *argv[]) { grpc_test_init(argc, argv); @@ -263,5 +271,6 @@ int main(int argc, char *argv[]) { test_overflow(); test_sticky_infinities(); test_similar(); + test_convert_extreme(); return 0; } From 07429fa9a6e0e705b8e297bd04ff41786b4f7f7a Mon Sep 17 00:00:00 2001 From: yang-g Date: Wed, 29 Mar 2017 10:57:31 -0700 Subject: [PATCH 2/2] 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; }