Protect on some overflow scenarios, add a test and build/run the test

pull/1108/head
Yang Gao 10 years ago
parent b7b965c842
commit cdb2a6e071
  1. 47
      Makefile
  2. 15
      build.json
  3. 6
      src/cpp/util/time.cc
  4. 15
      test/cpp/util/time_test.cc
  5. 5
      tools/run_tests/tests.json

File diff suppressed because one or more lines are too long

@ -1707,6 +1707,21 @@
"gpr"
]
},
{
"name": "cxx_time_test",
"build": "test",
"language": "c++",
"src": [
"test/cpp/util/time_test.cc"
],
"deps": [
"grpc_test_util",
"grpc++",
"grpc",
"gpr_test_util",
"gpr"
]
},
{
"name": "end2end_test",
"build": "test",

@ -42,11 +42,15 @@ using std::chrono::system_clock;
namespace grpc {
// TODO(yangg) prevent potential overflow.
void Timepoint2Timespec(const system_clock::time_point& from,
gpr_timespec* to) {
system_clock::duration deadline = from.time_since_epoch();
seconds secs = duration_cast<seconds>(deadline);
if (from == system_clock::time_point::max() ||
secs.count() >= gpr_inf_future.tv_sec || secs.count() < 0) {
*to = gpr_inf_future;
return;
}
nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
to->tv_sec = secs.count();
to->tv_nsec = nsecs.count();

@ -61,11 +61,24 @@ TEST_F(TimeTest, AbsolutePointTest) {
EXPECT_TRUE(tp == tp_converted_2);
}
// gpr_inf_future is treated specially and mapped to time_point::max()
// gpr_inf_future is treated specially and mapped to/from time_point::max()
TEST_F(TimeTest, InfFuture) {
EXPECT_EQ(system_clock::time_point::max(),
Timespec2Timepoint(gpr_inf_future));
gpr_timespec from_time_point_max;
Timepoint2Timespec(system_clock::time_point::max(), &from_time_point_max);
EXPECT_EQ(0, gpr_time_cmp(gpr_inf_future, from_time_point_max));
// This will cause an overflow
Timepoint2Timespec(
std::chrono::time_point<system_clock, std::chrono::seconds>::max(),
&from_time_point_max);
EXPECT_EQ(0, gpr_time_cmp(gpr_inf_future, from_time_point_max));
}
} // namespace
} // namespace grpc
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

@ -351,6 +351,11 @@
"language": "c++",
"name": "credentials_test"
},
{
"flaky": false,
"language": "c++",
"name": "cxx_time_test"
},
{
"flaky": false,
"language": "c++",

Loading…
Cancel
Save