| 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 |
|
| 19 |
#include "src/core/lib/backoff/backoff.h" |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
|
| 23 |
#include "gtest/gtest.h" |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
|
| 27 |
#include "src/core/lib/gprpp/time.h" |
| 28 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 29 |
#include "test/core/util/test_config.h" |
| 30 |
|
| 31 |
namespace grpc { |
| 32 |
namespace testing { |
| 33 |
namespace { |
| 34 |
|
| 35 |
using grpc_core::BackOff; |
| 36 |
|
| 37 |
TEST(BackOffTest, ConstantBackOff) { |
| 38 |
const auto initial_backoff = grpc_core::Duration::Milliseconds(200); |
| 39 |
const double multiplier = 1.0; |
| 40 |
const double jitter = 0.0; |
| 41 |
const auto max_backoff = grpc_core::Duration::Seconds(1); |
| 42 |
grpc_core::ExecCtx exec_ctx; |
| 43 |
BackOff::Options options; |
| 44 |
options.set_initial_backoff(initial_backoff) |
| 45 |
.set_multiplier(multiplier) |
| 46 |
.set_jitter(jitter) |
| 47 |
.set_max_backoff(max_backoff); |
| 48 |
BackOff backoff(options); |
| 49 |
|
| 50 |
grpc_core::Timestamp next_attempt_start_time = backoff.NextAttemptTime(); |
| 51 |
EXPECT_EQ(next_attempt_start_time - grpc_core::Timestamp::Now(), |
| 52 |
initial_backoff); |
| 53 |
for (int i = 0; i < 10000; i++) { |
| 54 |
next_attempt_start_time = backoff.NextAttemptTime(); |
| 55 |
EXPECT_EQ(next_attempt_start_time - grpc_core::Timestamp::Now(), |
| 56 |
initial_backoff); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
TEST(BackOffTest, MinConnect) { |
| 61 |
const auto initial_backoff = grpc_core::Duration::Milliseconds(100); |
| 62 |
const double multiplier = 1.0; |
| 63 |
const double jitter = 0.0; |
| 64 |
const auto max_backoff = grpc_core::Duration::Seconds(1); |
| 65 |
grpc_core::ExecCtx exec_ctx; |
| 66 |
BackOff::Options options; |
| 67 |
options.set_initial_backoff(initial_backoff) |
| 68 |
.set_multiplier(multiplier) |
| 69 |
.set_jitter(jitter) |
| 70 |
.set_max_backoff(max_backoff); |
| 71 |
BackOff backoff(options); |
| 72 |
grpc_core::Timestamp next = backoff.NextAttemptTime(); |
| 73 |
EXPECT_EQ(next - grpc_core::Timestamp::Now(), initial_backoff); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(BackOffTest, NoJitterBackOff) { |
| 77 |
const auto initial_backoff = grpc_core::Duration::Milliseconds(2); |
| 78 |
const double multiplier = 2.0; |
| 79 |
const double jitter = 0.0; |
| 80 |
const auto max_backoff = grpc_core::Duration::Milliseconds(513); |
| 81 |
BackOff::Options options; |
| 82 |
options.set_initial_backoff(initial_backoff) |
| 83 |
.set_multiplier(multiplier) |
| 84 |
.set_jitter(jitter) |
| 85 |
.set_max_backoff(max_backoff); |
| 86 |
BackOff backoff(options); |
| 87 |
// x_1 = 2 |
| 88 |
// x_n = 2**i + x_{i-1} ( = 2**(n+1) - 2 ) |
| 89 |
grpc_core::ExecCtx exec_ctx; |
| 90 |
grpc_core::ExecCtx::Get()->TestOnlySetNow( |
| 91 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(0)); |
| 92 |
grpc_core::Timestamp next = backoff.NextAttemptTime(); |
| 93 |
EXPECT_EQ(next, grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(2)); |
| 94 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 95 |
next = backoff.NextAttemptTime(); |
| 96 |
EXPECT_EQ(next, grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(6)); |
| 97 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 98 |
next = backoff.NextAttemptTime(); |
| 99 |
EXPECT_EQ(next, grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(14)); |
| 100 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 101 |
next = backoff.NextAttemptTime(); |
| 102 |
EXPECT_EQ(next, grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(30)); |
| 103 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 104 |
next = backoff.NextAttemptTime(); |
| 105 |
EXPECT_EQ(next, grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(62)); |
| 106 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 107 |
next = backoff.NextAttemptTime(); |
| 108 |
EXPECT_EQ(next, grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(126)); |
| 109 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 110 |
next = backoff.NextAttemptTime(); |
| 111 |
EXPECT_EQ(next, grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(254)); |
| 112 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 113 |
next = backoff.NextAttemptTime(); |
| 114 |
EXPECT_EQ(next, grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(510)); |
| 115 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 116 |
next = backoff.NextAttemptTime(); |
| 117 |
EXPECT_EQ(next, |
| 118 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(1022)); |
| 119 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 120 |
next = backoff.NextAttemptTime(); |
| 121 |
// Hit the maximum timeout. From this point onwards, retries will increase |
| 122 |
// only by max timeout. |
| 123 |
EXPECT_EQ(next, |
| 124 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(1535)); |
| 125 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 126 |
next = backoff.NextAttemptTime(); |
| 127 |
EXPECT_EQ(next, |
| 128 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(2048)); |
| 129 |
grpc_core::ExecCtx::Get()->TestOnlySetNow(next); |
| 130 |
next = backoff.NextAttemptTime(); |
| 131 |
EXPECT_EQ(next, |
| 132 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(2561)); |
| 133 |
} |
| 134 |
|
| 135 |
TEST(BackOffTest, JitterBackOff) { |
| 136 |
const auto initial_backoff = grpc_core::Duration::Milliseconds(500); |
| 137 |
auto current_backoff = initial_backoff; |
| 138 |
const auto max_backoff = grpc_core::Duration::Seconds(1); |
| 139 |
const double multiplier = 1.0; |
| 140 |
const double jitter = 0.1; |
| 141 |
BackOff::Options options; |
| 142 |
options.set_initial_backoff(initial_backoff) |
| 143 |
.set_multiplier(multiplier) |
| 144 |
.set_jitter(jitter) |
| 145 |
.set_max_backoff(max_backoff); |
| 146 |
BackOff backoff(options); |
| 147 |
|
| 148 |
grpc_core::ExecCtx exec_ctx; |
| 149 |
grpc_core::Timestamp next = backoff.NextAttemptTime(); |
| 150 |
EXPECT_EQ(next - grpc_core::Timestamp::Now(), initial_backoff); |
| 151 |
|
| 152 |
auto expected_next_lower_bound = grpc_core::Duration::Milliseconds( |
| 153 |
static_cast<double>(current_backoff.millis()) * (1 - jitter)); |
| 154 |
auto expected_next_upper_bound = grpc_core::Duration::Milliseconds( |
| 155 |
static_cast<double>(current_backoff.millis()) * (1 + jitter)); |
| 156 |
|
| 157 |
for (int i = 0; i < 10000; i++) { |
| 158 |
next = backoff.NextAttemptTime(); |
| 159 |
// next-now must be within (jitter*100)% of the current backoff (which |
| 160 |
// increases by * multiplier up to max_backoff). |
| 161 |
const grpc_core::Duration timeout_millis = |
| 162 |
next - grpc_core::Timestamp::Now(); |
| 163 |
EXPECT_GE(timeout_millis, expected_next_lower_bound); |
| 164 |
EXPECT_LE(timeout_millis, expected_next_upper_bound); |
| 165 |
current_backoff = std::min( |
| 166 |
grpc_core::Duration::Milliseconds( |
| 167 |
static_cast<double>(current_backoff.millis()) * multiplier), |
| 168 |
max_backoff); |
| 169 |
expected_next_lower_bound = grpc_core::Duration::Milliseconds( |
| 170 |
static_cast<double>(current_backoff.millis()) * (1 - jitter)); |
| 171 |
expected_next_upper_bound = grpc_core::Duration::Milliseconds( |
| 172 |
static_cast<double>(current_backoff.millis()) * (1 + jitter)); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
} // namespace |
| 177 |
} // namespace testing |
| 178 |
} // namespace grpc |
| 179 |
|
| 180 |
int main(int argc, char** argv) { |
| 181 |
::testing::InitGoogleTest(&argc, argv); |
| 182 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 183 |
grpc_init(); |
| 184 |
int ret = RUN_ALL_TESTS(); |
| 185 |
grpc_shutdown(); |
| 186 |
return ret; |
| 187 |
} |