Debian Codegraph @

crt/aws-c-io/tests/channel_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/atomics.h>
7 #include <aws/common/clock.h>
8 #include <aws/common/condition_variable.h>
9 #include <aws/common/string.h>
10
11 #include <aws/io/channel.h>
12 #include <aws/io/channel_bootstrap.h>
13 #include <aws/io/event_loop.h>
14 #include <aws/io/private/event_loop_impl.h>
15 #include <aws/io/socket.h>
16 #include <aws/testing/aws_test_harness.h>
17
18 #include "mock_dns_resolver.h"
19 #include "read_write_test_handler.h"
20
21 struct channel_setup_test_args {
22 struct aws_mutex mutex;
23 struct aws_condition_variable condition_variable;
24 bool setup_completed; /* protected by mutex */
25 bool shutdown_completed; /* protected by mutex */
26 int error_code; /* protected by mutex */
27 bool event_loop_group_shutdown_completed; /* protected by mutex (not used by all tests) */
28 enum aws_task_status task_status;
29 };
30
31 static void s_channel_setup_test_on_setup_completed(struct aws_channel *channel, int error_code, void *user_data) {
32 (void)channel;
33 struct channel_setup_test_args *setup_test_args = (struct channel_setup_test_args *)user_data;
34
35 aws_mutex_lock(&setup_test_args->mutex);
36 setup_test_args->error_code |= error_code;
37 setup_test_args->setup_completed = true;
38 aws_mutex_unlock(&setup_test_args->mutex);
39 aws_condition_variable_notify_one(&setup_test_args->condition_variable);
40 }
41
42 static bool s_channel_setup_test_setup_completed_predicate(void *arg) {
43 struct channel_setup_test_args *setup_test_args = (struct channel_setup_test_args *)arg;
44 return setup_test_args->setup_completed;
45 }
46
47 /* Create a new channel and wait until its setup completes */
48 static int s_channel_setup_create_and_wait(
49 struct aws_allocator *allocator,
50 struct aws_channel_options *args,
51 struct channel_setup_test_args *test_args,
52 struct aws_channel **returned_channel) {
53 ASSERT_NULL(*returned_channel);
54 *returned_channel = aws_channel_new(allocator, args);
55 ASSERT_NOT_NULL(*returned_channel);
56 ASSERT_SUCCESS(aws_mutex_lock(&test_args->mutex));
57 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
58 &test_args->condition_variable, &test_args->mutex, s_channel_setup_test_setup_completed_predicate, test_args));
59 ASSERT_INT_EQUALS(0, test_args->error_code);
60 ASSERT_SUCCESS(aws_mutex_unlock(&test_args->mutex));
61 return AWS_OP_SUCCESS;
62 }
63
64 static void s_event_loop_group_on_shutdown_complete(void *user_data) {
65 struct channel_setup_test_args *setup_test_args = user_data;
66 aws_mutex_lock(&setup_test_args->mutex);
67 setup_test_args->event_loop_group_shutdown_completed = true;
68 aws_condition_variable_notify_all(&setup_test_args->condition_variable);
69 aws_mutex_unlock(&setup_test_args->mutex);
70 }
71
72 static bool s_event_loop_group_shutdown_completed_predicate(void *arg) {
73 struct channel_setup_test_args *setup_test_args = (struct channel_setup_test_args *)arg;
74 return setup_test_args->event_loop_group_shutdown_completed;
75 }
76
77 static int s_test_channel_setup(struct aws_allocator *allocator, void *ctx) {
78 (void)ctx;
79 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
80
81 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
82 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
83
84 struct aws_channel *channel_1 = NULL;
85 struct aws_channel *channel_2 = NULL;
86
87 struct channel_setup_test_args test_args = {
88 .error_code = 0,
89 .mutex = AWS_MUTEX_INIT,
90 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
91 .setup_completed = false,
92 .shutdown_completed = false,
93 };
94
95 struct aws_channel_options args = {
96 .on_setup_completed = s_channel_setup_test_on_setup_completed,
97 .setup_user_data = &test_args,
98 .on_shutdown_completed = NULL,
99 .shutdown_user_data = NULL,
100 .event_loop = event_loop,
101 };
102
103 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &args, &test_args, &channel_1));
104 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &args, &test_args, &channel_2));
105
106 aws_channel_destroy(channel_1);
107 aws_channel_destroy(channel_2);
108 aws_event_loop_destroy(event_loop);
109
110 return AWS_OP_SUCCESS;
111 }
112
113 AWS_TEST_CASE(channel_setup, s_test_channel_setup)
114
115 static int s_test_channel_single_slot_cleans_up(struct aws_allocator *allocator, void *ctx) {
116 (void)ctx;
117 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
118
119 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
120 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
121
122 struct aws_channel *channel = NULL;
123
124 struct channel_setup_test_args test_args = {
125 .error_code = 0,
126 .mutex = AWS_MUTEX_INIT,
127 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
128 .setup_completed = false,
129 .shutdown_completed = false,
130 };
131
132 struct aws_channel_options args = {
133 .on_setup_completed = s_channel_setup_test_on_setup_completed,
134 .setup_user_data = &test_args,
135 .on_shutdown_completed = NULL,
136 .shutdown_user_data = NULL,
137 .event_loop = event_loop,
138 };
139
140 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &args, &test_args, &channel));
141
142 struct aws_channel_slot *slot;
143 slot = aws_channel_slot_new(channel);
144 ASSERT_NOT_NULL(slot);
145
146 aws_channel_destroy(channel);
147 aws_event_loop_destroy(event_loop);
148
149 return AWS_OP_SUCCESS;
150 }
151
152 AWS_TEST_CASE(channel_single_slot_cleans_up, s_test_channel_single_slot_cleans_up)
153
154 static int s_test_channel_slots_clean_up(struct aws_allocator *allocator, void *ctx) {
155 (void)ctx;
156 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
157
158 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
159 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
160
161 struct aws_channel *channel = NULL;
162
163 struct channel_setup_test_args test_args = {
164 .error_code = 0,
165 .mutex = AWS_MUTEX_INIT,
166 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
167 .setup_completed = false,
168 .shutdown_completed = false,
169 };
170
171 struct aws_channel_options args = {
172 .on_setup_completed = s_channel_setup_test_on_setup_completed,
173 .setup_user_data = &test_args,
174 .on_shutdown_completed = NULL,
175 .shutdown_user_data = NULL,
176 .event_loop = event_loop,
177 };
178
179 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &args, &test_args, &channel));
180
181 struct aws_channel_slot *slot_1, *slot_2, *slot_3, *slot_4, *slot_5;
182 slot_1 = aws_channel_slot_new(channel);
183 slot_2 = aws_channel_slot_new(channel);
184 slot_3 = aws_channel_slot_new(channel);
185 slot_4 = aws_channel_slot_new(channel);
186 slot_5 = aws_channel_slot_new(channel);
187
188 ASSERT_NOT_NULL(slot_1);
189 ASSERT_NOT_NULL(slot_2);
190 ASSERT_NOT_NULL(slot_3);
191 ASSERT_NOT_NULL(slot_4);
192 ASSERT_NOT_NULL(slot_5);
193
194 ASSERT_SUCCESS(aws_channel_slot_insert_right(slot_1, slot_2));
195 ASSERT_SUCCESS(aws_channel_slot_insert_right(slot_2, slot_3));
196 ASSERT_SUCCESS(aws_channel_slot_insert_left(slot_3, slot_4));
197 ASSERT_SUCCESS(aws_channel_slot_remove(slot_2));
198
199 ASSERT_PTR_EQUALS(slot_1, slot_4->adj_left);
200 ASSERT_PTR_EQUALS(slot_1->adj_right, slot_4);
201 ASSERT_PTR_EQUALS(slot_4->adj_left, slot_1);
202 ASSERT_NULL(slot_1->adj_left);
203
204 ASSERT_PTR_EQUALS(slot_4, slot_3->adj_left);
205 ASSERT_PTR_EQUALS(slot_4->adj_right, slot_3);
206 ASSERT_PTR_EQUALS(slot_3->adj_left, slot_4);
207 ASSERT_NULL(slot_3->adj_right);
208
209 ASSERT_SUCCESS(aws_channel_slot_replace(slot_4, slot_5));
210 ASSERT_PTR_EQUALS(slot_1, slot_5->adj_left);
211 ASSERT_PTR_EQUALS(slot_1->adj_right, slot_5);
212 ASSERT_PTR_EQUALS(slot_5->adj_left, slot_1);
213
214 ASSERT_PTR_EQUALS(slot_5, slot_3->adj_left);
215 ASSERT_PTR_EQUALS(slot_5->adj_right, slot_3);
216 ASSERT_PTR_EQUALS(slot_3->adj_left, slot_5);
217
218 aws_channel_destroy(channel);
219 aws_event_loop_destroy(event_loop);
220
221 return AWS_OP_SUCCESS;
222 }
223
224 AWS_TEST_CASE(channel_slots_clean_up, s_test_channel_slots_clean_up)
225
226 static void s_wait_a_bit_task(struct aws_task *task, void *arg, enum aws_task_status status) {
227 (void)task;
228 (void)status;
229 struct aws_atomic_var *task_executed = arg;
230 aws_atomic_store_int(task_executed, true);
231 }
232
233 static int s_wait_a_bit(struct aws_event_loop *loop) {
234 struct aws_task task;
235 struct aws_atomic_var task_executed = AWS_ATOMIC_INIT_INT(false);
236 aws_task_init(&task, s_wait_a_bit_task, &task_executed, "wait_a_bit");
237
238 uint64_t run_at_ns;
239 ASSERT_SUCCESS(aws_event_loop_current_clock_time(loop, &run_at_ns));
240 run_at_ns += aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
241 aws_event_loop_schedule_task_future(loop, &task, run_at_ns);
242
243 while (!aws_atomic_load_int(&task_executed)) {
244 ; /* block until signaled */
245 }
246 return AWS_OP_SUCCESS;
247 }
248
249 static bool s_atomic_var_is_set_predicate(void *arg) {
250 struct aws_atomic_var *var = arg;
251 return aws_atomic_load_int(var);
252 }
253
254 static int s_test_channel_refcount(struct aws_allocator *allocator, void *ctx) {
255 (void)ctx;
256
257 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
258 ASSERT_NOT_NULL(event_loop);
259 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
260
261 /* Create channel */
262 struct channel_setup_test_args test_args = {
263 .error_code = 0,
264 .mutex = AWS_MUTEX_INIT,
265 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
266 .setup_completed = false,
267 .shutdown_completed = false,
268 };
269
270 struct aws_channel_options args = {
271 .on_setup_completed = s_channel_setup_test_on_setup_completed,
272 .setup_user_data = &test_args,
273 .on_shutdown_completed = NULL,
274 .shutdown_user_data = NULL,
275 .event_loop = event_loop,
276 };
277
278 struct aws_channel *channel = NULL;
279 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &args, &test_args, &channel));
280
281 /* Add handler to channel */
282 struct aws_channel_slot *slot = aws_channel_slot_new(channel);
283 ASSERT_NOT_NULL(slot);
284
285 struct aws_channel_handler *handler = rw_handler_new(allocator, NULL, NULL, false, 10000, NULL);
286
287 struct aws_atomic_var destroy_called = AWS_ATOMIC_INIT_INT(0);
288 struct aws_mutex destroy_mutex = AWS_MUTEX_INIT;
289 struct aws_condition_variable destroy_condition_variable = AWS_CONDITION_VARIABLE_INIT;
290 rw_handler_enable_wait_on_destroy(handler, &destroy_called, &destroy_condition_variable);
291
292 ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, handler));
293
294 /* Shut down channel */
295 ASSERT_SUCCESS(aws_channel_shutdown(channel, 0));
296
297 /* Acquire 2 holds on channel and try to destroy it. The holds should prevent memory from being freed yet */
298 aws_channel_acquire_hold(channel);
299 aws_channel_acquire_hold(channel);
300 aws_channel_destroy(channel);
301 ASSERT_SUCCESS(s_wait_a_bit(event_loop));
302 ASSERT_FALSE(aws_atomic_load_int(&destroy_called));
303
304 /* Release hold 1/2. Handler shouldn't get destroyed. */
305 aws_channel_release_hold(channel);
306 ASSERT_SUCCESS(s_wait_a_bit(event_loop));
307 ASSERT_FALSE(aws_atomic_load_int(&destroy_called));
308
309 /* Release hold 2/2. The handler and channel should be destroyed. */
310 aws_channel_release_hold(channel);
311 ASSERT_SUCCESS(aws_mutex_lock(&destroy_mutex));
312 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
313 &destroy_condition_variable, &destroy_mutex, s_atomic_var_is_set_predicate, &destroy_called));
314 ASSERT_SUCCESS(aws_mutex_unlock(&destroy_mutex));
315 ASSERT_TRUE(aws_atomic_load_int(&destroy_called));
316
317 while (!aws_atomic_load_int(&destroy_called)) {
318 ; /* block until signaled */
319 }
320
321 aws_event_loop_destroy(event_loop);
322
323 return AWS_OP_SUCCESS;
324 }
325 AWS_TEST_CASE(channel_refcount_delays_clean_up, s_test_channel_refcount)
326
327 static void s_channel_post_shutdown_task(struct aws_channel_task *task, void *arg, enum aws_task_status status) {
328 (void)task;
329
330 struct channel_setup_test_args *test_args = arg;
331 test_args->task_status = status;
332 }
333
334 static void s_channel_test_shutdown(struct aws_channel *channel, int error_code, void *user_data) {
335 (void)channel;
336 (void)error_code;
337
338 struct channel_setup_test_args *test_args = user_data;
339 aws_mutex_lock(&test_args->mutex);
340 test_args->shutdown_completed = true;
341 aws_mutex_unlock(&test_args->mutex);
342 aws_condition_variable_notify_one(&test_args->condition_variable);
343 }
344
345 static bool s_channel_test_shutdown_predicate(void *arg) {
346 struct channel_setup_test_args *test_args = (struct channel_setup_test_args *)arg;
347 return test_args->shutdown_completed;
348 }
349
350 enum tasks_run_id {
351 TASK_NOW_OFF_THREAD,
352 TASK_NOW_ON_THREAD,
353 TASK_FUTURE_OFF_THREAD,
354 TASK_FUTURE_ON_THREAD,
355 TASK_COUNT,
356 };
357
358 struct tasks_run_data {
359 struct aws_mutex mutex;
360 struct aws_condition_variable condvar;
361 bool did_task_run[TASK_COUNT];
362 bool did_task_fail[TASK_COUNT];
363 struct aws_channel_task tasks[TASK_COUNT];
364 };
365
366 static struct tasks_run_data s_tasks_run_data;
367
368 static void s_tasks_run_fn(struct aws_channel_task *task, void *arg, enum aws_task_status status) {
369 (void)task;
370 intptr_t id = (intptr_t)arg;
371
372 aws_mutex_lock(&s_tasks_run_data.mutex);
373 s_tasks_run_data.did_task_run[id] = true;
374 s_tasks_run_data.did_task_fail[id] = (status == AWS_TASK_STATUS_CANCELED);
375 aws_condition_variable_notify_one(&s_tasks_run_data.condvar);
376 aws_mutex_unlock(&s_tasks_run_data.mutex);
377 }
378
379 static void s_schedule_on_thread_tasks_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
380 (void)task;
381 (void)status;
382 struct aws_channel *channel = arg;
383 aws_channel_schedule_task_now(channel, &s_tasks_run_data.tasks[TASK_NOW_ON_THREAD]);
384 aws_channel_schedule_task_future(channel, &s_tasks_run_data.tasks[TASK_FUTURE_ON_THREAD], 1);
385 }
386
387 static bool s_tasks_run_done_pred(void *user_data) {
388 (void)user_data;
389 for (int i = 0; i < TASK_COUNT; ++i) {
390 if (!s_tasks_run_data.did_task_run[i]) {
391 return false;
392 }
393 }
394 return true;
395 }
396
397 static int s_test_channel_tasks_run_aux(
398 struct aws_allocator *allocator,
399 aws_task_fn *on_thread_invoker_fn,
400 void (*submit_now_fn)(struct aws_channel *, struct aws_channel_task *)) {
401 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
402
403 ASSERT_NOT_NULL(event_loop);
404 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
405
406 struct aws_channel *channel = NULL;
407
408 struct channel_setup_test_args test_args = {
409 .error_code = 0,
410 .mutex = AWS_MUTEX_INIT,
411 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
412 .setup_completed = false,
413 .shutdown_completed = false,
414 .task_status = 100,
415 };
416
417 struct aws_channel_options args = {
418 .on_setup_completed = s_channel_setup_test_on_setup_completed,
419 .setup_user_data = &test_args,
420 .on_shutdown_completed = s_channel_test_shutdown,
421 .shutdown_user_data = &test_args,
422 .event_loop = event_loop,
423 };
424
425 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &args, &test_args, &channel));
426
427 /* Set up tasks */
428 AWS_ZERO_STRUCT(s_tasks_run_data);
429 ASSERT_SUCCESS(aws_mutex_init(&s_tasks_run_data.mutex));
430 ASSERT_SUCCESS(aws_condition_variable_init(&s_tasks_run_data.condvar));
431 for (int i = 0; i < TASK_COUNT; ++i) {
432 aws_channel_task_init(&s_tasks_run_data.tasks[i], s_tasks_run_fn, (void *)(intptr_t)i, "test_channel_task");
433 }
434
435 /* Schedule channel-tasks from outside the channel's thread */
436 ASSERT_SUCCESS(aws_mutex_lock(&s_tasks_run_data.mutex));
437 submit_now_fn(channel, &s_tasks_run_data.tasks[TASK_NOW_OFF_THREAD]);
438 aws_channel_schedule_task_future(channel, &s_tasks_run_data.tasks[TASK_FUTURE_OFF_THREAD], 1);
439
440 /* Schedule task that schedules channel-tasks from on then channel's thread */
441 struct aws_task scheduler_task;
442 aws_task_init(&scheduler_task, on_thread_invoker_fn, channel, "schedule_on_thread_tasks");
443 aws_event_loop_schedule_task_now(event_loop, &scheduler_task);
444
445 /* Wait for all the tasks to finish */
446 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
447 &s_tasks_run_data.condvar, &s_tasks_run_data.mutex, s_tasks_run_done_pred, NULL));
448
449 /* Check that none failed */
450 bool all_succeeded = true;
451 for (int i = 0; i < TASK_COUNT; ++i) {
452 if (s_tasks_run_data.did_task_fail[i]) {
453 all_succeeded = false;
454 }
455 }
456 ASSERT_TRUE(all_succeeded);
457 ASSERT_SUCCESS(aws_mutex_unlock(&s_tasks_run_data.mutex));
458
459 aws_channel_destroy(channel);
460 aws_event_loop_destroy(event_loop);
461
462 return AWS_OP_SUCCESS;
463 }
464
465 static int s_test_channel_tasks_run(struct aws_allocator *allocator, void *ctx) {
466 (void)ctx;
467 ASSERT_SUCCESS(
468 s_test_channel_tasks_run_aux(allocator, s_schedule_on_thread_tasks_fn, aws_channel_schedule_task_now));
469
470 return AWS_OP_SUCCESS;
471 }
472
473 AWS_TEST_CASE(channel_tasks_run, s_test_channel_tasks_run);
474
475 static void s_serialized_tasks_run_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
476 (void)task;
477 (void)status;
478 struct aws_channel *channel = arg;
479 aws_channel_schedule_task_now_serialized(channel, &s_tasks_run_data.tasks[TASK_NOW_ON_THREAD]);
480 aws_channel_schedule_task_future(channel, &s_tasks_run_data.tasks[TASK_FUTURE_ON_THREAD], 1);
481 }
482
483 static int s_channel_tasks_serialized_run(struct aws_allocator *allocator, void *ctx) {
484 (void)ctx;
485 ASSERT_SUCCESS(
486 s_test_channel_tasks_run_aux(allocator, s_serialized_tasks_run_fn, aws_channel_schedule_task_now_serialized));
487
488 return AWS_OP_SUCCESS;
489 }
490
491 AWS_TEST_CASE(channel_tasks_serialized_run, s_channel_tasks_serialized_run);
492
493 static int s_test_channel_rejects_post_shutdown_tasks(struct aws_allocator *allocator, void *ctx) {
494 (void)ctx;
495 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
496
497 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
498 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
499
500 struct aws_channel *channel = NULL;
501
502 struct channel_setup_test_args test_args = {
503 .error_code = 0,
504 .mutex = AWS_MUTEX_INIT,
505 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
506 .setup_completed = false,
507 .shutdown_completed = false,
508 .task_status = 100,
509 };
510
511 struct aws_channel_options args = {
512 .on_setup_completed = s_channel_setup_test_on_setup_completed,
513 .setup_user_data = &test_args,
514 .on_shutdown_completed = s_channel_test_shutdown,
515 .shutdown_user_data = &test_args,
516 .event_loop = event_loop,
517 };
518
519 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &args, &test_args, &channel));
520
521 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
522 ASSERT_SUCCESS(aws_channel_shutdown(channel, AWS_ERROR_SUCCESS));
523 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
524 &test_args.condition_variable, &test_args.mutex, s_channel_test_shutdown_predicate, &test_args));
525 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
526
527 struct aws_channel_task task;
528 aws_channel_task_init(&task, s_channel_post_shutdown_task, &test_args, "channel_post_shutdown");
529 aws_channel_schedule_task_now(channel, &task);
530 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, test_args.task_status);
531
532 aws_channel_destroy(channel);
533 aws_event_loop_destroy(event_loop);
534
535 return AWS_OP_SUCCESS;
536 }
537
538 AWS_TEST_CASE(channel_rejects_post_shutdown_tasks, s_test_channel_rejects_post_shutdown_tasks)
539
540 static int s_test_channel_cancels_pending_tasks(struct aws_allocator *allocator, void *ctx) {
541 (void)ctx;
542 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
543
544 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
545 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
546
547 struct aws_channel *channel = NULL;
548
549 struct channel_setup_test_args test_args = {
550 .error_code = 0,
551 .mutex = AWS_MUTEX_INIT,
552 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
553 .setup_completed = false,
554 .shutdown_completed = false,
555 .task_status = 100,
556 };
557
558 struct aws_channel_options args = {
559 .on_setup_completed = s_channel_setup_test_on_setup_completed,
560 .setup_user_data = &test_args,
561 .on_shutdown_completed = s_channel_test_shutdown,
562 .shutdown_user_data = &test_args,
563 .event_loop = event_loop,
564 };
565
566 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &args, &test_args, &channel));
567
568 struct aws_channel_task task;
569 aws_channel_task_init(&task, s_channel_post_shutdown_task, &test_args, "channel_post_shutdown_cancellation");
570 /* schedule WAY in the future. */
571 aws_channel_schedule_task_future(channel, &task, UINT64_MAX - 1);
572 /* make sure it hasn't been invoked yet. */
573 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
574 ASSERT_INT_EQUALS(100, test_args.task_status);
575
576 ASSERT_SUCCESS(aws_channel_shutdown(channel, AWS_ERROR_SUCCESS));
577 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
578 &test_args.condition_variable, &test_args.mutex, s_channel_test_shutdown_predicate, &test_args));
579
580 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, test_args.task_status);
581 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
582
583 aws_channel_destroy(channel);
584 aws_event_loop_destroy(event_loop);
585
586 return AWS_OP_SUCCESS;
587 }
588
589 AWS_TEST_CASE(channel_cancels_pending_tasks, s_test_channel_cancels_pending_tasks)
590
591 static int s_test_channel_duplicate_shutdown(struct aws_allocator *allocator, void *ctx) {
592 (void)ctx;
593 struct aws_event_loop *event_loop = aws_event_loop_new_default(allocator, aws_high_res_clock_get_ticks);
594
595 ASSERT_NOT_NULL(event_loop, "Event loop creation failed with error: %s", aws_error_debug_str(aws_last_error()));
596 ASSERT_SUCCESS(aws_event_loop_run(event_loop));
597
598 struct aws_channel *channel = NULL;
599
600 struct channel_setup_test_args test_args = {
601 .error_code = 0,
602 .mutex = AWS_MUTEX_INIT,
603 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
604 .setup_completed = false,
605 .shutdown_completed = false,
606 };
607
608 struct aws_channel_options args = {
609 .on_setup_completed = s_channel_setup_test_on_setup_completed,
610 .setup_user_data = &test_args,
611 .on_shutdown_completed = s_channel_test_shutdown,
612 .shutdown_user_data = &test_args,
613 .event_loop = event_loop,
614 };
615
616 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &args, &test_args, &channel));
617
618 ASSERT_SUCCESS(aws_channel_shutdown(channel, AWS_ERROR_SUCCESS));
619 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
620 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
621 &test_args.condition_variable, &test_args.mutex, s_channel_test_shutdown_predicate, &test_args));
622 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
623
624 /* make sure this doesn't explode! */
625 ASSERT_SUCCESS(aws_channel_shutdown(channel, AWS_ERROR_SUCCESS));
626 aws_channel_destroy(channel);
627 aws_event_loop_destroy(event_loop);
628
629 return AWS_OP_SUCCESS;
630 }
631
632 AWS_TEST_CASE(channel_duplicate_shutdown, s_test_channel_duplicate_shutdown)
633
634 struct channel_connect_test_args {
635 struct aws_mutex *mutex;
636 struct aws_condition_variable cv;
637 int error_code;
638 struct aws_channel *channel;
639 bool setup;
640 bool shutdown;
641 };
642
643 static void s_test_channel_connect_some_hosts_timeout_setup(
644 struct aws_client_bootstrap *bootstrap,
645 int error_code,
646 struct aws_channel *channel,
647 void *user_data) {
648 (void)bootstrap;
649
650 struct channel_connect_test_args *test_args = user_data;
651 aws_mutex_lock(test_args->mutex);
652 test_args->setup = true;
653 test_args->channel = channel;
654 test_args->error_code = error_code;
655 aws_condition_variable_notify_one(&test_args->cv);
656 aws_mutex_unlock(test_args->mutex);
657 }
658
659 static void s_test_channel_connect_some_hosts_timeout_shutdown(
660 struct aws_client_bootstrap *bootstrap,
661 int error_code,
662 struct aws_channel *channel,
663 void *user_data) {
664 (void)bootstrap;
665 (void)channel;
666
667 struct channel_connect_test_args *test_args = user_data;
668 aws_mutex_lock(test_args->mutex);
669 test_args->channel = NULL;
670 test_args->shutdown = true;
671 test_args->error_code = error_code;
672 aws_condition_variable_notify_one(&test_args->cv);
673 aws_mutex_unlock(test_args->mutex);
674 }
675
676 static bool s_setup_complete_pred(void *user_data) {
677 struct channel_connect_test_args *test_args = user_data;
678 return test_args->setup;
679 }
680
681 static bool s_shutdown_complete_pred(void *user_data) {
682 struct channel_connect_test_args *test_args = user_data;
683 return test_args->shutdown;
684 }
685
686 static int s_test_channel_connect_some_hosts_timeout(struct aws_allocator *allocator, void *ctx) {
687 (void)ctx;
688
689 aws_io_library_init(allocator);
690
691 struct aws_mutex mutex = AWS_MUTEX_INIT;
692
693 struct channel_connect_test_args callback_data = {
694 .mutex = &mutex,
695 .cv = AWS_CONDITION_VARIABLE_INIT,
696 .error_code = 0,
697 .channel = NULL,
698 .setup = false,
699 .shutdown = false,
700 };
701
702 struct aws_event_loop_group_options elg_options = {
703 .loop_count = 1,
704 };
705 struct aws_event_loop_group *event_loop_group = aws_event_loop_group_new(allocator, &elg_options);
706
707 /* resolve our s3 test bucket and an EC2 host with an ACL that blackholes the connection */
708 const struct aws_string *addr1_ipv4 = NULL;
709 const struct aws_string *addr2_ipv4 = NULL;
710
711 struct aws_string *bh_host = aws_string_new_from_c_str(allocator, "ec2-54-158-231-48.compute-1.amazonaws.com");
712 struct aws_string *s3_host = aws_string_new_from_c_str(allocator, "aws-crt-test-stuff.s3.amazonaws.com");
713
714 struct aws_host_address *resolved_s3_address = NULL;
715 struct aws_host_address *resolved_bh_address = NULL;
716
717 /* resolve s3 test bucket */
718 struct aws_array_list s3_addresses;
719 aws_array_list_init_dynamic(&s3_addresses, allocator, 4, sizeof(struct aws_host_address));
720 aws_default_dns_resolve(allocator, s3_host, &s3_addresses, NULL);
721 const size_t s3_address_count = aws_array_list_length(&s3_addresses);
722 ASSERT_TRUE(s3_address_count >= 1);
723 /* find the first A record, ignore AAAA records */
724 for (size_t addr_idx = 0; addr_idx < s3_address_count; ++addr_idx) {
725 aws_array_list_get_at_ptr(&s3_addresses, (void *)&resolved_s3_address, addr_idx);
726 if (resolved_s3_address->record_type == AWS_ADDRESS_RECORD_TYPE_A) {
727 break;
728 }
729 }
730 ASSERT_NOT_NULL(resolved_s3_address);
731 ASSERT_INT_EQUALS(AWS_ADDRESS_RECORD_TYPE_A, resolved_s3_address->record_type, "Did not find an A record");
732 addr1_ipv4 = aws_string_new_from_string(allocator, resolved_s3_address->address);
733
734 /* resolve black hole */
735 struct aws_array_list bh_addresses;
736 aws_array_list_init_dynamic(&bh_addresses, allocator, 4, sizeof(struct aws_host_address));
737 aws_default_dns_resolve(allocator, bh_host, &bh_addresses, NULL);
738 const size_t bh_address_count = aws_array_list_length(&bh_addresses);
739 ASSERT_TRUE(bh_address_count >= 1);
740 /* find the first A record, ignore AAAA records */
741 for (size_t addr_idx = 0; addr_idx < bh_address_count; ++addr_idx) {
742 aws_array_list_get_at_ptr(&bh_addresses, (void *)&resolved_bh_address, addr_idx);
743 if (resolved_bh_address->record_type == AWS_ADDRESS_RECORD_TYPE_A) {
744 break;
745 }
746 }
747 ASSERT_NOT_NULL(resolved_bh_address);
748 ASSERT_INT_EQUALS(AWS_ADDRESS_RECORD_TYPE_A, resolved_bh_address->record_type, "Did not find an A record");
749 addr2_ipv4 = aws_string_new_from_string(allocator, resolved_bh_address->address);
750
751 /* create a resolver with 2 addresses: 1 which will always succeed, and 1 which will always timeout */
752 struct mock_dns_resolver mock_dns_resolver;
753 ASSERT_SUCCESS(mock_dns_resolver_init(&mock_dns_resolver, 2, allocator));
754
755 struct aws_host_resolution_config mock_resolver_config = {
756 .max_ttl = 1,
757 .impl = mock_dns_resolve,
758 .impl_data = &mock_dns_resolver,
759 };
760
761 struct aws_host_address host_address_1 = {
762 .address = addr1_ipv4,
763 .allocator = allocator,
764 .expiry = 0,
765 /* connections should always succeed, if not, things are worse than this unit test failing */
766 .host = s3_host,
767 .connection_failure_count = 0,
768 .record_type = AWS_ADDRESS_RECORD_TYPE_A,
769 .use_count = 0,
770 .weight = 0,
771 };
772
773 struct aws_host_address host_address_2 = {
774 .address = addr2_ipv4,
775 .allocator = allocator,
776 .expiry = 0,
777 /* same black-holed host from the timeout test, connections are a guaranteed timeout */
778 .host = bh_host,
779 .connection_failure_count = 0,
780 .record_type = AWS_ADDRESS_RECORD_TYPE_AAAA,
781 .use_count = 0,
782 .weight = 0,
783 };
784
785 struct aws_array_list address_list;
786 ASSERT_SUCCESS(aws_array_list_init_dynamic(&address_list, allocator, 2, sizeof(struct aws_host_address)));
787 ASSERT_SUCCESS(aws_array_list_push_back(&address_list, &host_address_2));
788 ASSERT_SUCCESS(aws_array_list_push_back(&address_list, &host_address_1));
789 ASSERT_SUCCESS(mock_dns_resolver_append_address_list(&mock_dns_resolver, &address_list));
790
791 struct aws_host_resolver_default_options resolver_options = {
792 .el_group = event_loop_group,
793 .max_entries = 8,
794 };
795 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
796
797 struct aws_client_bootstrap_options bootstrap_options = {
798 .event_loop_group = event_loop_group,
799 .host_resolver = resolver,
800 .host_resolution_config = &mock_resolver_config,
801 };
802
803 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
804 ASSERT_NOT_NULL(bootstrap);
805
806 struct aws_socket_options options;
807 AWS_ZERO_STRUCT(options);
808 options.connect_timeout_ms = 10000;
809 options.type = AWS_SOCKET_STREAM;
810
811 struct aws_socket_channel_bootstrap_options channel_options;
812 AWS_ZERO_STRUCT(channel_options);
813 channel_options.bootstrap = bootstrap;
814 channel_options.host_name = aws_string_c_str(s3_host);
815 channel_options.port = 80;
816 channel_options.socket_options = &options;
817 channel_options.setup_callback = s_test_channel_connect_some_hosts_timeout_setup;
818 channel_options.shutdown_callback = s_test_channel_connect_some_hosts_timeout_shutdown;
819 channel_options.user_data = &callback_data;
820
821 ASSERT_SUCCESS(aws_client_bootstrap_new_socket_channel(&channel_options));
822
823 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
824 ASSERT_SUCCESS(aws_condition_variable_wait_pred(&callback_data.cv, &mutex, s_setup_complete_pred, &callback_data));
825
826 ASSERT_INT_EQUALS(0, callback_data.error_code, aws_error_str(callback_data.error_code));
827 ASSERT_NOT_NULL(callback_data.channel);
828 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
829
830 /* this should cause a disconnect and tear down */
831 ASSERT_SUCCESS(aws_mutex_lock(&mutex));
832 ASSERT_SUCCESS(aws_channel_shutdown(callback_data.channel, AWS_OP_SUCCESS));
833 ASSERT_SUCCESS(
834 aws_condition_variable_wait_pred(&callback_data.cv, &mutex, s_shutdown_complete_pred, &callback_data));
835
836 ASSERT_INT_EQUALS(0, callback_data.error_code, aws_error_str(callback_data.error_code));
837 ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
838
839 /* clean up */
840 aws_client_bootstrap_release(bootstrap);
841 aws_host_resolver_release(resolver);
842 mock_dns_resolver_clean_up(&mock_dns_resolver);
843 aws_event_loop_group_release(event_loop_group);
844
845 for (size_t addr_idx = 0; addr_idx < s3_address_count; ++addr_idx) {
846 aws_array_list_get_at_ptr(&s3_addresses, (void *)&resolved_s3_address, addr_idx);
847 aws_host_address_clean_up(resolved_s3_address);
848 }
849 aws_array_list_clean_up(&s3_addresses);
850 for (size_t addr_idx = 0; addr_idx < bh_address_count; ++addr_idx) {
851 aws_array_list_get_at_ptr(&bh_addresses, (void *)&resolved_bh_address, addr_idx);
852 aws_host_address_clean_up(resolved_bh_address);
853 }
854 aws_array_list_clean_up(&bh_addresses);
855
856 aws_io_library_clean_up();
857
858 return 0;
859 }
860
861 AWS_TEST_CASE(channel_connect_some_hosts_timeout, s_test_channel_connect_some_hosts_timeout);
862
863 /* This is a regression test. The channel didn't used to do anything to keep the event-loop alive.
864 * So if the event-loop-group was released before the channel, the loops would get destroyed,
865 * then the channel would try to schedule its own destruction task on the loop and crash. */
866 static int s_test_channel_keeps_event_loop_group_alive(struct aws_allocator *allocator, void *ctx) {
867 (void)ctx;
868 aws_io_library_init(allocator);
869
870 struct channel_setup_test_args test_args = {
871 .mutex = AWS_MUTEX_INIT,
872 .condition_variable = AWS_CONDITION_VARIABLE_INIT,
873 };
874
875 struct aws_shutdown_callback_options event_loop_group_shutdown_options = {
876 .shutdown_callback_fn = s_event_loop_group_on_shutdown_complete,
877 .shutdown_callback_user_data = &test_args,
878 };
879 struct aws_event_loop_group *event_loop_group =
880 aws_event_loop_group_new_default(allocator, 1, &event_loop_group_shutdown_options);
881 ASSERT_NOT_NULL(event_loop_group);
882
883 struct aws_event_loop *event_loop = aws_event_loop_group_get_next_loop(event_loop_group);
884
885 struct aws_channel_options channel_options = {
886 .on_setup_completed = s_channel_setup_test_on_setup_completed,
887 .setup_user_data = &test_args,
888 .on_shutdown_completed = s_channel_test_shutdown,
889 .shutdown_user_data = &test_args,
890 .event_loop = event_loop,
891 };
892
893 struct aws_channel *channel = NULL;
894 ASSERT_SUCCESS(s_channel_setup_create_and_wait(allocator, &channel_options, &test_args, &channel));
895
896 /* shut down channel, but don't clean it up yet */
897 aws_channel_shutdown(channel, 0);
898
899 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
900 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
901 &test_args.condition_variable, &test_args.mutex, s_channel_test_shutdown_predicate, &test_args));
902 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
903
904 /* release event loop group before channel */
905 aws_event_loop_group_release(event_loop_group);
906
907 /* wait a bit to ensure the event-loop-group doesn't shut down (because channel has a hold on it) */
908 uint64_t wait_time = aws_timestamp_convert(500, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
909 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
910 ASSERT_FAILS(
911 aws_condition_variable_wait_for_pred(
912 &test_args.condition_variable,
913 &test_args.mutex,
914 wait_time,
915 s_event_loop_group_shutdown_completed_predicate,
916 &test_args),
917 "Channel failed to keep event loop alive");
918 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
919
920 /* release channel for destruction */
921 aws_channel_destroy(channel);
922
923 /* event loop group should shut down now */
924 ASSERT_SUCCESS(aws_mutex_lock(&test_args.mutex));
925 ASSERT_SUCCESS(aws_condition_variable_wait_pred(
926 &test_args.condition_variable, &test_args.mutex, s_event_loop_group_shutdown_completed_predicate, &test_args));
927 ASSERT_SUCCESS(aws_mutex_unlock(&test_args.mutex));
928
929 aws_io_library_clean_up();
930 return AWS_OP_SUCCESS;
931 }
932 AWS_TEST_CASE(channel_keeps_event_loop_group_alive, s_test_channel_keeps_event_loop_group_alive)