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 #include "test/core/test_util/test_config.h"
24
25 using std::chrono::microseconds;
26 using std::chrono::system_clock;
27
28 namespace grpc {
29 namespace {
30
31 class TimeTest : public ::testing::Test {};
32
TEST_F(TimeTest,AbsolutePointTest)33 TEST_F(TimeTest, AbsolutePointTest) {
34 int64_t us = 10000000L;
35 gpr_timespec ts = gpr_time_from_micros(us, GPR_TIMESPAN);
36 ts.clock_type = GPR_CLOCK_REALTIME;
37 system_clock::time_point tp{microseconds(us)};
38 system_clock::time_point tp_converted = Timespec2Timepoint(ts);
39 gpr_timespec ts_converted;
40 Timepoint2Timespec(tp_converted, &ts_converted);
41 EXPECT_TRUE(ts.tv_sec == ts_converted.tv_sec);
42 EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec);
43 system_clock::time_point tp_converted_2 = Timespec2Timepoint(ts_converted);
44 EXPECT_TRUE(tp == tp_converted);
45 EXPECT_TRUE(tp == tp_converted_2);
46 }
47
48 // gpr_inf_future is treated specially and mapped to/from time_point::max()
TEST_F(TimeTest,InfFuture)49 TEST_F(TimeTest, InfFuture) {
50 EXPECT_EQ(system_clock::time_point::max(),
51 Timespec2Timepoint(gpr_inf_future(GPR_CLOCK_REALTIME)));
52 gpr_timespec from_time_point_max;
53 Timepoint2Timespec(system_clock::time_point::max(), &from_time_point_max);
54 EXPECT_EQ(
55 0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max));
56 // This will cause an overflow
57 Timepoint2Timespec(
58 std::chrono::time_point<system_clock, std::chrono::seconds>::max(),
59 &from_time_point_max);
60 EXPECT_EQ(
61 0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max));
62 }
63
64 } // namespace
65 } // namespace grpc
66
main(int argc,char ** argv)67 int main(int argc, char** argv) {
68 grpc::testing::TestEnvironment env(&argc, argv);
69 ::testing::InitGoogleTest(&argc, argv);
70 return RUN_ALL_TESTS();
71 }
72