| 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 "src/core/lib/transport/timeout_encoding.h" |
| 20 |
|
| 21 |
#include <string> |
| 22 |
|
| 23 |
#include "absl/strings/str_format.h" |
| 24 |
#include "absl/strings/string_view.h" |
| 25 |
#include "gtest/gtest.h" |
| 26 |
|
| 27 |
#include "src/core/lib/gpr/useful.h" |
| 28 |
#include "src/core/lib/gprpp/time.h" |
| 29 |
|
| 30 |
namespace grpc_core { |
| 31 |
namespace { |
| 32 |
|
| 33 |
void assert_encodes_as(Duration ts, const char* s) { |
| 34 |
EXPECT_EQ(absl::string_view(s), |
| 35 |
Timeout::FromDuration(ts).Encode().as_string_view()) |
| 36 |
<< " ts=" << ts.ToString(); |
| 37 |
} |
| 38 |
|
| 39 |
TEST(TimeoutTest, Encoding) { |
| 40 |
assert_encodes_as(Duration::Milliseconds(-1), "1n"); |
| 41 |
assert_encodes_as(Duration::Milliseconds(-10), "1n"); |
| 42 |
assert_encodes_as(Duration::Milliseconds(1), "1m"); |
| 43 |
assert_encodes_as(Duration::Milliseconds(10), "10m"); |
| 44 |
assert_encodes_as(Duration::Milliseconds(100), "100m"); |
| 45 |
assert_encodes_as(Duration::Milliseconds(890), "890m"); |
| 46 |
assert_encodes_as(Duration::Milliseconds(900), "900m"); |
| 47 |
assert_encodes_as(Duration::Milliseconds(901), "901m"); |
| 48 |
assert_encodes_as(Duration::Milliseconds(1000), "1S"); |
| 49 |
assert_encodes_as(Duration::Milliseconds(2000), "2S"); |
| 50 |
assert_encodes_as(Duration::Milliseconds(2500), "2500m"); |
| 51 |
assert_encodes_as(Duration::Milliseconds(59900), "59900m"); |
| 52 |
assert_encodes_as(Duration::Seconds(50), "50S"); |
| 53 |
assert_encodes_as(Duration::Seconds(59), "59S"); |
| 54 |
assert_encodes_as(Duration::Seconds(60), "1M"); |
| 55 |
assert_encodes_as(Duration::Seconds(80), "80S"); |
| 56 |
assert_encodes_as(Duration::Seconds(90), "90S"); |
| 57 |
assert_encodes_as(Duration::Seconds(120), "2M"); |
| 58 |
assert_encodes_as(Duration::Minutes(20), "20M"); |
| 59 |
assert_encodes_as(Duration::Hours(1), "1H"); |
| 60 |
assert_encodes_as(Duration::Hours(10), "10H"); |
| 61 |
assert_encodes_as(Duration::Hours(1) - Duration::Milliseconds(100), "1H"); |
| 62 |
assert_encodes_as(Duration::Hours(100), "100H"); |
| 63 |
assert_encodes_as(Duration::Hours(100000), "27000H"); |
| 64 |
} |
| 65 |
|
| 66 |
void assert_decodes_as(const char* buffer, Duration expected) { |
| 67 |
EXPECT_EQ(expected, ParseTimeout(Slice::FromCopiedString(buffer))); |
| 68 |
} |
| 69 |
|
| 70 |
void decode_suite(char ext, Duration (*answer)(int64_t x)) { |
| 71 |
long test_vals[] = {1, 12, 123, 1234, 12345, 123456, |
| 72 |
1234567, 12345678, 123456789, 98765432, 9876543, 987654, |
| 73 |
98765, 9876, 987, 98, 9}; |
| 74 |
for (unsigned i = 0; i < GPR_ARRAY_SIZE(test_vals); i++) { |
| 75 |
std::string input = absl::StrFormat("%ld%c", test_vals[i], ext); |
| 76 |
assert_decodes_as(input.c_str(), answer(test_vals[i])); |
| 77 |
|
| 78 |
input = absl::StrFormat(" %ld%c", test_vals[i], ext); |
| 79 |
assert_decodes_as(input.c_str(), answer(test_vals[i])); |
| 80 |
|
| 81 |
input = absl::StrFormat("%ld %c", test_vals[i], ext); |
| 82 |
assert_decodes_as(input.c_str(), answer(test_vals[i])); |
| 83 |
|
| 84 |
input = absl::StrFormat("%ld %c ", test_vals[i], ext); |
| 85 |
assert_decodes_as(input.c_str(), answer(test_vals[i])); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
TEST(TimeoutTest, DecodingSucceeds) { |
| 90 |
decode_suite('n', Duration::NanosecondsRoundUp); |
| 91 |
decode_suite('u', Duration::MicrosecondsRoundUp); |
| 92 |
decode_suite('m', Duration::Milliseconds); |
| 93 |
decode_suite('S', Duration::Seconds); |
| 94 |
decode_suite('M', Duration::Minutes); |
| 95 |
decode_suite('H', Duration::Hours); |
| 96 |
assert_decodes_as("1000000000S", Duration::Seconds(1000 * 1000 * 1000)); |
| 97 |
assert_decodes_as("1000000000000000000000u", Duration::Infinity()); |
| 98 |
assert_decodes_as("1000000001S", Duration::Infinity()); |
| 99 |
assert_decodes_as("2000000001S", Duration::Infinity()); |
| 100 |
assert_decodes_as("9999999999S", Duration::Infinity()); |
| 101 |
} |
| 102 |
|
| 103 |
void assert_decoding_fails(const char* s) { |
| 104 |
EXPECT_EQ(absl::nullopt, ParseTimeout(Slice::FromCopiedString(s))) |
| 105 |
<< " s=" << s; |
| 106 |
} |
| 107 |
|
| 108 |
TEST(TimeoutTest, DecodingFails) { |
| 109 |
assert_decoding_fails(""); |
| 110 |
assert_decoding_fails(" "); |
| 111 |
assert_decoding_fails("x"); |
| 112 |
assert_decoding_fails("1"); |
| 113 |
assert_decoding_fails("1x"); |
| 114 |
assert_decoding_fails("1ux"); |
| 115 |
assert_decoding_fails("!"); |
| 116 |
assert_decoding_fails("n1"); |
| 117 |
assert_decoding_fails("-1u"); |
| 118 |
} |
| 119 |
|
| 120 |
} // namespace |
| 121 |
} // namespace grpc_core |
| 122 |
|
| 123 |
int main(int argc, char** argv) { |
| 124 |
::testing::InitGoogleTest(&argc, argv); |
| 125 |
return RUN_ALL_TESTS(); |
| 126 |
} |