| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/io/retry_strategy.h> |
| 6 |
|
| 7 |
#include <aws/testing/aws_test_harness.h> |
| 8 |
|
| 9 |
#include <aws/common/clock.h> |
| 10 |
#include <aws/common/condition_variable.h> |
| 11 |
|
| 12 |
#include <aws/io/event_loop.h> |
| 13 |
|
| 14 |
struct exponential_backoff_test_data { |
| 15 |
size_t retry_count; |
| 16 |
size_t client_error_count; |
| 17 |
int failure_error_code; |
| 18 |
struct aws_mutex mutex; |
| 19 |
struct aws_condition_variable cvar; |
| 20 |
}; |
| 21 |
|
| 22 |
static void s_too_many_retries_test_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data) { |
| 23 |
(void)error_code; |
| 24 |
|
| 25 |
struct exponential_backoff_test_data *test_data = user_data; |
| 26 |
enum aws_retry_error_type error_type = AWS_RETRY_ERROR_TYPE_SERVER_ERROR; |
| 27 |
|
| 28 |
aws_mutex_lock(&test_data->mutex); |
| 29 |
test_data->retry_count += 1; |
| 30 |
|
| 31 |
if (test_data->client_error_count) { |
| 32 |
error_type = AWS_RETRY_ERROR_TYPE_CLIENT_ERROR; |
| 33 |
test_data->client_error_count--; |
| 34 |
} |
| 35 |
aws_mutex_unlock(&test_data->mutex); |
| 36 |
|
| 37 |
if (aws_retry_strategy_schedule_retry(token, error_type, s_too_many_retries_test_on_retry_ready, user_data)) { |
| 38 |
aws_mutex_lock(&test_data->mutex); |
| 39 |
test_data->failure_error_code = aws_last_error(); |
| 40 |
aws_mutex_unlock(&test_data->mutex); |
| 41 |
aws_retry_token_release(token); |
| 42 |
aws_condition_variable_notify_all(&test_data->cvar); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static void s_too_many_retries_test_token_acquired( |
| 47 |
struct aws_retry_strategy *retry_strategy, |
| 48 |
int error_code, |
| 49 |
struct aws_retry_token *token, |
| 50 |
void *user_data) { |
| 51 |
(void)retry_strategy; |
| 52 |
(void)error_code; |
| 53 |
|
| 54 |
aws_retry_strategy_schedule_retry( |
| 55 |
token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_too_many_retries_test_on_retry_ready, user_data); |
| 56 |
} |
| 57 |
|
| 58 |
static bool s_retry_has_failed(void *arg) { |
| 59 |
struct exponential_backoff_test_data *test_data = arg; |
| 60 |
return test_data->failure_error_code != AWS_OP_SUCCESS; |
| 61 |
} |
| 62 |
|
| 63 |
static int s_test_exponential_backoff_retry_too_many_retries_for_jitter_mode( |
| 64 |
struct aws_allocator *allocator, |
| 65 |
enum aws_exponential_backoff_jitter_mode jitter_mode) { |
| 66 |
|
| 67 |
aws_io_library_init(allocator); |
| 68 |
|
| 69 |
struct aws_event_loop_group_options elg_options = { |
| 70 |
.loop_count = 1, |
| 71 |
}; |
| 72 |
struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options); |
| 73 |
struct aws_exponential_backoff_retry_options config = { |
| 74 |
.max_retries = 3, |
| 75 |
.jitter_mode = jitter_mode, |
| 76 |
.el_group = el_group, |
| 77 |
}; |
| 78 |
|
| 79 |
struct aws_retry_strategy *retry_strategy = aws_retry_strategy_new_exponential_backoff(allocator, &config); |
| 80 |
ASSERT_NOT_NULL(retry_strategy); |
| 81 |
|
| 82 |
struct exponential_backoff_test_data test_data = { |
| 83 |
.retry_count = 0, |
| 84 |
.failure_error_code = 0, |
| 85 |
.mutex = AWS_MUTEX_INIT, |
| 86 |
.cvar = AWS_CONDITION_VARIABLE_INIT, |
| 87 |
}; |
| 88 |
|
| 89 |
ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); |
| 90 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 91 |
retry_strategy, NULL, s_too_many_retries_test_token_acquired, &test_data, 0)); |
| 92 |
|
| 93 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred(&test_data.cvar, &test_data.mutex, s_retry_has_failed, &test_data)); |
| 94 |
aws_mutex_unlock(&test_data.mutex); |
| 95 |
|
| 96 |
ASSERT_UINT_EQUALS(config.max_retries, test_data.retry_count); |
| 97 |
ASSERT_UINT_EQUALS(AWS_IO_MAX_RETRIES_EXCEEDED, test_data.failure_error_code); |
| 98 |
|
| 99 |
aws_retry_strategy_release(retry_strategy); |
| 100 |
aws_event_loop_group_release(el_group); |
| 101 |
|
| 102 |
aws_io_library_clean_up(); |
| 103 |
|
| 104 |
return AWS_OP_SUCCESS; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
static int s_test_exponential_backoff_retry_too_many_retries_no_jitter_fn(struct aws_allocator *allocator, void *ctx) { |
| 109 |
(void)ctx; |
| 110 |
return s_test_exponential_backoff_retry_too_many_retries_for_jitter_mode( |
| 111 |
allocator, AWS_EXPONENTIAL_BACKOFF_JITTER_NONE); |
| 112 |
} |
| 113 |
|
| 114 |
AWS_TEST_CASE( |
| 115 |
test_exponential_backoff_retry_too_many_retries_no_jitter, |
| 116 |
s_test_exponential_backoff_retry_too_many_retries_no_jitter_fn) |
| 117 |
|
| 118 |
|
| 119 |
static int s_test_exponential_backoff_retry_too_many_retries_full_jitter_fn( |
| 120 |
struct aws_allocator *allocator, |
| 121 |
void *ctx) { |
| 122 |
(void)ctx; |
| 123 |
return s_test_exponential_backoff_retry_too_many_retries_for_jitter_mode( |
| 124 |
allocator, AWS_EXPONENTIAL_BACKOFF_JITTER_FULL); |
| 125 |
} |
| 126 |
|
| 127 |
AWS_TEST_CASE( |
| 128 |
test_exponential_backoff_retry_too_many_retries_full_jitter, |
| 129 |
s_test_exponential_backoff_retry_too_many_retries_full_jitter_fn) |
| 130 |
|
| 131 |
|
| 132 |
static int s_test_exponential_backoff_retry_too_many_retries_decorrelated_jitter_fn( |
| 133 |
struct aws_allocator *allocator, |
| 134 |
void *ctx) { |
| 135 |
(void)ctx; |
| 136 |
return s_test_exponential_backoff_retry_too_many_retries_for_jitter_mode( |
| 137 |
allocator, AWS_EXPONENTIAL_BACKOFF_JITTER_DECORRELATED); |
| 138 |
} |
| 139 |
|
| 140 |
AWS_TEST_CASE( |
| 141 |
test_exponential_backoff_retry_too_many_retries_decorrelated_jitter, |
| 142 |
s_test_exponential_backoff_retry_too_many_retries_decorrelated_jitter_fn) |
| 143 |
|
| 144 |
|
| 145 |
static int s_test_exponential_backoff_retry_too_many_retries_default_jitter_fn( |
| 146 |
struct aws_allocator *allocator, |
| 147 |
void *ctx) { |
| 148 |
(void)ctx; |
| 149 |
return s_test_exponential_backoff_retry_too_many_retries_for_jitter_mode( |
| 150 |
allocator, AWS_EXPONENTIAL_BACKOFF_JITTER_DEFAULT); |
| 151 |
} |
| 152 |
|
| 153 |
AWS_TEST_CASE( |
| 154 |
test_exponential_backoff_retry_too_many_retries_default_jitter, |
| 155 |
s_test_exponential_backoff_retry_too_many_retries_default_jitter_fn) |
| 156 |
|
| 157 |
|
| 158 |
static int s_test_exponential_backoff_retry_client_errors_do_not_count_fn(struct aws_allocator *allocator, void *ctx) { |
| 159 |
(void)ctx; |
| 160 |
|
| 161 |
aws_io_library_init(allocator); |
| 162 |
|
| 163 |
struct aws_event_loop_group_options elg_options = { |
| 164 |
.loop_count = 1, |
| 165 |
}; |
| 166 |
struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options); |
| 167 |
struct aws_exponential_backoff_retry_options config = { |
| 168 |
.el_group = el_group, |
| 169 |
.max_retries = 3, |
| 170 |
}; |
| 171 |
|
| 172 |
struct aws_retry_strategy *retry_strategy = aws_retry_strategy_new_exponential_backoff(allocator, &config); |
| 173 |
ASSERT_NOT_NULL(retry_strategy); |
| 174 |
|
| 175 |
struct exponential_backoff_test_data test_data = { |
| 176 |
.retry_count = 0, |
| 177 |
.failure_error_code = 0, |
| 178 |
.mutex = AWS_MUTEX_INIT, |
| 179 |
.cvar = AWS_CONDITION_VARIABLE_INIT, |
| 180 |
.client_error_count = 2, |
| 181 |
}; |
| 182 |
|
| 183 |
ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); |
| 184 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 185 |
retry_strategy, NULL, s_too_many_retries_test_token_acquired, &test_data, 0)); |
| 186 |
|
| 187 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred(&test_data.cvar, &test_data.mutex, s_retry_has_failed, &test_data)); |
| 188 |
aws_mutex_unlock(&test_data.mutex); |
| 189 |
|
| 190 |
ASSERT_UINT_EQUALS(config.max_retries + 2, test_data.retry_count); |
| 191 |
ASSERT_UINT_EQUALS(AWS_IO_MAX_RETRIES_EXCEEDED, test_data.failure_error_code); |
| 192 |
|
| 193 |
aws_retry_strategy_release(retry_strategy); |
| 194 |
aws_event_loop_group_release(el_group); |
| 195 |
|
| 196 |
aws_io_library_clean_up(); |
| 197 |
|
| 198 |
return AWS_OP_SUCCESS; |
| 199 |
} |
| 200 |
AWS_TEST_CASE( |
| 201 |
test_exponential_backoff_retry_client_errors_do_not_count, |
| 202 |
s_test_exponential_backoff_retry_client_errors_do_not_count_fn) |
| 203 |
|
| 204 |
|
| 205 |
static int s_test_exponential_backoff_retry_no_jitter_time_taken_fn(struct aws_allocator *allocator, void *ctx) { |
| 206 |
(void)ctx; |
| 207 |
|
| 208 |
aws_io_library_init(allocator); |
| 209 |
|
| 210 |
struct aws_event_loop_group_options elg_options = { |
| 211 |
.loop_count = 1, |
| 212 |
}; |
| 213 |
struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options); |
| 214 |
struct aws_exponential_backoff_retry_options config = { |
| 215 |
.max_retries = 3, |
| 216 |
.jitter_mode = AWS_EXPONENTIAL_BACKOFF_JITTER_NONE, |
| 217 |
.el_group = el_group, |
| 218 |
}; |
| 219 |
|
| 220 |
struct aws_retry_strategy *retry_strategy = aws_retry_strategy_new_exponential_backoff(allocator, &config); |
| 221 |
ASSERT_NOT_NULL(retry_strategy); |
| 222 |
|
| 223 |
struct exponential_backoff_test_data test_data = { |
| 224 |
.retry_count = 0, |
| 225 |
.failure_error_code = 0, |
| 226 |
.mutex = AWS_MUTEX_INIT, |
| 227 |
.cvar = AWS_CONDITION_VARIABLE_INIT, |
| 228 |
}; |
| 229 |
|
| 230 |
uint64_t before_time = 0; |
| 231 |
ASSERT_SUCCESS(aws_high_res_clock_get_ticks(&before_time)); |
| 232 |
ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); |
| 233 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 234 |
retry_strategy, NULL, s_too_many_retries_test_token_acquired, &test_data, 0)); |
| 235 |
|
| 236 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred(&test_data.cvar, &test_data.mutex, s_retry_has_failed, &test_data)); |
| 237 |
aws_mutex_unlock(&test_data.mutex); |
| 238 |
uint64_t after_time = 0; |
| 239 |
ASSERT_SUCCESS(aws_high_res_clock_get_ticks(&after_time)); |
| 240 |
uint64_t backoff_scale_factor = |
| 241 |
aws_timestamp_convert(config.backoff_scale_factor_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 242 |
uint64_t expected_interval = (1 * backoff_scale_factor) + (2 * backoff_scale_factor) + (4 * backoff_scale_factor); |
| 243 |
ASSERT_TRUE(expected_interval <= after_time - before_time); |
| 244 |
|
| 245 |
ASSERT_UINT_EQUALS(config.max_retries, test_data.retry_count); |
| 246 |
ASSERT_UINT_EQUALS(AWS_IO_MAX_RETRIES_EXCEEDED, test_data.failure_error_code); |
| 247 |
|
| 248 |
aws_retry_strategy_release(retry_strategy); |
| 249 |
aws_event_loop_group_release(el_group); |
| 250 |
|
| 251 |
aws_io_library_clean_up(); |
| 252 |
|
| 253 |
return AWS_OP_SUCCESS; |
| 254 |
} |
| 255 |
AWS_TEST_CASE( |
| 256 |
test_exponential_backoff_retry_no_jitter_time_taken, |
| 257 |
s_test_exponential_backoff_retry_no_jitter_time_taken_fn) |
| 258 |
|
| 259 |
|
| 260 |
static int s_test_exponential_max_backoff_retry_no_jitter_fn(struct aws_allocator *allocator, void *ctx) { |
| 261 |
(void)ctx; |
| 262 |
|
| 263 |
aws_io_library_init(allocator); |
| 264 |
|
| 265 |
struct aws_event_loop_group_options elg_options = { |
| 266 |
.loop_count = 1, |
| 267 |
}; |
| 268 |
struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options); |
| 269 |
struct aws_exponential_backoff_retry_options config = { |
| 270 |
.max_retries = 3, |
| 271 |
.jitter_mode = AWS_EXPONENTIAL_BACKOFF_JITTER_NONE, |
| 272 |
.el_group = el_group, |
| 273 |
.backoff_scale_factor_ms = 1000, |
| 274 |
.max_backoff_secs = 3, |
| 275 |
}; |
| 276 |
|
| 277 |
struct aws_retry_strategy *retry_strategy = aws_retry_strategy_new_exponential_backoff(allocator, &config); |
| 278 |
ASSERT_NOT_NULL(retry_strategy); |
| 279 |
|
| 280 |
struct exponential_backoff_test_data test_data = { |
| 281 |
.retry_count = 0, |
| 282 |
.failure_error_code = 0, |
| 283 |
.mutex = AWS_MUTEX_INIT, |
| 284 |
.cvar = AWS_CONDITION_VARIABLE_INIT, |
| 285 |
}; |
| 286 |
|
| 287 |
uint64_t before_time = 0; |
| 288 |
ASSERT_SUCCESS(aws_high_res_clock_get_ticks(&before_time)); |
| 289 |
ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); |
| 290 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 291 |
retry_strategy, NULL, s_too_many_retries_test_token_acquired, &test_data, 0)); |
| 292 |
|
| 293 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred(&test_data.cvar, &test_data.mutex, s_retry_has_failed, &test_data)); |
| 294 |
aws_mutex_unlock(&test_data.mutex); |
| 295 |
uint64_t after_time = 0; |
| 296 |
ASSERT_SUCCESS(aws_high_res_clock_get_ticks(&after_time)); |
| 297 |
uint64_t backoff_scale_factor = |
| 298 |
aws_timestamp_convert(config.backoff_scale_factor_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 299 |
uint64_t max_backoff_scale_factor = |
| 300 |
aws_timestamp_convert(config.max_backoff_secs, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 301 |
|
| 302 |
uint64_t expected_interval = aws_min_u64(max_backoff_scale_factor, 1 * backoff_scale_factor) + |
| 303 |
aws_min_u64(max_backoff_scale_factor, 2 * backoff_scale_factor) + |
| 304 |
aws_min_u64(max_backoff_scale_factor, 4 * backoff_scale_factor); |
| 305 |
ASSERT_TRUE(expected_interval <= after_time - before_time); |
| 306 |
|
| 307 |
ASSERT_UINT_EQUALS(config.max_retries, test_data.retry_count); |
| 308 |
ASSERT_UINT_EQUALS(AWS_IO_MAX_RETRIES_EXCEEDED, test_data.failure_error_code); |
| 309 |
|
| 310 |
aws_retry_strategy_release(retry_strategy); |
| 311 |
aws_event_loop_group_release(el_group); |
| 312 |
|
| 313 |
aws_io_library_clean_up(); |
| 314 |
|
| 315 |
return AWS_OP_SUCCESS; |
| 316 |
} |
| 317 |
AWS_TEST_CASE(test_exponential_max_backoff_retry_no_jitter, s_test_exponential_max_backoff_retry_no_jitter_fn) |
| 318 |
|
| 319 |
|
| 320 |
static int s_test_exponential_backoff_retry_invalid_options_fn(struct aws_allocator *allocator, void *ctx) { |
| 321 |
(void)ctx; |
| 322 |
|
| 323 |
aws_io_library_init(allocator); |
| 324 |
|
| 325 |
struct aws_event_loop_group_options elg_options = { |
| 326 |
.loop_count = 1, |
| 327 |
}; |
| 328 |
struct aws_event_loop_group *el_group = aws_event_loop_group_new(allocator, &elg_options); |
| 329 |
struct aws_exponential_backoff_retry_options config = { |
| 330 |
.max_retries = 64, |
| 331 |
.el_group = el_group, |
| 332 |
}; |
| 333 |
|
| 334 |
struct aws_retry_strategy *retry_strategy = aws_retry_strategy_new_exponential_backoff(allocator, &config); |
| 335 |
ASSERT_NULL(retry_strategy); |
| 336 |
ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error()); |
| 337 |
|
| 338 |
aws_event_loop_group_release(el_group); |
| 339 |
|
| 340 |
aws_io_library_clean_up(); |
| 341 |
|
| 342 |
return AWS_OP_SUCCESS; |
| 343 |
} |
| 344 |
AWS_TEST_CASE(test_exponential_backoff_retry_invalid_options, s_test_exponential_backoff_retry_invalid_options_fn) |