• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/support/time.h>
20 #include <grpcpp/support/time.h>
21 #include <gtest/gtest.h>
22 
23 using std::chrono::duration_cast;
24 using std::chrono::microseconds;
25 using std::chrono::system_clock;
26 
27 namespace grpc {
28 namespace {
29 
30 class TimeTest : public ::testing::Test {};
31 
TEST_F(TimeTest,AbsolutePointTest)32 TEST_F(TimeTest, AbsolutePointTest) {
33   int64_t us = 10000000L;
34   gpr_timespec ts = gpr_time_from_micros(us, GPR_TIMESPAN);
35   ts.clock_type = GPR_CLOCK_REALTIME;
36   system_clock::time_point tp{microseconds(us)};
37   system_clock::time_point tp_converted = Timespec2Timepoint(ts);
38   gpr_timespec ts_converted;
39   Timepoint2Timespec(tp_converted, &ts_converted);
40   EXPECT_TRUE(ts.tv_sec == ts_converted.tv_sec);
41   EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec);
42   system_clock::time_point tp_converted_2 = Timespec2Timepoint(ts_converted);
43   EXPECT_TRUE(tp == tp_converted);
44   EXPECT_TRUE(tp == tp_converted_2);
45 }
46 
47 // gpr_inf_future is treated specially and mapped to/from time_point::max()
TEST_F(TimeTest,InfFuture)48 TEST_F(TimeTest, InfFuture) {
49   EXPECT_EQ(system_clock::time_point::max(),
50             Timespec2Timepoint(gpr_inf_future(GPR_CLOCK_REALTIME)));
51   gpr_timespec from_time_point_max;
52   Timepoint2Timespec(system_clock::time_point::max(), &from_time_point_max);
53   EXPECT_EQ(
54       0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max));
55   // This will cause an overflow
56   Timepoint2Timespec(
57       std::chrono::time_point<system_clock, std::chrono::seconds>::max(),
58       &from_time_point_max);
59   EXPECT_EQ(
60       0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max));
61 }
62 
63 }  // namespace
64 }  // namespace grpc
65 
main(int argc,char ** argv)66 int main(int argc, char** argv) {
67   ::testing::InitGoogleTest(&argc, argv);
68   return RUN_ALL_TESTS();
69 }
70