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