Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 34 | #include <grpc/support/time.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 35 | #include <grpc++/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | class TimeTest : public ::testing::Test {}; |
| 46 | |
| 47 | TEST_F(TimeTest, AbsolutePointTest) { |
murgatroid99 | 309830f | 2016-02-05 11:30:00 -0800 | [diff] [blame^] | 48 | int64_t us = 10000000L; |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 49 | gpr_timespec ts = gpr_time_from_micros(us, GPR_TIMESPAN); |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 50 | ts.clock_type = GPR_CLOCK_REALTIME; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | system_clock::time_point tp{microseconds(us)}; |
yangg | 87da1b9 | 2014-12-11 15:57:37 -0800 | [diff] [blame] | 52 | system_clock::time_point tp_converted = Timespec2Timepoint(ts); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | gpr_timespec ts_converted; |
yangg | 87da1b9 | 2014-12-11 15:57:37 -0800 | [diff] [blame] | 54 | Timepoint2Timespec(tp_converted, &ts_converted); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | EXPECT_TRUE(ts.tv_sec == ts_converted.tv_sec); |
| 56 | EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec); |
yangg | 87da1b9 | 2014-12-11 15:57:37 -0800 | [diff] [blame] | 57 | system_clock::time_point tp_converted_2 = Timespec2Timepoint(ts_converted); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | EXPECT_TRUE(tp == tp_converted); |
| 59 | EXPECT_TRUE(tp == tp_converted_2); |
| 60 | } |
| 61 | |
Yang Gao | cdb2a6e | 2015-03-20 23:55:04 -0700 | [diff] [blame] | 62 | // gpr_inf_future is treated specially and mapped to/from time_point::max() |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 63 | TEST_F(TimeTest, InfFuture) { |
| 64 | EXPECT_EQ(system_clock::time_point::max(), |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 65 | Timespec2Timepoint(gpr_inf_future(GPR_CLOCK_REALTIME))); |
Yang Gao | cdb2a6e | 2015-03-20 23:55:04 -0700 | [diff] [blame] | 66 | gpr_timespec from_time_point_max; |
| 67 | Timepoint2Timespec(system_clock::time_point::max(), &from_time_point_max); |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 68 | EXPECT_EQ( |
| 69 | 0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max)); |
Yang Gao | cdb2a6e | 2015-03-20 23:55:04 -0700 | [diff] [blame] | 70 | // This will cause an overflow |
| 71 | Timepoint2Timespec( |
| 72 | std::chrono::time_point<system_clock, std::chrono::seconds>::max(), |
| 73 | &from_time_point_max); |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 74 | EXPECT_EQ( |
| 75 | 0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max)); |
yangg | ed5e7e0 | 2015-01-06 10:16:15 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | } // namespace |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 79 | } // namespace grpc |
Yang Gao | cdb2a6e | 2015-03-20 23:55:04 -0700 | [diff] [blame] | 80 | |
| 81 | int main(int argc, char** argv) { |
| 82 | ::testing::InitGoogleTest(&argc, argv); |
| 83 | return RUN_ALL_TESTS(); |
| 84 | } |