test/cpp/util/time_test.cc
Go to the documentation of this file.
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 
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;
38  system_clock::time_point tp{microseconds(us)};
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) {
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
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) {
70  ::testing::InitGoogleTest(&argc, argv);
71  return RUN_ALL_TESTS();
72 }
gpr_timespec::tv_nsec
int32_t tv_nsec
Definition: gpr_types.h:52
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
gpr_timespec::tv_sec
int64_t tv_sec
Definition: gpr_types.h:51
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
absl::time_internal::cctz::time_point
std::chrono::time_point< std::chrono::system_clock, D > time_point
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:39
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
time.h
grpc::EXPECT_TRUE
EXPECT_TRUE(status.ok())
time.h
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
gpr_time_cmp
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:30
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc::Timespec2Timepoint
std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t)
Definition: time_cc.cc:68
gpr_timespec::clock_type
gpr_clock_type clock_type
Definition: gpr_types.h:55
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
gpr_time_from_micros
GPRAPI gpr_timespec gpr_time_from_micros(int64_t us, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:115
L
lua_State * L
Definition: upb/upb/bindings/lua/main.c:35
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::Timepoint2Timespec
void Timepoint2Timespec(const std::chrono::system_clock::time_point &from, gpr_timespec *to)
grpc::EXPECT_EQ
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code())
main
int main(int argc, char **argv)
Definition: test/cpp/util/time_test.cc:68
gpr_timespec
Definition: gpr_types.h:50
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
TEST_F
#define TEST_F(test_fixture, test_name)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2367


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:36