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