Merge pull request #10352 from yang-g/time_conversion

Make convert clock_type consistent with add/sub when dealing with ext…
pull/9820/head
Yang Gao 8 years ago committed by GitHub
commit a232b52c4a
  1. 14
      src/core/lib/support/time.c
  2. 18
      test/core/support/time_test.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;
@ -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) {

@ -255,6 +255,22 @@ static void test_similar(void) {
gpr_time_from_micros(10, GPR_TIMESPAN)));
}
static void test_convert_extreme(void) {
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[]) {
grpc_test_init(argc, argv);
@ -263,5 +279,7 @@ int main(int argc, char *argv[]) {
test_overflow();
test_sticky_infinities();
test_similar();
test_convert_extreme();
test_cmp_extreme();
return 0;
}

Loading…
Cancel
Save