Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include <grpc/support/time.h>
35 : #include <grpc++/support/time.h>
36 : #include <gtest/gtest.h>
37 :
38 : using std::chrono::duration_cast;
39 : using std::chrono::microseconds;
40 : using std::chrono::system_clock;
41 :
42 : namespace grpc {
43 : namespace {
44 :
45 4 : class TimeTest : public ::testing::Test {};
46 :
47 5 : TEST_F(TimeTest, AbsolutePointTest) {
48 1 : long us = 10000000L;
49 1 : gpr_timespec ts = gpr_time_from_micros(us, GPR_TIMESPAN);
50 1 : ts.clock_type = GPR_CLOCK_REALTIME;
51 1 : system_clock::time_point tp{microseconds(us)};
52 1 : system_clock::time_point tp_converted = Timespec2Timepoint(ts);
53 : gpr_timespec ts_converted;
54 1 : Timepoint2Timespec(tp_converted, &ts_converted);
55 1 : EXPECT_TRUE(ts.tv_sec == ts_converted.tv_sec);
56 1 : EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec);
57 1 : system_clock::time_point tp_converted_2 = Timespec2Timepoint(ts_converted);
58 1 : EXPECT_TRUE(tp == tp_converted);
59 1 : EXPECT_TRUE(tp == tp_converted_2);
60 1 : }
61 :
62 : // gpr_inf_future is treated specially and mapped to/from time_point::max()
63 5 : TEST_F(TimeTest, InfFuture) {
64 1 : EXPECT_EQ(system_clock::time_point::max(),
65 1 : Timespec2Timepoint(gpr_inf_future(GPR_CLOCK_REALTIME)));
66 : gpr_timespec from_time_point_max;
67 1 : Timepoint2Timespec(system_clock::time_point::max(), &from_time_point_max);
68 1 : EXPECT_EQ(
69 1 : 0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max));
70 : // This will cause an overflow
71 : Timepoint2Timespec(
72 : std::chrono::time_point<system_clock, std::chrono::seconds>::max(),
73 1 : &from_time_point_max);
74 1 : EXPECT_EQ(
75 1 : 0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max));
76 1 : }
77 :
78 : } // namespace
79 : } // namespace grpc
80 :
81 1 : int main(int argc, char** argv) {
82 1 : ::testing::InitGoogleTest(&argc, argv);
83 1 : return RUN_ALL_TESTS();
84 3 : }
|