1 //
2 //
3 // Copyright 2018 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/client_channel/retry_throttle.h"
20
21 #include "gtest/gtest.h"
22
23 #include "test/core/util/test_config.h"
24
25 namespace grpc_core {
26 namespace internal {
27 namespace {
28
TEST(ServerRetryThrottleData,Basic)29 TEST(ServerRetryThrottleData, Basic) {
30 // Max token count is 4, so threshold for retrying is 2.
31 // Token count starts at 4.
32 // Each failure decrements by 1. Each success increments by 1.6.
33 auto throttle_data =
34 MakeRefCounted<ServerRetryThrottleData>(4000, 1600, nullptr);
35 // Failure: token_count=3. Above threshold.
36 EXPECT_TRUE(throttle_data->RecordFailure());
37 // Success: token_count=4. Not incremented beyond max.
38 throttle_data->RecordSuccess();
39 // Failure: token_count=3. Above threshold.
40 EXPECT_TRUE(throttle_data->RecordFailure());
41 // Failure: token_count=2. At threshold, so no retries.
42 EXPECT_FALSE(throttle_data->RecordFailure());
43 // Failure: token_count=1. Below threshold, so no retries.
44 EXPECT_FALSE(throttle_data->RecordFailure());
45 // Failure: token_count=0. Below threshold, so no retries.
46 EXPECT_FALSE(throttle_data->RecordFailure());
47 // Failure: token_count=0. Below threshold, so no retries. Not
48 // decremented below min.
49 EXPECT_FALSE(throttle_data->RecordFailure());
50 // Success: token_count=1.6.
51 throttle_data->RecordSuccess();
52 // Success: token_count=3.2.
53 throttle_data->RecordSuccess();
54 // Failure: token_count=2.2. Above threshold.
55 EXPECT_TRUE(throttle_data->RecordFailure());
56 // Failure: token_count=1.2. Below threshold, so no retries.
57 EXPECT_FALSE(throttle_data->RecordFailure());
58 // Success: token_count=2.8.
59 throttle_data->RecordSuccess();
60 // Failure: token_count=1.8. Below threshold, so no retries.
61 EXPECT_FALSE(throttle_data->RecordFailure());
62 // Success: token_count=3.4.
63 throttle_data->RecordSuccess();
64 // Failure: token_count=2.4. Above threshold.
65 EXPECT_TRUE(throttle_data->RecordFailure());
66 }
67
TEST(ServerRetryThrottleData,Replacement)68 TEST(ServerRetryThrottleData, Replacement) {
69 // Create old throttle data.
70 // Max token count is 4, so threshold for retrying is 2.
71 // Token count starts at 4.
72 // Each failure decrements by 1. Each success increments by 1.
73 auto old_throttle_data =
74 MakeRefCounted<ServerRetryThrottleData>(4000, 1000, nullptr);
75 // Failure: token_count=3. Above threshold.
76 EXPECT_TRUE(old_throttle_data->RecordFailure());
77 // Create new throttle data.
78 // Max token count is 10, so threshold for retrying is 5.
79 // Token count starts at 7.5 (ratio inherited from old_throttle_data).
80 // Each failure decrements by 1. Each success increments by 3.
81 auto throttle_data = MakeRefCounted<ServerRetryThrottleData>(
82 10000, 3000, old_throttle_data.get());
83 // Failure via old_throttle_data: token_count=6.5.
84 EXPECT_TRUE(old_throttle_data->RecordFailure());
85 // Failure: token_count=5.5.
86 EXPECT_TRUE(old_throttle_data->RecordFailure());
87 // Failure via old_throttle_data: token_count=4.5. Below threshold.
88 EXPECT_FALSE(old_throttle_data->RecordFailure());
89 // Failure: token_count=3.5. Below threshold.
90 EXPECT_FALSE(throttle_data->RecordFailure());
91 // Success: token_count=6.5.
92 throttle_data->RecordSuccess();
93 // Failure via old_throttle_data: token_count=5.5. Above threshold.
94 EXPECT_TRUE(old_throttle_data->RecordFailure());
95 // Failure: token_count=4.5. Below threshold.
96 EXPECT_FALSE(throttle_data->RecordFailure());
97 }
98
TEST(ServerRetryThrottleMap,Replacement)99 TEST(ServerRetryThrottleMap, Replacement) {
100 const std::string kServerName = "server_name";
101 // Create old throttle data.
102 // Max token count is 4, so threshold for retrying is 2.
103 // Token count starts at 4.
104 // Each failure decrements by 1. Each success increments by 1.
105 auto old_throttle_data =
106 ServerRetryThrottleMap::Get()->GetDataForServer(kServerName, 4000, 1000);
107 // Failure: token_count=3. Above threshold.
108 EXPECT_TRUE(old_throttle_data->RecordFailure());
109 // Create new throttle data.
110 // Max token count is 10, so threshold for retrying is 5.
111 // Token count starts at 7.5 (ratio inherited from old_throttle_data).
112 // Each failure decrements by 1. Each success increments by 3.
113 auto throttle_data =
114 ServerRetryThrottleMap::Get()->GetDataForServer(kServerName, 10000, 3000);
115 // Failure via old_throttle_data: token_count=6.5.
116 EXPECT_TRUE(old_throttle_data->RecordFailure());
117 // Failure: token_count=5.5.
118 EXPECT_TRUE(old_throttle_data->RecordFailure());
119 // Failure via old_throttle_data: token_count=4.5. Below threshold.
120 EXPECT_FALSE(old_throttle_data->RecordFailure());
121 // Failure: token_count=3.5. Below threshold.
122 EXPECT_FALSE(throttle_data->RecordFailure());
123 // Success: token_count=6.5.
124 throttle_data->RecordSuccess();
125 // Failure via old_throttle_data: token_count=5.5. Above threshold.
126 EXPECT_TRUE(old_throttle_data->RecordFailure());
127 // Failure: token_count=4.5. Below threshold.
128 EXPECT_FALSE(throttle_data->RecordFailure());
129 }
130
131 } // namespace
132 } // namespace internal
133 } // namespace grpc_core
134
main(int argc,char ** argv)135 int main(int argc, char** argv) {
136 grpc::testing::TestEnvironment env(&argc, argv);
137 ::testing::InitGoogleTest(&argc, argv);
138 return RUN_ALL_TESTS();
139 }
140