retry_throttle_test.cc 5.6 KB

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