| 1 |
// |
| 2 |
// Copyright 2021 the gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include "src/core/lib/gprpp/time_util.h" |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
#include "absl/time/time.h" |
| 23 |
#include "gtest/gtest.h" |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/support/time.h> |
| 27 |
|
| 28 |
TEST(TimeUtilTest, ToGprTimeSpecFromAbslDurationWithRegularValues) { |
| 29 |
std::vector<int> times = {-10, -1, 0, 1, 10}; |
| 30 |
for (int t : times) { |
| 31 |
EXPECT_EQ(0, gpr_time_cmp(gpr_time_from_nanos(t, GPR_TIMESPAN), |
| 32 |
grpc_core::ToGprTimeSpec(absl::Nanoseconds(t)))); |
| 33 |
EXPECT_EQ(0, gpr_time_cmp(gpr_time_from_micros(t, GPR_TIMESPAN), |
| 34 |
grpc_core::ToGprTimeSpec(absl::Microseconds(t)))); |
| 35 |
EXPECT_EQ(0, gpr_time_cmp(gpr_time_from_millis(t, GPR_TIMESPAN), |
| 36 |
grpc_core::ToGprTimeSpec(absl::Milliseconds(t)))); |
| 37 |
EXPECT_EQ(0, gpr_time_cmp(gpr_time_from_seconds(t, GPR_TIMESPAN), |
| 38 |
grpc_core::ToGprTimeSpec(absl::Seconds(t)))); |
| 39 |
EXPECT_EQ(0, gpr_time_cmp(gpr_time_from_minutes(t, GPR_TIMESPAN), |
| 40 |
grpc_core::ToGprTimeSpec(absl::Minutes(t)))); |
| 41 |
EXPECT_EQ(0, gpr_time_cmp(gpr_time_from_hours(t, GPR_TIMESPAN), |
| 42 |
grpc_core::ToGprTimeSpec(absl::Hours(t)))); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
TEST(TimeUtilTest, ToGprTimeSpecFromAbslDurationWithInfinites) { |
| 47 |
EXPECT_EQ(0, |
| 48 |
gpr_time_cmp(gpr_inf_past(GPR_TIMESPAN), |
| 49 |
grpc_core::ToGprTimeSpec(-absl::InfiniteDuration()))); |
| 50 |
EXPECT_EQ(0, gpr_time_cmp(gpr_time_0(GPR_TIMESPAN), |
| 51 |
grpc_core::ToGprTimeSpec(absl::ZeroDuration()))); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(TimeUtilTest, ToGprTimeSpecFromAbslTimeWithRegularValues) { |
| 55 |
std::vector<int> times = {0, 10, 100000000}; |
| 56 |
for (int t : times) { |
| 57 |
EXPECT_EQ(0, |
| 58 |
gpr_time_cmp(gpr_time_from_nanos(t, GPR_CLOCK_REALTIME), |
| 59 |
grpc_core::ToGprTimeSpec(absl::FromUnixNanos(t)))); |
| 60 |
EXPECT_EQ(0, |
| 61 |
gpr_time_cmp(gpr_time_from_micros(t, GPR_CLOCK_REALTIME), |
| 62 |
grpc_core::ToGprTimeSpec(absl::FromUnixMicros(t)))); |
| 63 |
EXPECT_EQ(0, |
| 64 |
gpr_time_cmp(gpr_time_from_millis(t, GPR_CLOCK_REALTIME), |
| 65 |
grpc_core::ToGprTimeSpec(absl::FromUnixMillis(t)))); |
| 66 |
EXPECT_EQ(0, |
| 67 |
gpr_time_cmp(gpr_time_from_seconds(t, GPR_CLOCK_REALTIME), |
| 68 |
grpc_core::ToGprTimeSpec(absl::FromUnixSeconds(t)))); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
TEST(TimeUtilTest, ToGprTimeSpecFromAbslTimeWithInfinites) { |
| 73 |
EXPECT_EQ(0, gpr_time_cmp(gpr_inf_future(GPR_CLOCK_REALTIME), |
| 74 |
grpc_core::ToGprTimeSpec(absl::InfiniteFuture()))); |
| 75 |
EXPECT_EQ(0, gpr_time_cmp(gpr_inf_past(GPR_CLOCK_REALTIME), |
| 76 |
grpc_core::ToGprTimeSpec(absl::InfinitePast()))); |
| 77 |
} |
| 78 |
|
| 79 |
TEST(TimeUtilTest, ToAbslDurationWithRegularValues) { |
| 80 |
std::vector<int> times = {-10, -1, 0, 1, 10}; |
| 81 |
for (int t : times) { |
| 82 |
EXPECT_EQ(absl::Nanoseconds(t), |
| 83 |
grpc_core::ToAbslDuration(gpr_time_from_nanos(t, GPR_TIMESPAN))); |
| 84 |
EXPECT_EQ(absl::Microseconds(t), |
| 85 |
grpc_core::ToAbslDuration(gpr_time_from_micros(t, GPR_TIMESPAN))); |
| 86 |
EXPECT_EQ(absl::Milliseconds(t), |
| 87 |
grpc_core::ToAbslDuration(gpr_time_from_millis(t, GPR_TIMESPAN))); |
| 88 |
EXPECT_EQ(absl::Seconds(t), grpc_core::ToAbslDuration( |
| 89 |
gpr_time_from_seconds(t, GPR_TIMESPAN))); |
| 90 |
EXPECT_EQ(absl::Minutes(t), grpc_core::ToAbslDuration( |
| 91 |
gpr_time_from_minutes(t, GPR_TIMESPAN))); |
| 92 |
EXPECT_EQ(absl::Hours(t), |
| 93 |
grpc_core::ToAbslDuration(gpr_time_from_hours(t, GPR_TIMESPAN))); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
TEST(TimeUtilTest, ToAbslDurationWithInfinites) { |
| 98 |
EXPECT_EQ(absl::InfiniteDuration(), |
| 99 |
grpc_core::ToAbslDuration(gpr_inf_future(GPR_TIMESPAN))); |
| 100 |
EXPECT_EQ(-absl::InfiniteDuration(), |
| 101 |
grpc_core::ToAbslDuration(gpr_inf_past(GPR_TIMESPAN))); |
| 102 |
} |
| 103 |
|
| 104 |
TEST(TimeUtilTest, ToAbslTimeWithRegularValues) { |
| 105 |
std::vector<int> times = {0, 10, 100000000}; |
| 106 |
for (int t : times) { |
| 107 |
EXPECT_EQ(absl::FromUnixNanos(t), grpc_core::ToAbslTime(gpr_time_from_nanos( |
| 108 |
t, GPR_CLOCK_REALTIME))); |
| 109 |
EXPECT_EQ( |
| 110 |
absl::FromUnixMicros(t), |
| 111 |
grpc_core::ToAbslTime(gpr_time_from_micros(t, GPR_CLOCK_REALTIME))); |
| 112 |
EXPECT_EQ( |
| 113 |
absl::FromUnixMillis(t), |
| 114 |
grpc_core::ToAbslTime(gpr_time_from_millis(t, GPR_CLOCK_REALTIME))); |
| 115 |
EXPECT_EQ( |
| 116 |
absl::FromUnixSeconds(t), |
| 117 |
grpc_core::ToAbslTime(gpr_time_from_seconds(t, GPR_CLOCK_REALTIME))); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
TEST(TimeUtilTest, ToAbslTimeWithInfinites) { |
| 122 |
EXPECT_EQ(absl::InfiniteFuture(), |
| 123 |
grpc_core::ToAbslTime(gpr_inf_future(GPR_CLOCK_REALTIME))); |
| 124 |
EXPECT_EQ(absl::InfinitePast(), |
| 125 |
grpc_core::ToAbslTime(gpr_inf_past(GPR_CLOCK_REALTIME))); |
| 126 |
EXPECT_EQ(absl::UnixEpoch(), |
| 127 |
grpc_core::ToAbslTime(gpr_time_0(GPR_CLOCK_REALTIME))); |
| 128 |
} |
| 129 |
|
| 130 |
int main(int argc, char** argv) { |
| 131 |
::testing::InitGoogleTest(&argc, argv); |
| 132 |
int ret = RUN_ALL_TESTS(); |
| 133 |
return ret; |
| 134 |
} |