OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 class TimeTest : public ::testing::Test {}; |
| 46 |
| 47 TEST_F(TimeTest, AbsolutePointTest) { |
| 48 int64_t us = 10000000L; |
| 49 gpr_timespec ts = gpr_time_from_micros(us, GPR_TIMESPAN); |
| 50 ts.clock_type = GPR_CLOCK_REALTIME; |
| 51 system_clock::time_point tp{microseconds(us)}; |
| 52 system_clock::time_point tp_converted = Timespec2Timepoint(ts); |
| 53 gpr_timespec ts_converted; |
| 54 Timepoint2Timespec(tp_converted, &ts_converted); |
| 55 EXPECT_TRUE(ts.tv_sec == ts_converted.tv_sec); |
| 56 EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec); |
| 57 system_clock::time_point tp_converted_2 = Timespec2Timepoint(ts_converted); |
| 58 EXPECT_TRUE(tp == tp_converted); |
| 59 EXPECT_TRUE(tp == tp_converted_2); |
| 60 } |
| 61 |
| 62 // gpr_inf_future is treated specially and mapped to/from time_point::max() |
| 63 TEST_F(TimeTest, InfFuture) { |
| 64 EXPECT_EQ(system_clock::time_point::max(), |
| 65 Timespec2Timepoint(gpr_inf_future(GPR_CLOCK_REALTIME))); |
| 66 gpr_timespec from_time_point_max; |
| 67 Timepoint2Timespec(system_clock::time_point::max(), &from_time_point_max); |
| 68 EXPECT_EQ( |
| 69 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 &from_time_point_max); |
| 74 EXPECT_EQ( |
| 75 0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), from_time_point_max)); |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 } // namespace grpc |
| 80 |
| 81 int main(int argc, char** argv) { |
| 82 ::testing::InitGoogleTest(&argc, argv); |
| 83 return RUN_ALL_TESTS(); |
| 84 } |
OLD | NEW |