| 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 |
#include <aws/common/shutdown_types.h> |
| 12 |
|
| 13 |
#include <aws/io/event_loop.h> |
| 14 |
|
| 15 |
struct exponential_backoff_test_data { |
| 16 |
size_t retry_count; |
| 17 |
size_t client_error_count; |
| 18 |
struct aws_event_loop_group *el_group; |
| 19 |
struct aws_retry_strategy *retry_strategy; |
| 20 |
int failure_error_code; |
| 21 |
struct aws_mutex mutex; |
| 22 |
struct aws_condition_variable cvar; |
| 23 |
bool el_group_shutdown; |
| 24 |
}; |
| 25 |
|
| 26 |
static struct exponential_backoff_test_data s_fixture_test_data = { |
| 27 |
.cvar = AWS_CONDITION_VARIABLE_INIT, |
| 28 |
.mutex = AWS_MUTEX_INIT, |
| 29 |
}; |
| 30 |
|
| 31 |
static void s_el_group_completion_callback(void *arg) { |
| 32 |
struct exponential_backoff_test_data *test_data = arg; |
| 33 |
|
| 34 |
aws_mutex_lock(&test_data->mutex); |
| 35 |
test_data->el_group_shutdown = true; |
| 36 |
aws_mutex_unlock(&test_data->mutex); |
| 37 |
aws_condition_variable_notify_one(&test_data->cvar); |
| 38 |
} |
| 39 |
|
| 40 |
static bool s_el_group_shutdown_predicate(void *arg) { |
| 41 |
struct exponential_backoff_test_data *test_data = arg; |
| 42 |
return test_data->el_group_shutdown; |
| 43 |
} |
| 44 |
|
| 45 |
static int s_fixture_setup(struct aws_allocator *allocator, void *ctx) { |
| 46 |
aws_io_library_init(allocator); |
| 47 |
struct exponential_backoff_test_data *test_data = ctx; |
| 48 |
struct aws_shutdown_callback_options shutdown_options = { |
| 49 |
.shutdown_callback_fn = s_el_group_completion_callback, |
| 50 |
.shutdown_callback_user_data = ctx, |
| 51 |
}; |
| 52 |
|
| 53 |
struct aws_event_loop_group_options elg_options = { |
| 54 |
.loop_count = 1, |
| 55 |
.shutdown_options = &shutdown_options, |
| 56 |
}; |
| 57 |
test_data->el_group = aws_event_loop_group_new(allocator, &elg_options); |
| 58 |
|
| 59 |
ASSERT_NOT_NULL(test_data->el_group); |
| 60 |
struct aws_standard_retry_options retry_options = { |
| 61 |
.initial_bucket_capacity = 15, |
| 62 |
.backoff_retry_options = |
| 63 |
{ |
| 64 |
.el_group = test_data->el_group, |
| 65 |
}, |
| 66 |
}; |
| 67 |
test_data->retry_strategy = aws_retry_strategy_new_standard(allocator, &retry_options); |
| 68 |
ASSERT_NOT_NULL(test_data->retry_strategy); |
| 69 |
|
| 70 |
return AWS_OP_SUCCESS; |
| 71 |
} |
| 72 |
|
| 73 |
static int s_fixture_shutdown(struct aws_allocator *allocator, int setup_error_code, void *ctx) { |
| 74 |
(void)allocator; |
| 75 |
|
| 76 |
if (!setup_error_code) { |
| 77 |
struct exponential_backoff_test_data *test_data = ctx; |
| 78 |
|
| 79 |
aws_mutex_lock(&test_data->mutex); |
| 80 |
aws_retry_strategy_release(test_data->retry_strategy); |
| 81 |
aws_event_loop_group_release(test_data->el_group); |
| 82 |
aws_condition_variable_wait_pred(&test_data->cvar, &test_data->mutex, s_el_group_shutdown_predicate, ctx); |
| 83 |
aws_mutex_unlock(&test_data->mutex); |
| 84 |
} |
| 85 |
|
| 86 |
aws_io_library_clean_up(); |
| 87 |
|
| 88 |
return AWS_OP_SUCCESS; |
| 89 |
} |
| 90 |
|
| 91 |
static int s_test_standard_retry_strategy_setup_shutdown(struct aws_allocator *allocator, void *ctx) { |
| 92 |
(void)allocator; |
| 93 |
(void)ctx; |
| 94 |
return AWS_OP_SUCCESS; |
| 95 |
} |
| 96 |
|
| 97 |
AWS_TEST_CASE_FIXTURE( |
| 98 |
test_standard_retry_strategy_setup_shutdown, |
| 99 |
s_fixture_setup, |
| 100 |
s_test_standard_retry_strategy_setup_shutdown, |
| 101 |
s_fixture_shutdown, |
| 102 |
&s_fixture_test_data); |
| 103 |
|
| 104 |
struct retry_data { |
| 105 |
struct aws_retry_token *retry_token; |
| 106 |
struct aws_retry_strategy *retry_strategy; |
| 107 |
struct aws_mutex mutex; |
| 108 |
struct aws_condition_variable cvar; |
| 109 |
int token_acquisition_error_code; |
| 110 |
int schedule_retry_error_code; |
| 111 |
struct aws_retry_token *schedule_token_value; |
| 112 |
}; |
| 113 |
|
| 114 |
static bool s_retry_token_acquisition_completed(void *arg) { |
| 115 |
struct retry_data *retry_data = arg; |
| 116 |
return retry_data->retry_token || retry_data->token_acquisition_error_code; |
| 117 |
} |
| 118 |
|
| 119 |
static void s_on_retry_token_acquired( |
| 120 |
struct aws_retry_strategy *retry_strategy, |
| 121 |
int error_code, |
| 122 |
struct aws_retry_token *token, |
| 123 |
void *user_data) { |
| 124 |
|
| 125 |
struct retry_data *retry_data = user_data; |
| 126 |
aws_mutex_lock(&retry_data->mutex); |
| 127 |
retry_data->retry_token = token; |
| 128 |
retry_data->token_acquisition_error_code = error_code; |
| 129 |
retry_data->retry_strategy = retry_strategy; |
| 130 |
aws_mutex_unlock(&retry_data->mutex); |
| 131 |
aws_condition_variable_notify_one(&retry_data->cvar); |
| 132 |
} |
| 133 |
|
| 134 |
static bool s_retry_ready_completion_predicate(void *arg) { |
| 135 |
struct retry_data *retry_data = arg; |
| 136 |
return retry_data->schedule_retry_error_code || retry_data->schedule_token_value; |
| 137 |
} |
| 138 |
|
| 139 |
static void s_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data) { |
| 140 |
struct retry_data *retry_data = user_data; |
| 141 |
aws_mutex_lock(&retry_data->mutex); |
| 142 |
retry_data->schedule_retry_error_code = error_code; |
| 143 |
retry_data->schedule_token_value = token; |
| 144 |
aws_mutex_unlock(&retry_data->mutex); |
| 145 |
aws_condition_variable_notify_one(&retry_data->cvar); |
| 146 |
} |
| 147 |
|
| 148 |
static int s_test_standard_retry_strategy_failure_exhausts_bucket(struct aws_allocator *allocator, void *ctx) { |
| 149 |
(void)allocator; |
| 150 |
|
| 151 |
struct exponential_backoff_test_data *test_data = ctx; |
| 152 |
|
| 153 |
struct retry_data retry_data = { |
| 154 |
.mutex = AWS_MUTEX_INIT, |
| 155 |
.cvar = AWS_CONDITION_VARIABLE_INIT, |
| 156 |
}; |
| 157 |
|
| 158 |
struct retry_data retry_data_dup_same_partition = { |
| 159 |
.mutex = AWS_MUTEX_INIT, |
| 160 |
.cvar = AWS_CONDITION_VARIABLE_INIT, |
| 161 |
}; |
| 162 |
|
| 163 |
struct aws_byte_cursor partition = aws_byte_cursor_from_c_str("us-east-1:super-badly-named-aws-service"); |
| 164 |
|
| 165 |
ASSERT_SUCCESS(aws_mutex_lock(&retry_data.mutex)); |
| 166 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 167 |
test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data, 0)); |
| 168 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 169 |
&retry_data.cvar, &retry_data.mutex, s_retry_token_acquisition_completed, &retry_data)); |
| 170 |
|
| 171 |
ASSERT_PTR_EQUALS(test_data->retry_strategy, retry_data.retry_strategy); |
| 172 |
ASSERT_NOT_NULL(retry_data.retry_token); |
| 173 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, retry_data.token_acquisition_error_code); |
| 174 |
|
| 175 |
aws_mutex_unlock(&retry_data.mutex); |
| 176 |
|
| 177 |
ASSERT_SUCCESS(aws_mutex_lock(&retry_data_dup_same_partition.mutex)); |
| 178 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 179 |
test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data_dup_same_partition, 0)); |
| 180 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 181 |
&retry_data_dup_same_partition.cvar, |
| 182 |
&retry_data_dup_same_partition.mutex, |
| 183 |
s_retry_token_acquisition_completed, |
| 184 |
&retry_data_dup_same_partition)); |
| 185 |
|
| 186 |
ASSERT_PTR_EQUALS(test_data->retry_strategy, retry_data_dup_same_partition.retry_strategy); |
| 187 |
ASSERT_NOT_NULL(retry_data_dup_same_partition.retry_token); |
| 188 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, retry_data_dup_same_partition.token_acquisition_error_code); |
| 189 |
aws_mutex_unlock(&retry_data_dup_same_partition.mutex); |
| 190 |
|
| 191 |
|
| 192 |
aws_mutex_lock(&retry_data.mutex); |
| 193 |
ASSERT_SUCCESS(aws_retry_strategy_schedule_retry( |
| 194 |
retry_data.retry_token, AWS_RETRY_ERROR_TYPE_TRANSIENT, s_on_retry_ready, &retry_data)); |
| 195 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 196 |
&retry_data.cvar, &retry_data.mutex, s_retry_ready_completion_predicate, &retry_data)); |
| 197 |
|
| 198 |
ASSERT_PTR_EQUALS(retry_data.retry_token, retry_data.schedule_token_value); |
| 199 |
ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, retry_data.schedule_retry_error_code); |
| 200 |
retry_data.schedule_retry_error_code = 0; |
| 201 |
retry_data.schedule_token_value = NULL; |
| 202 |
|
| 203 |
aws_mutex_unlock(&retry_data.mutex); |
| 204 |
|
| 205 |
|
| 206 |
aws_mutex_lock(&retry_data_dup_same_partition.mutex); |
| 207 |
ASSERT_SUCCESS(aws_retry_strategy_schedule_retry( |
| 208 |
retry_data_dup_same_partition.retry_token, |
| 209 |
AWS_RETRY_ERROR_TYPE_SERVER_ERROR, |
| 210 |
s_on_retry_ready, |
| 211 |
&retry_data_dup_same_partition)); |
| 212 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 213 |
&retry_data_dup_same_partition.cvar, |
| 214 |
&retry_data_dup_same_partition.mutex, |
| 215 |
s_retry_ready_completion_predicate, |
| 216 |
&retry_data_dup_same_partition)); |
| 217 |
|
| 218 |
ASSERT_PTR_EQUALS(retry_data_dup_same_partition.retry_token, retry_data_dup_same_partition.schedule_token_value); |
| 219 |
ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, retry_data_dup_same_partition.schedule_retry_error_code); |
| 220 |
retry_data_dup_same_partition.schedule_retry_error_code = 0; |
| 221 |
retry_data_dup_same_partition.schedule_token_value = NULL; |
| 222 |
|
| 223 |
|
| 224 |
ASSERT_ERROR( |
| 225 |
AWS_IO_RETRY_PERMISSION_DENIED, |
| 226 |
aws_retry_strategy_schedule_retry( |
| 227 |
retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data)); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
ASSERT_ERROR( |
| 232 |
AWS_IO_RETRY_PERMISSION_DENIED, |
| 233 |
aws_retry_strategy_schedule_retry( |
| 234 |
retry_data_dup_same_partition.retry_token, |
| 235 |
AWS_RETRY_ERROR_TYPE_SERVER_ERROR, |
| 236 |
s_on_retry_ready, |
| 237 |
&retry_data_dup_same_partition)); |
| 238 |
|
| 239 |
aws_retry_token_release(retry_data_dup_same_partition.retry_token); |
| 240 |
aws_retry_token_release(retry_data.retry_token); |
| 241 |
|
| 242 |
ASSERT_SUCCESS(aws_mutex_unlock(&retry_data_dup_same_partition.mutex)); |
| 243 |
|
| 244 |
|
| 245 |
struct retry_data separate_partition = { |
| 246 |
.mutex = AWS_MUTEX_INIT, |
| 247 |
.cvar = AWS_CONDITION_VARIABLE_INIT, |
| 248 |
}; |
| 249 |
|
| 250 |
ASSERT_SUCCESS(aws_mutex_lock(&separate_partition.mutex)); |
| 251 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 252 |
test_data->retry_strategy, NULL, s_on_retry_token_acquired, &separate_partition, 0)); |
| 253 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 254 |
&separate_partition.cvar, &separate_partition.mutex, s_retry_token_acquisition_completed, &separate_partition)); |
| 255 |
|
| 256 |
ASSERT_PTR_EQUALS(test_data->retry_strategy, separate_partition.retry_strategy); |
| 257 |
ASSERT_NOT_NULL(separate_partition.retry_token); |
| 258 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, separate_partition.token_acquisition_error_code); |
| 259 |
|
| 260 |
ASSERT_SUCCESS(aws_retry_strategy_schedule_retry( |
| 261 |
separate_partition.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &separate_partition)); |
| 262 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 263 |
&separate_partition.cvar, &separate_partition.mutex, s_retry_ready_completion_predicate, &separate_partition)); |
| 264 |
|
| 265 |
ASSERT_PTR_EQUALS(separate_partition.retry_token, separate_partition.schedule_token_value); |
| 266 |
ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, separate_partition.schedule_retry_error_code); |
| 267 |
|
| 268 |
aws_retry_token_release(separate_partition.retry_token); |
| 269 |
|
| 270 |
ASSERT_SUCCESS(aws_mutex_unlock(&separate_partition.mutex)); |
| 271 |
|
| 272 |
return AWS_OP_SUCCESS; |
| 273 |
} |
| 274 |
AWS_TEST_CASE_FIXTURE( |
| 275 |
test_standard_retry_strategy_failure_exhausts_bucket, |
| 276 |
s_fixture_setup, |
| 277 |
s_test_standard_retry_strategy_failure_exhausts_bucket, |
| 278 |
s_fixture_shutdown, |
| 279 |
&s_fixture_test_data); |
| 280 |
|
| 281 |
static int s_test_standard_retry_strategy_failure_recovers(struct aws_allocator *allocator, void *ctx) { |
| 282 |
(void)allocator; |
| 283 |
|
| 284 |
struct exponential_backoff_test_data *test_data = ctx; |
| 285 |
|
| 286 |
struct retry_data retry_data = { |
| 287 |
.mutex = AWS_MUTEX_INIT, |
| 288 |
.cvar = AWS_CONDITION_VARIABLE_INIT, |
| 289 |
}; |
| 290 |
|
| 291 |
struct aws_byte_cursor partition = |
| 292 |
aws_byte_cursor_from_c_str("us-west-2:elastic-something-something-manager-manager"); |
| 293 |
|
| 294 |
ASSERT_SUCCESS(aws_mutex_lock(&retry_data.mutex)); |
| 295 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 296 |
test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data, 0)); |
| 297 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 298 |
&retry_data.cvar, &retry_data.mutex, s_retry_token_acquisition_completed, &retry_data)); |
| 299 |
|
| 300 |
ASSERT_PTR_EQUALS(test_data->retry_strategy, retry_data.retry_strategy); |
| 301 |
ASSERT_NOT_NULL(retry_data.retry_token); |
| 302 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, retry_data.token_acquisition_error_code); |
| 303 |
|
| 304 |
|
| 305 |
ASSERT_SUCCESS(aws_retry_strategy_schedule_retry( |
| 306 |
retry_data.retry_token, AWS_RETRY_ERROR_TYPE_TRANSIENT, s_on_retry_ready, &retry_data)); |
| 307 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 308 |
&retry_data.cvar, &retry_data.mutex, s_retry_ready_completion_predicate, &retry_data)); |
| 309 |
|
| 310 |
ASSERT_PTR_EQUALS(retry_data.retry_token, retry_data.schedule_token_value); |
| 311 |
ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, retry_data.schedule_retry_error_code); |
| 312 |
retry_data.schedule_retry_error_code = 0; |
| 313 |
retry_data.schedule_token_value = NULL; |
| 314 |
|
| 315 |
|
| 316 |
ASSERT_SUCCESS(aws_retry_strategy_schedule_retry( |
| 317 |
retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data)); |
| 318 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 319 |
&retry_data.cvar, &retry_data.mutex, s_retry_ready_completion_predicate, &retry_data)); |
| 320 |
|
| 321 |
ASSERT_PTR_EQUALS(retry_data.retry_token, retry_data.schedule_token_value); |
| 322 |
ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, retry_data.schedule_retry_error_code); |
| 323 |
retry_data.schedule_retry_error_code = 0; |
| 324 |
retry_data.schedule_token_value = NULL; |
| 325 |
|
| 326 |
|
| 327 |
ASSERT_ERROR( |
| 328 |
AWS_IO_RETRY_PERMISSION_DENIED, |
| 329 |
aws_retry_strategy_schedule_retry( |
| 330 |
retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data)); |
| 331 |
|
| 332 |
aws_retry_token_release(retry_data.retry_token); |
| 333 |
|
| 334 |
int i = 0; |
| 335 |
|
| 336 |
while (i < 5) { |
| 337 |
retry_data.token_acquisition_error_code = 0; |
| 338 |
retry_data.schedule_retry_error_code = 0; |
| 339 |
retry_data.schedule_token_value = NULL; |
| 340 |
retry_data.retry_token = NULL; |
| 341 |
|
| 342 |
|
| 343 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 344 |
test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data, 0)); |
| 345 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 346 |
&retry_data.cvar, &retry_data.mutex, s_retry_token_acquisition_completed, &retry_data)); |
| 347 |
|
| 348 |
ASSERT_SUCCESS(aws_retry_token_record_success(retry_data.retry_token)); |
| 349 |
aws_retry_token_release(retry_data.retry_token); |
| 350 |
i++; |
| 351 |
} |
| 352 |
|
| 353 |
retry_data.token_acquisition_error_code = 0; |
| 354 |
retry_data.schedule_retry_error_code = 0; |
| 355 |
retry_data.schedule_token_value = NULL; |
| 356 |
retry_data.retry_token = NULL; |
| 357 |
|
| 358 |
|
| 359 |
ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token( |
| 360 |
test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data, 0)); |
| 361 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 362 |
&retry_data.cvar, &retry_data.mutex, s_retry_token_acquisition_completed, &retry_data)); |
| 363 |
|
| 364 |
|
| 365 |
ASSERT_SUCCESS(aws_retry_strategy_schedule_retry( |
| 366 |
retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data)); |
| 367 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 368 |
&retry_data.cvar, &retry_data.mutex, s_retry_ready_completion_predicate, &retry_data)); |
| 369 |
|
| 370 |
|
| 371 |
ASSERT_ERROR( |
| 372 |
AWS_IO_RETRY_PERMISSION_DENIED, |
| 373 |
aws_retry_strategy_schedule_retry( |
| 374 |
retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data)); |
| 375 |
|
| 376 |
aws_retry_token_release(retry_data.retry_token); |
| 377 |
ASSERT_SUCCESS(aws_mutex_unlock(&retry_data.mutex)); |
| 378 |
|
| 379 |
return AWS_OP_SUCCESS; |
| 380 |
} |
| 381 |
AWS_TEST_CASE_FIXTURE( |
| 382 |
test_standard_retry_strategy_failure_recovers, |
| 383 |
s_fixture_setup, |
| 384 |
s_test_standard_retry_strategy_failure_recovers, |
| 385 |
s_fixture_shutdown, |
| 386 |
&s_fixture_test_data); |