• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ext/filters/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   ServerRetryThrottleMap::Init();
101   const char kServerName[] = "server_name";
102   // Create old throttle data.
103   // Max token count is 4, so threshold for retrying is 2.
104   // Token count starts at 4.
105   // Each failure decrements by 1.  Each success increments by 1.
106   auto old_throttle_data =
107       ServerRetryThrottleMap::GetDataForServer(kServerName, 4000, 1000);
108   // Failure: token_count=3.  Above threshold.
109   EXPECT_TRUE(old_throttle_data->RecordFailure());
110   // Create new throttle data.
111   // Max token count is 10, so threshold for retrying is 5.
112   // Token count starts at 7.5 (ratio inherited from old_throttle_data).
113   // Each failure decrements by 1.  Each success increments by 3.
114   auto throttle_data =
115       ServerRetryThrottleMap::GetDataForServer(kServerName, 10000, 3000);
116   // Failure via old_throttle_data: token_count=6.5.
117   EXPECT_TRUE(old_throttle_data->RecordFailure());
118   // Failure: token_count=5.5.
119   EXPECT_TRUE(old_throttle_data->RecordFailure());
120   // Failure via old_throttle_data: token_count=4.5.  Below threshold.
121   EXPECT_FALSE(old_throttle_data->RecordFailure());
122   // Failure: token_count=3.5.  Below threshold.
123   EXPECT_FALSE(throttle_data->RecordFailure());
124   // Success: token_count=6.5.
125   throttle_data->RecordSuccess();
126   // Failure via old_throttle_data: token_count=5.5.  Above threshold.
127   EXPECT_TRUE(old_throttle_data->RecordFailure());
128   // Failure: token_count=4.5.  Below threshold.
129   EXPECT_FALSE(throttle_data->RecordFailure());
130   // Clean up.
131   ServerRetryThrottleMap::Shutdown();
132 }
133 
134 }  // namespace
135 }  // namespace internal
136 }  // namespace grpc_core
137 
main(int argc,char ** argv)138 int main(int argc, char** argv) {
139   grpc_test_init(argc, argv);
140   ::testing::InitGoogleTest(&argc, argv);
141   return RUN_ALL_TESTS();
142 }
143