blob: 803bd4621013fcf0da2d16c77f8a4c00ad578615 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080034#include <grpc/support/time.h>
yang-g9e2f90c2015-08-21 15:35:03 -070035#include <grpc++/support/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036#include <gtest/gtest.h>
37
38using std::chrono::duration_cast;
39using std::chrono::microseconds;
40using std::chrono::system_clock;
41
42namespace grpc {
43namespace {
44
45class TimeTest : public ::testing::Test {};
46
47TEST_F(TimeTest, AbsolutePointTest) {
murgatroid99309830f2016-02-05 11:30:00 -080048 int64_t us = 10000000L;
Craig Tiller677c50c2015-07-13 10:49:06 -070049 gpr_timespec ts = gpr_time_from_micros(us, GPR_TIMESPAN);
Craig Tiller354398f2015-07-13 09:16:03 -070050 ts.clock_type = GPR_CLOCK_REALTIME;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051 system_clock::time_point tp{microseconds(us)};
yangg87da1b92014-12-11 15:57:37 -080052 system_clock::time_point tp_converted = Timespec2Timepoint(ts);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053 gpr_timespec ts_converted;
yangg87da1b92014-12-11 15:57:37 -080054 Timepoint2Timespec(tp_converted, &ts_converted);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055 EXPECT_TRUE(ts.tv_sec == ts_converted.tv_sec);
56 EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec);
yangg87da1b92014-12-11 15:57:37 -080057 system_clock::time_point tp_converted_2 = Timespec2Timepoint(ts_converted);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058 EXPECT_TRUE(tp == tp_converted);
59 EXPECT_TRUE(tp == tp_converted_2);
60}
61
Yang Gaocdb2a6e2015-03-20 23:55:04 -070062// gpr_inf_future is treated specially and mapped to/from time_point::max()
yangged5e7e02015-01-06 10:16:15 -080063TEST_F(TimeTest, InfFuture) {
64 EXPECT_EQ(system_clock::time_point::max(),
Craig Tiller354398f2015-07-13 09:16:03 -070065 Timespec2Timepoint(gpr_inf_future(GPR_CLOCK_REALTIME)));
Yang Gaocdb2a6e2015-03-20 23:55:04 -070066 gpr_timespec from_time_point_max;
67 Timepoint2Timespec(system_clock::time_point::max(), &from_time_point_max);
Craig Tiller354398f2015-07-13 09:16:03 -070068 EXPECT_EQ(
69 0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max));
Yang Gaocdb2a6e2015-03-20 23:55:04 -070070 // This will cause an overflow
71 Timepoint2Timespec(
72 std::chrono::time_point<system_clock, std::chrono::seconds>::max(),
73 &from_time_point_max);
Craig Tiller354398f2015-07-13 09:16:03 -070074 EXPECT_EQ(
75 0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max));
yangged5e7e02015-01-06 10:16:15 -080076}
77
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078} // namespace
Craig Tiller190d3602015-02-18 09:23:38 -080079} // namespace grpc
Yang Gaocdb2a6e2015-03-20 23:55:04 -070080
81int main(int argc, char** argv) {
82 ::testing::InitGoogleTest(&argc, argv);
83 return RUN_ALL_TESTS();
84}