backoff_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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 
20 
21 #include <algorithm>
22 
23 #include <gtest/gtest.h>
24 
25 #include <grpc/grpc.h>
26 #include <grpc/support/log.h>
27 
31 
32 namespace grpc {
33 namespace testing {
34 namespace {
35 
36 using grpc_core::BackOff;
37 
38 TEST(BackOffTest, ConstantBackOff) {
39  const auto initial_backoff = grpc_core::Duration::Milliseconds(200);
40  const double multiplier = 1.0;
41  const double jitter = 0.0;
42  const auto max_backoff = grpc_core::Duration::Seconds(1);
44  BackOff::Options options;
45  options.set_initial_backoff(initial_backoff)
46  .set_multiplier(multiplier)
47  .set_jitter(jitter)
48  .set_max_backoff(max_backoff);
49  BackOff backoff(options);
50 
51  grpc_core::Timestamp next_attempt_start_time = backoff.NextAttemptTime();
52  EXPECT_EQ(next_attempt_start_time - grpc_core::ExecCtx::Get()->Now(),
53  initial_backoff);
54  for (int i = 0; i < 10000; i++) {
55  next_attempt_start_time = backoff.NextAttemptTime();
56  EXPECT_EQ(next_attempt_start_time - grpc_core::ExecCtx::Get()->Now(),
57  initial_backoff);
58  }
59 }
60 
61 TEST(BackOffTest, MinConnect) {
62  const auto initial_backoff = grpc_core::Duration::Milliseconds(100);
63  const double multiplier = 1.0;
64  const double jitter = 0.0;
65  const auto max_backoff = grpc_core::Duration::Seconds(1);
67  BackOff::Options options;
68  options.set_initial_backoff(initial_backoff)
69  .set_multiplier(multiplier)
70  .set_jitter(jitter)
71  .set_max_backoff(max_backoff);
72  BackOff backoff(options);
73  grpc_core::Timestamp next = backoff.NextAttemptTime();
74  EXPECT_EQ(next - grpc_core::ExecCtx::Get()->Now(), initial_backoff);
75 }
76 
77 TEST(BackOffTest, NoJitterBackOff) {
78  const auto initial_backoff = grpc_core::Duration::Milliseconds(2);
79  const double multiplier = 2.0;
80  const double jitter = 0.0;
81  const auto max_backoff = grpc_core::Duration::Milliseconds(513);
82  BackOff::Options options;
83  options.set_initial_backoff(initial_backoff)
84  .set_multiplier(multiplier)
85  .set_jitter(jitter)
86  .set_max_backoff(max_backoff);
87  BackOff backoff(options);
88  // x_1 = 2
89  // x_n = 2**i + x_{i-1} ( = 2**(n+1) - 2 )
93  grpc_core::Timestamp next = backoff.NextAttemptTime();
96  next = backoff.NextAttemptTime();
99  next = backoff.NextAttemptTime();
102  next = backoff.NextAttemptTime();
105  next = backoff.NextAttemptTime();
108  next = backoff.NextAttemptTime();
111  next = backoff.NextAttemptTime();
114  next = backoff.NextAttemptTime();
117  next = backoff.NextAttemptTime();
118  EXPECT_EQ(next,
121  next = backoff.NextAttemptTime();
122  // Hit the maximum timeout. From this point onwards, retries will increase
123  // only by max timeout.
124  EXPECT_EQ(next,
127  next = backoff.NextAttemptTime();
128  EXPECT_EQ(next,
131  next = backoff.NextAttemptTime();
132  EXPECT_EQ(next,
134 }
135 
136 TEST(BackOffTest, JitterBackOff) {
137  const auto initial_backoff = grpc_core::Duration::Milliseconds(500);
138  auto current_backoff = initial_backoff;
139  const auto max_backoff = grpc_core::Duration::Seconds(1);
140  const double multiplier = 1.0;
141  const double jitter = 0.1;
142  BackOff::Options options;
143  options.set_initial_backoff(initial_backoff)
144  .set_multiplier(multiplier)
145  .set_jitter(jitter)
146  .set_max_backoff(max_backoff);
147  BackOff backoff(options);
148 
150  grpc_core::Timestamp next = backoff.NextAttemptTime();
151  EXPECT_EQ(next - grpc_core::ExecCtx::Get()->Now(), initial_backoff);
152 
153  auto expected_next_lower_bound = grpc_core::Duration::Milliseconds(
154  static_cast<double>(current_backoff.millis()) * (1 - jitter));
155  auto expected_next_upper_bound = grpc_core::Duration::Milliseconds(
156  static_cast<double>(current_backoff.millis()) * (1 + jitter));
157 
158  for (int i = 0; i < 10000; i++) {
159  next = backoff.NextAttemptTime();
160  // next-now must be within (jitter*100)% of the current backoff (which
161  // increases by * multiplier up to max_backoff).
162  const grpc_core::Duration timeout_millis =
164  EXPECT_GE(timeout_millis, expected_next_lower_bound);
165  EXPECT_LE(timeout_millis, expected_next_upper_bound);
166  current_backoff = std::min(
168  static_cast<double>(current_backoff.millis()) * multiplier),
169  max_backoff);
170  expected_next_lower_bound = grpc_core::Duration::Milliseconds(
171  static_cast<double>(current_backoff.millis()) * (1 - jitter));
172  expected_next_upper_bound = grpc_core::Duration::Milliseconds(
173  static_cast<double>(current_backoff.millis()) * (1 + jitter));
174  }
175 }
176 
177 } // namespace
178 } // namespace testing
179 } // namespace grpc
180 
181 int main(int argc, char** argv) {
182  ::testing::InitGoogleTest(&argc, argv);
183  grpc::testing::TestEnvironment env(&argc, argv);
184  grpc_init();
185  int ret = RUN_ALL_TESTS();
186  grpc_shutdown();
187  return ret;
188 }
testing
Definition: aws_request_signer_test.cc:25
log.h
backoff.h
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
options
double_dict options[]
Definition: capstone_test.c:55
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
EXPECT_LE
#define EXPECT_LE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2030
grpc.h
time.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
min
#define min(a, b)
Definition: qsort.h:83
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::BackOff
Definition: backoff.h:32
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch
static constexpr Timestamp FromMillisecondsAfterProcessEpoch(int64_t millis)
Definition: src/core/lib/gprpp/time.h:73
test_config.h
grpc_core::ExecCtx::TestOnlySetNow
void TestOnlySetNow(Timestamp new_val)
Definition: exec_ctx.h:199
grpc_core::Duration::Milliseconds
static constexpr Duration Milliseconds(int64_t millis)
Definition: src/core/lib/gprpp/time.h:155
absl::Now
ABSL_NAMESPACE_BEGIN Time Now()
Definition: abseil-cpp/absl/time/clock.cc:39
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
main
int main(int argc, char **argv)
Definition: backoff_test.cc:181
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_core::Duration::Seconds
static constexpr Duration Seconds(int64_t seconds)
Definition: src/core/lib/gprpp/time.h:151
exec_ctx.h
grpc::testing::EXPECT_EQ
EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange")
EXPECT_GE
#define EXPECT_GE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2034
grpc::testing::TEST
TEST(StatsTest, IncCounters)
Definition: stats_test.cc:51
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:45