Update time_cmp to ignore tv_nsec when tv_sec is INT64 MAX or MIN

pull/10352/head
yang-g 8 years ago
parent 1aba869e9e
commit 07429fa9a6
  1. 2
      src/core/lib/support/time.c
  2. 19
      test/core/support/time_test.c

@ -42,7 +42,7 @@
int gpr_time_cmp(gpr_timespec a, gpr_timespec b) { int gpr_time_cmp(gpr_timespec a, gpr_timespec b) {
int cmp = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec); int cmp = (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec);
GPR_ASSERT(a.clock_type == b.clock_type); 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); cmp = (a.tv_nsec > b.tv_nsec) - (a.tv_nsec < b.tv_nsec);
} }
return cmp; return cmp;

@ -256,11 +256,19 @@ static void test_similar(void) {
} }
static void test_convert_extreme(void) { static void test_convert_extreme(void) {
gpr_timespec realtime_t = {INT64_MAX, 1, GPR_CLOCK_REALTIME}; gpr_timespec realtime = {INT64_MAX, 1, GPR_CLOCK_REALTIME};
gpr_timespec monotime_t = gpr_timespec monotime = gpr_convert_clock_type(realtime, GPR_CLOCK_MONOTONIC);
gpr_convert_clock_type(realtime_t, GPR_CLOCK_MONOTONIC); GPR_ASSERT(monotime.tv_sec == realtime.tv_sec);
GPR_ASSERT(monotime_t.tv_sec == realtime_t.tv_sec); GPR_ASSERT(monotime.clock_type == GPR_CLOCK_MONOTONIC);
GPR_ASSERT(monotime_t.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[]) { int main(int argc, char *argv[]) {
@ -272,5 +280,6 @@ int main(int argc, char *argv[]) {
test_sticky_infinities(); test_sticky_infinities();
test_similar(); test_similar();
test_convert_extreme(); test_convert_extreme();
test_cmp_extreme();
return 0; return 0;
} }

Loading…
Cancel
Save