File: standard_retry_test.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (380 lines) | stat: -rw-r--r-- 15,799 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <aws/io/retry_strategy.h>

#include <aws/testing/aws_test_harness.h>

#include <aws/common/clock.h>
#include <aws/common/condition_variable.h>

#include <aws/io/event_loop.h>

struct exponential_backoff_test_data {
    size_t retry_count;
    size_t client_error_count;
    struct aws_event_loop_group *el_group;
    struct aws_retry_strategy *retry_strategy;
    int failure_error_code;
    struct aws_mutex mutex;
    struct aws_condition_variable cvar;
    bool el_group_shutdown;
};

static struct exponential_backoff_test_data s_fixture_test_data = {
    .cvar = AWS_CONDITION_VARIABLE_INIT,
    .mutex = AWS_MUTEX_INIT,
};

static void s_el_group_completion_callback(void *arg) {
    struct exponential_backoff_test_data *test_data = arg;

    aws_mutex_lock(&test_data->mutex);
    test_data->el_group_shutdown = true;
    aws_mutex_unlock(&test_data->mutex);
    aws_condition_variable_notify_one(&test_data->cvar);
}

static bool s_el_group_shutdown_predicate(void *arg) {
    struct exponential_backoff_test_data *test_data = arg;
    return test_data->el_group_shutdown;
}

static int s_fixture_setup(struct aws_allocator *allocator, void *ctx) {
    aws_io_library_init(allocator);
    struct exponential_backoff_test_data *test_data = ctx;
    struct aws_shutdown_callback_options shutdown_options = {
        .shutdown_callback_fn = s_el_group_completion_callback,
        .shutdown_callback_user_data = ctx,
    };

    test_data->el_group = aws_event_loop_group_new_default(allocator, 1, &shutdown_options);
    ASSERT_NOT_NULL(test_data->el_group);
    struct aws_standard_retry_options retry_options = {
        .initial_bucket_capacity = 15,
        .backoff_retry_options =
            {
                .el_group = test_data->el_group,
            },
    };
    test_data->retry_strategy = aws_retry_strategy_new_standard(allocator, &retry_options);
    ASSERT_NOT_NULL(test_data->retry_strategy);

    return AWS_OP_SUCCESS;
}

static int s_fixture_shutdown(struct aws_allocator *allocator, int setup_error_code, void *ctx) {
    (void)allocator;

    if (!setup_error_code) {
        struct exponential_backoff_test_data *test_data = ctx;

        aws_mutex_lock(&test_data->mutex);
        aws_retry_strategy_release(test_data->retry_strategy);
        aws_event_loop_group_release(test_data->el_group);
        aws_condition_variable_wait_pred(&test_data->cvar, &test_data->mutex, s_el_group_shutdown_predicate, ctx);
        aws_mutex_unlock(&test_data->mutex);
    }

    aws_io_library_clean_up();

    return AWS_OP_SUCCESS;
}

static int s_test_standard_retry_strategy_setup_shutdown(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;
    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE_FIXTURE(
    test_standard_retry_strategy_setup_shutdown,
    s_fixture_setup,
    s_test_standard_retry_strategy_setup_shutdown,
    s_fixture_shutdown,
    &s_fixture_test_data);

struct retry_data {
    struct aws_retry_token *retry_token;
    struct aws_retry_strategy *retry_strategy;
    struct aws_mutex mutex;
    struct aws_condition_variable cvar;
    int token_acquisition_error_code;
    int schedule_retry_error_code;
    struct aws_retry_token *schedule_token_value;
};

static bool s_retry_token_acquisition_completed(void *arg) {
    struct retry_data *retry_data = arg;
    return retry_data->retry_token || retry_data->token_acquisition_error_code;
}

static void s_on_retry_token_acquired(
    struct aws_retry_strategy *retry_strategy,
    int error_code,
    struct aws_retry_token *token,
    void *user_data) {

    struct retry_data *retry_data = user_data;
    aws_mutex_lock(&retry_data->mutex);
    retry_data->retry_token = token;
    retry_data->token_acquisition_error_code = error_code;
    retry_data->retry_strategy = retry_strategy;
    aws_mutex_unlock(&retry_data->mutex);
    aws_condition_variable_notify_one(&retry_data->cvar);
}

static bool s_retry_ready_completion_predicate(void *arg) {
    struct retry_data *retry_data = arg;
    return retry_data->schedule_retry_error_code || retry_data->schedule_token_value;
}

static void s_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data) {
    struct retry_data *retry_data = user_data;
    aws_mutex_lock(&retry_data->mutex);
    retry_data->schedule_retry_error_code = error_code;
    retry_data->schedule_token_value = token;
    aws_mutex_unlock(&retry_data->mutex);
    aws_condition_variable_notify_one(&retry_data->cvar);
}

static int s_test_standard_retry_strategy_failure_exhausts_bucket(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;

    struct exponential_backoff_test_data *test_data = ctx;

    struct retry_data retry_data = {
        .mutex = AWS_MUTEX_INIT,
        .cvar = AWS_CONDITION_VARIABLE_INIT,
    };

    struct retry_data retry_data_dup_same_partition = {
        .mutex = AWS_MUTEX_INIT,
        .cvar = AWS_CONDITION_VARIABLE_INIT,
    };

    struct aws_byte_cursor partition = aws_byte_cursor_from_c_str("us-east-1:super-badly-named-aws-service");

    ASSERT_SUCCESS(aws_mutex_lock(&retry_data.mutex));
    ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token(
        test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data, 0));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &retry_data.cvar, &retry_data.mutex, s_retry_token_acquisition_completed, &retry_data));

    ASSERT_PTR_EQUALS(test_data->retry_strategy, retry_data.retry_strategy);
    ASSERT_NOT_NULL(retry_data.retry_token);
    ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, retry_data.token_acquisition_error_code);

    aws_mutex_unlock(&retry_data.mutex);
    /* do a duplicate partition, this should take a different path since the bucket already exists. */
    ASSERT_SUCCESS(aws_mutex_lock(&retry_data_dup_same_partition.mutex));
    ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token(
        test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data_dup_same_partition, 0));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &retry_data_dup_same_partition.cvar,
        &retry_data_dup_same_partition.mutex,
        s_retry_token_acquisition_completed,
        &retry_data_dup_same_partition));

    ASSERT_PTR_EQUALS(test_data->retry_strategy, retry_data_dup_same_partition.retry_strategy);
    ASSERT_NOT_NULL(retry_data_dup_same_partition.retry_token);
    ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, retry_data_dup_same_partition.token_acquisition_error_code);
    aws_mutex_unlock(&retry_data_dup_same_partition.mutex);

    /* should deduct 10 from capacity */
    aws_mutex_lock(&retry_data.mutex);
    ASSERT_SUCCESS(aws_retry_strategy_schedule_retry(
        retry_data.retry_token, AWS_RETRY_ERROR_TYPE_TRANSIENT, s_on_retry_ready, &retry_data));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &retry_data.cvar, &retry_data.mutex, s_retry_ready_completion_predicate, &retry_data));

    ASSERT_PTR_EQUALS(retry_data.retry_token, retry_data.schedule_token_value);
    ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, retry_data.schedule_retry_error_code);
    retry_data.schedule_retry_error_code = 0;
    retry_data.schedule_token_value = NULL;

    aws_mutex_unlock(&retry_data.mutex);

    /* should deduct 5 from capacity from a different token but the same partition */
    aws_mutex_lock(&retry_data_dup_same_partition.mutex);
    ASSERT_SUCCESS(aws_retry_strategy_schedule_retry(
        retry_data_dup_same_partition.retry_token,
        AWS_RETRY_ERROR_TYPE_SERVER_ERROR,
        s_on_retry_ready,
        &retry_data_dup_same_partition));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &retry_data_dup_same_partition.cvar,
        &retry_data_dup_same_partition.mutex,
        s_retry_ready_completion_predicate,
        &retry_data_dup_same_partition));

    ASSERT_PTR_EQUALS(retry_data_dup_same_partition.retry_token, retry_data_dup_same_partition.schedule_token_value);
    ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, retry_data_dup_same_partition.schedule_retry_error_code);
    retry_data_dup_same_partition.schedule_retry_error_code = 0;
    retry_data_dup_same_partition.schedule_token_value = NULL;

    /* this should fail. Partition capacity was 15, we've deducted 15 already, even though 3 retries were permitted. */
    ASSERT_ERROR(
        AWS_IO_RETRY_PERMISSION_DENIED,
        aws_retry_strategy_schedule_retry(
            retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data));

    /* this should fail too even though it's a separate token, they're using the same bucket. Partition capacity was 15,
     * we've deducted 15 already, even though 3 retries were permitted. */
    ASSERT_ERROR(
        AWS_IO_RETRY_PERMISSION_DENIED,
        aws_retry_strategy_schedule_retry(
            retry_data_dup_same_partition.retry_token,
            AWS_RETRY_ERROR_TYPE_SERVER_ERROR,
            s_on_retry_ready,
            &retry_data_dup_same_partition));

    aws_retry_token_release(retry_data_dup_same_partition.retry_token);
    aws_retry_token_release(retry_data.retry_token);

    ASSERT_SUCCESS(aws_mutex_unlock(&retry_data_dup_same_partition.mutex));

    /* verify it doesn't affect other partitions */
    struct retry_data separate_partition = {
        .mutex = AWS_MUTEX_INIT,
        .cvar = AWS_CONDITION_VARIABLE_INIT,
    };

    ASSERT_SUCCESS(aws_mutex_lock(&separate_partition.mutex));
    ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token(
        test_data->retry_strategy, NULL, s_on_retry_token_acquired, &separate_partition, 0));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &separate_partition.cvar, &separate_partition.mutex, s_retry_token_acquisition_completed, &separate_partition));

    ASSERT_PTR_EQUALS(test_data->retry_strategy, separate_partition.retry_strategy);
    ASSERT_NOT_NULL(separate_partition.retry_token);
    ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, separate_partition.token_acquisition_error_code);

    ASSERT_SUCCESS(aws_retry_strategy_schedule_retry(
        separate_partition.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &separate_partition));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &separate_partition.cvar, &separate_partition.mutex, s_retry_ready_completion_predicate, &separate_partition));

    ASSERT_PTR_EQUALS(separate_partition.retry_token, separate_partition.schedule_token_value);
    ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, separate_partition.schedule_retry_error_code);

    aws_retry_token_release(separate_partition.retry_token);

    ASSERT_SUCCESS(aws_mutex_unlock(&separate_partition.mutex));

    return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
    test_standard_retry_strategy_failure_exhausts_bucket,
    s_fixture_setup,
    s_test_standard_retry_strategy_failure_exhausts_bucket,
    s_fixture_shutdown,
    &s_fixture_test_data);

static int s_test_standard_retry_strategy_failure_recovers(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;

    struct exponential_backoff_test_data *test_data = ctx;

    struct retry_data retry_data = {
        .mutex = AWS_MUTEX_INIT,
        .cvar = AWS_CONDITION_VARIABLE_INIT,
    };

    struct aws_byte_cursor partition =
        aws_byte_cursor_from_c_str("us-west-2:elastic-something-something-manager-manager");

    ASSERT_SUCCESS(aws_mutex_lock(&retry_data.mutex));
    ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token(
        test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data, 0));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &retry_data.cvar, &retry_data.mutex, s_retry_token_acquisition_completed, &retry_data));

    ASSERT_PTR_EQUALS(test_data->retry_strategy, retry_data.retry_strategy);
    ASSERT_NOT_NULL(retry_data.retry_token);
    ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, retry_data.token_acquisition_error_code);

    /* should deduct 10 from capacity */
    ASSERT_SUCCESS(aws_retry_strategy_schedule_retry(
        retry_data.retry_token, AWS_RETRY_ERROR_TYPE_TRANSIENT, s_on_retry_ready, &retry_data));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &retry_data.cvar, &retry_data.mutex, s_retry_ready_completion_predicate, &retry_data));

    ASSERT_PTR_EQUALS(retry_data.retry_token, retry_data.schedule_token_value);
    ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, retry_data.schedule_retry_error_code);
    retry_data.schedule_retry_error_code = 0;
    retry_data.schedule_token_value = NULL;

    /* should deduct 5 from capacity */
    ASSERT_SUCCESS(aws_retry_strategy_schedule_retry(
        retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &retry_data.cvar, &retry_data.mutex, s_retry_ready_completion_predicate, &retry_data));

    ASSERT_PTR_EQUALS(retry_data.retry_token, retry_data.schedule_token_value);
    ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, retry_data.schedule_retry_error_code);
    retry_data.schedule_retry_error_code = 0;
    retry_data.schedule_token_value = NULL;

    /* this should fail. Partition capacity was 15, we've deducted 15 already, even though 3 retries were permitted. */
    ASSERT_ERROR(
        AWS_IO_RETRY_PERMISSION_DENIED,
        aws_retry_strategy_schedule_retry(
            retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data));

    aws_retry_token_release(retry_data.retry_token);

    int i = 0;
    /* pay back 5 of them */
    while (i < 5) {
        retry_data.token_acquisition_error_code = 0;
        retry_data.schedule_retry_error_code = 0;
        retry_data.schedule_token_value = NULL;
        retry_data.retry_token = NULL;

        /* acquire another token */
        ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token(
            test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data, 0));
        ASSERT_SUCCESS(aws_condition_variable_wait_pred(
            &retry_data.cvar, &retry_data.mutex, s_retry_token_acquisition_completed, &retry_data));

        ASSERT_SUCCESS(aws_retry_token_record_success(retry_data.retry_token));
        aws_retry_token_release(retry_data.retry_token);
        i++;
    }

    retry_data.token_acquisition_error_code = 0;
    retry_data.schedule_retry_error_code = 0;
    retry_data.schedule_token_value = NULL;
    retry_data.retry_token = NULL;

    /* acquire another token */
    ASSERT_SUCCESS(aws_retry_strategy_acquire_retry_token(
        test_data->retry_strategy, &partition, s_on_retry_token_acquired, &retry_data, 0));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &retry_data.cvar, &retry_data.mutex, s_retry_token_acquisition_completed, &retry_data));

    /* should now succeed */
    ASSERT_SUCCESS(aws_retry_strategy_schedule_retry(
        retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data));
    ASSERT_SUCCESS(aws_condition_variable_wait_pred(
        &retry_data.cvar, &retry_data.mutex, s_retry_ready_completion_predicate, &retry_data));

    /* we only paid 5 back, make sure it fails again. */
    ASSERT_ERROR(
        AWS_IO_RETRY_PERMISSION_DENIED,
        aws_retry_strategy_schedule_retry(
            retry_data.retry_token, AWS_RETRY_ERROR_TYPE_SERVER_ERROR, s_on_retry_ready, &retry_data));

    aws_retry_token_release(retry_data.retry_token);
    ASSERT_SUCCESS(aws_mutex_unlock(&retry_data.mutex));

    return AWS_OP_SUCCESS;
}
AWS_TEST_CASE_FIXTURE(
    test_standard_retry_strategy_failure_recovers,
    s_fixture_setup,
    s_test_standard_retry_strategy_failure_recovers,
    s_fixture_shutdown,
    &s_fixture_test_data);