Debian Codegraph @

crt/aws-c-io/tests/future_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 #include <aws/io/future.h>
6
7 #include <aws/common/clock.h>
8 #include <aws/common/ref_count.h>
9 #include <aws/common/task_scheduler.h>
10 #include <aws/common/thread.h>
11 #include <aws/io/channel.h>
12 #include <aws/io/event_loop.h>
13 #include <aws/io/private/event_loop_impl.h>
14 #include <aws/testing/aws_test_harness.h>
15
16 #include "future_test.h"
17
18 #define ONE_SEC_IN_NS ((uint64_t)AWS_TIMESTAMP_NANOS)
19 #define MAX_TIMEOUT_NS (10 * ONE_SEC_IN_NS)
20
21 AWS_FUTURE_T_POINTER_WITH_DESTROY_IMPLEMENTATION(aws_future_destroyme, struct aws_destroyme, aws_destroyme_destroy);
22 AWS_FUTURE_T_POINTER_WITH_RELEASE_IMPLEMENTATION(aws_future_refcountme, struct aws_refcountme, aws_refcountme_release);
23
24 /* Run through the basics of an AWS_FUTURE_T_BY_VALUE */
25 static int s_test_future_by_value(struct aws_allocator *alloc, void *ctx) {
26 (void)ctx;
27 aws_io_library_init(alloc);
28
29 struct aws_future_bool *future = aws_future_bool_new(alloc);
30 ASSERT_NOT_NULL(future);
31
32 ASSERT_FALSE(aws_future_bool_is_done(future));
33
34 /* set result */
35 aws_future_bool_set_result(future, true);
36 ASSERT_TRUE(aws_future_bool_is_done(future));
37 ASSERT_INT_EQUALS(0, aws_future_bool_get_error(future));
38 ASSERT_TRUE(aws_future_bool_get_result(future));
39
40 future = aws_future_bool_release(future);
41 ASSERT_NULL(future);
42
43 aws_io_library_clean_up();
44 return 0;
45 }
46 AWS_TEST_CASE(future_by_value, s_test_future_by_value)
47
48 /* Run through the basics of an aws_future<void> */
49 static int s_test_future_void(struct aws_allocator *alloc, void *ctx) {
50 (void)ctx;
51 aws_io_library_init(alloc);
52
53 struct aws_future_void *future = aws_future_void_new(alloc);
54 ASSERT_NOT_NULL(future);
55
56 ASSERT_FALSE(aws_future_void_is_done(future));
57
58 /* set valueless result */
59 aws_future_void_set_result(future);
60 ASSERT_TRUE(aws_future_void_is_done(future));
61 ASSERT_INT_EQUALS(0, aws_future_void_get_error(future));
62
63 future = aws_future_void_release(future);
64 ASSERT_NULL(future);
65
66 aws_io_library_clean_up();
67 return 0;
68 }
69 AWS_TEST_CASE(future_void, s_test_future_void)
70
71 struct future_size_callback_recorder {
72 struct aws_future_size *future; /* record all state when this future's callback fires */
73 struct aws_event_loop *event_loop; /* record whether callback fires on this event-loop's thread */
74 struct aws_channel *channel;
75
76 /* record state of the world when callback invoked */
77 int error_code;
78 size_t result;
79 aws_thread_id_t thread_id;
80 bool is_event_loop_thread;
81 int invoke_count;
82 };
83
84 static void s_record_on_future_size_done(void *user_data) {
85 struct future_size_callback_recorder *recorder = user_data;
86 recorder->error_code = aws_future_size_get_error(recorder->future);
87 if (recorder->error_code == 0) {
88 recorder->result = aws_future_size_get_result(recorder->future);
89 }
90 recorder->thread_id = aws_thread_current_thread_id();
91 recorder->invoke_count++;
92
93 if (recorder->event_loop) {
94 recorder->is_event_loop_thread = aws_event_loop_thread_is_callers_thread(recorder->event_loop);
95 }
96 }
97
98 /* Test callback firing immediately upon registration */
99 static int s_test_future_callback_fires_immediately(struct aws_allocator *alloc, void *ctx) {
100 (void)ctx;
101 aws_io_library_init(alloc);
102
103 struct future_size_callback_recorder recorder;
104 AWS_ZERO_STRUCT(recorder);
105
106 recorder.future = aws_future_size_new(alloc);
107 aws_future_size_set_result(recorder.future, 123);
108
109 aws_future_size_register_callback(recorder.future, s_record_on_future_size_done, &recorder);
110
111 /* callback should have fired immediately, on main thread, since future was already done */
112 ASSERT_INT_EQUALS(1, recorder.invoke_count);
113 ASSERT_INT_EQUALS(0, recorder.error_code);
114 ASSERT_UINT_EQUALS(123, recorder.result);
115
116 aws_thread_id_t main_thread_id = aws_thread_current_thread_id();
117 ASSERT_INT_EQUALS(0, memcmp(&main_thread_id, &recorder.thread_id, sizeof(aws_thread_id_t)));
118
119 aws_future_size_release(recorder.future);
120 aws_io_library_clean_up();
121 return 0;
122 }
123 AWS_TEST_CASE(future_callback_fires_immediately, s_test_future_callback_fires_immediately);
124
125 struct future_size_thread_job {
126 struct aws_allocator *alloc;
127 uint64_t delay_ns;
128 struct aws_future_size *my_future;
129 };
130
131 /* Function that runs on thread, and completes future after delay */
132 static void s_run_thread_job(void *user_data) {
133 struct future_size_thread_job *job = user_data;
134
135 aws_thread_current_sleep(job->delay_ns);
136 aws_future_size_set_result(job->my_future, 987);
137
138 aws_future_size_release(job->my_future);
139 aws_mem_release(job->alloc, job);
140 }
141
142 /* Start thread that will complete future after delay */
143 static struct aws_future_size *s_start_thread_job(struct aws_allocator *alloc, uint64_t delay_ns) {
144 struct aws_future_size *future = aws_future_size_new(alloc);
145
146 struct future_size_thread_job *job = aws_mem_calloc(alloc, 1, sizeof(struct future_size_thread_job));
147 job->alloc = alloc;
148 job->delay_ns = delay_ns;
149 job->my_future = aws_future_size_acquire(future);
150
151 struct aws_thread thread;
152 AWS_FATAL_ASSERT(aws_thread_init(&thread, alloc) == AWS_OP_SUCCESS);
153
154 struct aws_thread_options thread_options = *aws_default_thread_options();
155 thread_options.join_strategy = AWS_TJS_MANAGED;
156 thread_options.name = aws_byte_cursor_from_c_str("FutureSizeJob");
157
158 AWS_FATAL_ASSERT(aws_thread_launch(&thread, s_run_thread_job, job, &thread_options) == AWS_OP_SUCCESS);
159
160 return future;
161 }
162
163 /* Test callback firing on a different thread than the one that registered it.
164 * This is the first test that looks like real-world use of aws_future */
165 static int s_test_future_callback_fires_on_another_thread(struct aws_allocator *alloc, void *ctx) {
166 (void)ctx;
167 aws_io_library_init(alloc);
168
169 /* Kick off thread, which will set result in 1sec */
170 struct future_size_callback_recorder recorder = {
171 .future = s_start_thread_job(alloc, ONE_SEC_IN_NS /*delay_ns*/),
172 };
173
174 aws_future_size_register_callback(recorder.future, s_record_on_future_size_done, &recorder);
175
176 /* Wait until other thread joins, at which point the future is complete and the callback has fired */
177 aws_thread_set_managed_join_timeout_ns(MAX_TIMEOUT_NS);
178 ASSERT_SUCCESS(aws_thread_join_all_managed());
179
180 /* callback should have fired on the other thread */
181 ASSERT_INT_EQUALS(1, recorder.invoke_count);
182 ASSERT_INT_EQUALS(0, recorder.error_code);
183 ASSERT_UINT_EQUALS(987, recorder.result);
184
185 aws_thread_id_t main_thread_id = aws_thread_current_thread_id();
186 ASSERT_TRUE(memcmp(&main_thread_id, &recorder.thread_id, sizeof(aws_thread_id_t)) != 0);
187
188 aws_future_size_release(recorder.future);
189 aws_io_library_clean_up();
190 return 0;
191 }
192 AWS_TEST_CASE(future_callback_fires_on_another_thread, s_test_future_callback_fires_on_another_thread);
193
194 static int s_test_future_register_callback_if_not_done(struct aws_allocator *alloc, void *ctx) {
195 (void)ctx;
196 aws_io_library_init(alloc);
197
198 {
199 /* the callback should not get registered if future is already done */
200 struct future_size_callback_recorder recorder = {
201 .future = aws_future_size_new(alloc),
202 };
203 aws_future_size_set_result(recorder.future, 555);
204 ASSERT_FALSE(
205 aws_future_size_register_callback_if_not_done(recorder.future, s_record_on_future_size_done, &recorder));
206
207 ASSERT_INT_EQUALS(0, recorder.invoke_count);
208 aws_future_size_release(recorder.future);
209 }
210
211 {
212 /* the callback should get registered if the future isn't done yet */
213 struct future_size_callback_recorder recorder = {
214 .future = aws_future_size_new(alloc),
215 };
216 ASSERT_TRUE(
217 aws_future_size_register_callback_if_not_done(recorder.future, s_record_on_future_size_done, &recorder));
218 ASSERT_INT_EQUALS(0, recorder.invoke_count);
219
220 /* now set result, the callback should fire */
221 aws_future_size_set_result(recorder.future, 555);
222 ASSERT_INT_EQUALS(1, recorder.invoke_count);
223
224 /* after callback fires, you're allowed to call register_callback_if_not_done() again.
225 * (This makes it easy to call an async function repeatedly in a loop,
226 * where you keep looping as long as the futures complete immediately,
227 * but bail out if the callback gets registered) */
228 ASSERT_FALSE(
229 aws_future_size_register_callback_if_not_done(recorder.future, s_record_on_future_size_done, &recorder));
230
231 /* make sure callback didn't fire a 2nd time */
232 ASSERT_INT_EQUALS(1, recorder.invoke_count);
233
234 aws_future_size_release(recorder.future);
235 }
236 aws_io_library_clean_up();
237 return 0;
238 }
239 AWS_TEST_CASE(future_register_callback_if_not_done, s_test_future_register_callback_if_not_done)
240
241 /* Test that an event-loop callback still runs if it's registered after the future is already done */
242 static int s_test_future_register_event_loop_callback_after_done(struct aws_allocator *alloc, void *ctx) {
243 (void)ctx;
244 aws_io_library_init(alloc);
245
246 struct future_size_callback_recorder recorder = {
247 .future = aws_future_size_new(alloc),
248 .event_loop = aws_event_loop_new_default(alloc, aws_high_res_clock_get_ticks),
249 };
250 ASSERT_SUCCESS(aws_event_loop_run(recorder.event_loop));
251
252 /* register callback after result already set */
253 aws_future_size_set_result(recorder.future, 765);
254
255 aws_future_size_register_event_loop_callback(
256 recorder.future, recorder.event_loop, s_record_on_future_size_done, &recorder);
257
258 /* Wait until event loop is destroyed, at which point the future is complete and the callback has fired */
259 aws_event_loop_destroy(recorder.event_loop);
260
261 /* callback should have fired on event-loop thread */
262 ASSERT_INT_EQUALS(1, recorder.invoke_count);
263 ASSERT_INT_EQUALS(0, recorder.error_code);
264 ASSERT_UINT_EQUALS(765, recorder.result);
265 ASSERT_TRUE(recorder.is_event_loop_thread);
266
267 /* cleanup */
268 aws_future_size_release(recorder.future);
269 aws_io_library_clean_up();
270 return 0;
271 }
272 AWS_TEST_CASE(future_register_event_loop_callback_after_done, s_test_future_register_event_loop_callback_after_done)
273
274 /* Test that an event-loop callback still runs if it's registered before the future is done */
275 static int s_test_future_register_event_loop_callback_before_done(struct aws_allocator *alloc, void *ctx) {
276 (void)ctx;
277 aws_io_library_init(alloc);
278
279 struct future_size_callback_recorder recorder = {
280 .future = aws_future_size_new(alloc),
281 .event_loop = aws_event_loop_new_default(alloc, aws_high_res_clock_get_ticks),
282 };
283 ASSERT_SUCCESS(aws_event_loop_run(recorder.event_loop));
284
285 /* register callback before result is set */
286 aws_future_size_register_event_loop_callback(
287 recorder.future, recorder.event_loop, s_record_on_future_size_done, &recorder);
288
289 aws_future_size_set_result(recorder.future, 765);
290
291 /* Wait until event loop is destroyed, at which point the future is complete and the callback has fired */
292 aws_event_loop_destroy(recorder.event_loop);
293
294 /* callback should have fired on event-loop thread */
295 ASSERT_INT_EQUALS(1, recorder.invoke_count);
296 ASSERT_INT_EQUALS(0, recorder.error_code);
297 ASSERT_UINT_EQUALS(765, recorder.result);
298 ASSERT_TRUE(recorder.is_event_loop_thread);
299
300 /* cleanup */
301 aws_future_size_release(recorder.future);
302 aws_io_library_clean_up();
303 return 0;
304 }
305 AWS_TEST_CASE(future_register_event_loop_callback_before_done, s_test_future_register_event_loop_callback_before_done)
306
307 void s_set_result_from_event_loop_task(struct aws_task *task, void *user_data, enum aws_task_status status) {
308 (void)task;
309 (void)status;
310 struct future_size_callback_recorder *recorder = user_data;
311
312 AWS_FATAL_ASSERT(recorder->invoke_count == 0); /* The future shouldn't be done yet */
313
314 aws_future_size_set_result(recorder->future, 1234567);
315
316 /* The callback should NOT be invoked from the same callstack as set_result().
317 * The callback should run as its own scheduled task */
318 AWS_FATAL_ASSERT(recorder->invoke_count == 0);
319 }
320
321 /* Test that an event-loop callback always runs as its own scheduled task.
322 * Even if set_result() is called from the event-loop thread, the callback
323 * should NOT run in the same callstack as set_result() */
324 static int s_test_future_register_event_loop_callback_always_scheduled(struct aws_allocator *alloc, void *ctx) {
325 (void)ctx;
326 aws_io_library_init(alloc);
327
328 struct future_size_callback_recorder recorder = {
329 .future = aws_future_size_new(alloc),
330 .event_loop = aws_event_loop_new_default(alloc, aws_high_res_clock_get_ticks),
331 };
332 ASSERT_SUCCESS(aws_event_loop_run(recorder.event_loop));
333
334 /* register callback before result is set */
335 aws_future_size_register_event_loop_callback(
336 recorder.future, recorder.event_loop, s_record_on_future_size_done, &recorder);
337
338 struct aws_task set_result_from_event_loop_task;
339 aws_task_init(
340 &set_result_from_event_loop_task, s_set_result_from_event_loop_task, &recorder, "set_result_from_event_loop");
341
342 aws_event_loop_schedule_task_now(recorder.event_loop, &set_result_from_event_loop_task);
343
344 /* Wait until event loop is destroyed, at which point the future is complete and the callback has fired */
345 aws_event_loop_destroy(recorder.event_loop);
346
347 /* callback should have fired on event-loop thread */
348 ASSERT_INT_EQUALS(1, recorder.invoke_count);
349 ASSERT_INT_EQUALS(0, recorder.error_code);
350 ASSERT_UINT_EQUALS(1234567, recorder.result);
351 ASSERT_TRUE(recorder.is_event_loop_thread);
352
353 /* cleanup */
354 aws_future_size_release(recorder.future);
355 aws_io_library_clean_up();
356 return 0;
357 }
358 AWS_TEST_CASE(
359 future_register_event_loop_callback_always_scheduled,
360 s_test_future_register_event_loop_callback_always_scheduled)
361
362 static void s_on_channel_setup(struct aws_channel *channel, int error_code, void *user_data) {
363 (void)channel;
364 struct aws_future_void *setup_future = user_data;
365 if (error_code) {
366 aws_future_void_set_error(setup_future, error_code);
367 } else {
368 aws_future_void_set_result(setup_future);
369 }
370 }
371
372 /* Test channel callback */
373 static int s_test_future_register_channel_callback(struct aws_allocator *alloc, void *ctx) {
374 (void)ctx;
375 aws_io_library_init(alloc);
376
377 /* Set up event-loop */
378 struct future_size_callback_recorder recorder = {
379 .future = aws_future_size_new(alloc),
380 .event_loop = aws_event_loop_new_default(alloc, aws_high_res_clock_get_ticks),
381 };
382 ASSERT_SUCCESS(aws_event_loop_run(recorder.event_loop));
383
384 /* Set up channel */
385 struct aws_future_void *channel_setup_future = aws_future_void_new(alloc);
386 struct aws_channel_options channel_options = {
387 .event_loop = recorder.event_loop,
388 .on_setup_completed = s_on_channel_setup,
389 .setup_user_data = channel_setup_future,
390 };
391 struct aws_channel *channel = aws_channel_new(alloc, &channel_options);
392 ASSERT_TRUE(aws_future_void_wait(channel_setup_future, MAX_TIMEOUT_NS));
393 ASSERT_INT_EQUALS(0, aws_future_void_get_error(channel_setup_future));
394
395 /* register callback after result already set */
396 aws_future_size_set_result(recorder.future, 234567);
397
398 aws_future_size_register_channel_callback(recorder.future, channel, s_record_on_future_size_done, &recorder);
399
400 /* wait until channel/event-loop are destroyed,
401 * at which point the future is complete and the callback has fired */
402 aws_channel_release_hold(channel);
403 aws_event_loop_destroy(recorder.event_loop);
404
405 /* callback should have fired on channel/event-loop thread */
406 ASSERT_INT_EQUALS(1, recorder.invoke_count);
407 ASSERT_INT_EQUALS(0, recorder.error_code);
408 ASSERT_UINT_EQUALS(234567, recorder.result);
409 ASSERT_TRUE(recorder.is_event_loop_thread);
410
411 /* cleanup */
412 aws_future_void_release(channel_setup_future);
413 aws_future_size_release(recorder.future);
414 aws_io_library_clean_up();
415 return 0;
416 }
417 AWS_TEST_CASE(future_register_channel_callback, s_test_future_register_channel_callback);
418
419 static int s_test_future_wait_timeout(struct aws_allocator *alloc, void *ctx) {
420 (void)ctx;
421 aws_io_library_init(alloc);
422
423 struct aws_future_void *future = aws_future_void_new(alloc);
424
425 uint64_t start_ns;
426 ASSERT_SUCCESS(aws_high_res_clock_get_ticks(&start_ns));
427
428 /* The future will never complete, so this should time out and return false */
429 ASSERT_FALSE(aws_future_void_wait(future, ONE_SEC_IN_NS));
430
431 uint64_t end_ns;
432 ASSERT_SUCCESS(aws_high_res_clock_get_ticks(&end_ns));
433
434 /* Ensure that the wait actually took some time */
435 uint64_t duration_ns = end_ns - start_ns;
436 ASSERT_TRUE(duration_ns >= (uint64_t)(0.9 * ONE_SEC_IN_NS));
437
438 aws_future_void_release(future);
439 aws_io_library_clean_up();
440 return 0;
441 }
442 AWS_TEST_CASE(future_wait_timeout, s_test_future_wait_timeout)
443
444 /* This is a regression test */
445 static int s_test_future_wait_timeout_max(struct aws_allocator *alloc, void *ctx) {
446 (void)ctx;
447 aws_io_library_init(alloc);
448
449 /* Thread will complete the future in 1sec */
450 struct aws_future_size *future = s_start_thread_job(alloc, ONE_SEC_IN_NS);
451
452 /* Wait for future to complete, with timeout of UINT64_MAX.
453 * Once upon a time, there was a bug where this became a negative number and immediately timed out. */
454 bool completed_before_timeout = aws_future_size_wait(future, UINT64_MAX);
455 ASSERT_TRUE(completed_before_timeout);
456
457 /* Wait until other thread joins, at which point the future is complete and the callback has fired */
458 aws_thread_set_managed_join_timeout_ns(MAX_TIMEOUT_NS);
459 ASSERT_SUCCESS(aws_thread_join_all_managed());
460
461 aws_future_size_release(future);
462 aws_io_library_clean_up();
463 return 0;
464 }
465 AWS_TEST_CASE(future_wait_timeout_max, s_test_future_wait_timeout_max)
466
467 struct aws_destroyme {
468 struct aws_allocator *alloc;
469 bool *set_true_on_death;
470 };
471
472 struct aws_destroyme *aws_destroyme_new(struct aws_allocator *alloc, bool *set_true_on_death) {
473 struct aws_destroyme *destroyme = aws_mem_calloc(alloc, 1, sizeof(struct aws_destroyme));
474 destroyme->alloc = alloc;
475 destroyme->set_true_on_death = set_true_on_death;
476 *destroyme->set_true_on_death = false;
477 return destroyme;
478 }
479
480 void aws_destroyme_destroy(struct aws_destroyme *destroyme) {
481 AWS_FATAL_ASSERT(destroyme != NULL && "future should not call destroy() on NULL");
482 AWS_FATAL_ASSERT(*destroyme->set_true_on_death == false && "destroy() called multiple times on same object");
483 *destroyme->set_true_on_death = true;
484 aws_mem_release(destroyme->alloc, destroyme);
485 }
486
487 /* Run through the basics of an AWS_FUTURE_T_POINTER_WITH_DESTROY */
488 static int s_test_future_pointer_with_destroy(struct aws_allocator *alloc, void *ctx) {
489 (void)ctx;
490 aws_io_library_init(alloc);
491
492 struct aws_future_destroyme *future = aws_future_destroyme_new(alloc);
493 ASSERT_FALSE(aws_future_destroyme_is_done(future));
494
495 /* set result */
496 bool original_destroyme_died = false;
497 struct aws_destroyme *original_destroyme = aws_destroyme_new(alloc, &original_destroyme_died);
498 struct aws_destroyme *destroyme_pointer_copy = original_destroyme;
499 aws_future_destroyme_set_result_by_move(future, &original_destroyme);
500
501 ASSERT_NULL(original_destroyme); /* future should NULL this out while taking ownership of the result */
502 ASSERT_TRUE(aws_future_destroyme_is_done(future));
503 ASSERT_FALSE(original_destroyme_died);
504
505 /* messing with refcount shouldn't trigger destroy */
506 aws_future_destroyme_acquire(future);
507 aws_future_destroyme_release(future);
508 ASSERT_FALSE(original_destroyme_died);
509
510 /* get result (without taking ownership) */
511 struct aws_destroyme *destroyme_from_future = aws_future_destroyme_peek_result(future);
512 ASSERT_NOT_NULL(destroyme_from_future);
513 ASSERT_PTR_EQUALS(destroyme_pointer_copy, destroyme_from_future);
514 ASSERT_FALSE(original_destroyme_died);
515
516 /* result should be destroyed along with future */
517 aws_future_destroyme_release(future);
518 ASSERT_TRUE(original_destroyme_died);
519
520 aws_io_library_clean_up();
521 return 0;
522 }
523 AWS_TEST_CASE(future_pointer_with_destroy, s_test_future_pointer_with_destroy)
524
525 struct aws_refcountme {
526 struct aws_allocator *alloc;
527 struct aws_ref_count ref_count;
528 bool *set_true_on_death;
529 };
530
531 static void s_refcountme_destroy(void *user_data) {
532 struct aws_refcountme *refcountme = user_data;
533 *refcountme->set_true_on_death = true;
534 aws_mem_release(refcountme->alloc, refcountme);
535 }
536
537 struct aws_refcountme *aws_refcountme_new(struct aws_allocator *alloc, bool *set_true_on_death) {
538 struct aws_refcountme *refcountme = aws_mem_calloc(alloc, 1, sizeof(struct aws_refcountme));
539 refcountme->alloc = alloc;
540 aws_ref_count_init(&refcountme->ref_count, refcountme, s_refcountme_destroy);
541 refcountme->set_true_on_death = set_true_on_death;
542 *refcountme->set_true_on_death = false;
543 return refcountme;
544 }
545
546 struct aws_refcountme *aws_refcountme_acquire(struct aws_refcountme *refcountme) {
547 aws_ref_count_acquire(&refcountme->ref_count);
548 return refcountme;
549 }
550
551 /* Most release() functions accept NULL, but not this one, because we want to
552 * ensure that aws_future won't pass NULL to the release function */
553 struct aws_refcountme *aws_refcountme_release(struct aws_refcountme *refcountme) {
554 AWS_FATAL_ASSERT(refcountme != NULL && "future should not call release() on NULL");
555 AWS_FATAL_ASSERT(*refcountme->set_true_on_death == false && "release() called multiple times on same object");
556 *refcountme->set_true_on_death = true;
557 aws_mem_release(refcountme->alloc, refcountme);
558 return NULL;
559 }
560
561 /* Run through the basics of an AWS_FUTURE_T_POINTER_WITH_RELEASE */
562 static int s_test_future_pointer_with_release(struct aws_allocator *alloc, void *ctx) {
563 (void)ctx;
564 aws_io_library_init(alloc);
565
566 struct aws_future_refcountme *future = aws_future_refcountme_new(alloc);
567 ASSERT_FALSE(aws_future_refcountme_is_done(future));
568
569 /* set result */
570 bool original_refcountme_died = false;
571 struct aws_refcountme *original_refcountme = aws_refcountme_new(alloc, &original_refcountme_died);
572 struct aws_refcountme *refcountme_pointer_copy = original_refcountme;
573
574 aws_future_refcountme_set_result_by_move(future, &original_refcountme);
575 ASSERT_NULL(original_refcountme); /* future should NULL this out while taking ownership of the result */
576 ASSERT_TRUE(aws_future_refcountme_is_done(future));
577 ASSERT_FALSE(original_refcountme_died);
578
579 /* get result (without taking ownership) */
580 struct aws_refcountme *refcountme_from_future = aws_future_refcountme_peek_result(future);
581 ASSERT_NOT_NULL(refcountme_from_future);
582 ASSERT_PTR_EQUALS(refcountme_pointer_copy, refcountme_from_future);
583
584 /* result should be destroyed along with future */
585 aws_future_refcountme_release(future);
586 ASSERT_TRUE(original_refcountme_died);
587
588 aws_io_library_clean_up();
589 return 0;
590 }
591 AWS_TEST_CASE(future_pointer_with_release, s_test_future_pointer_with_release)
592
593 /* Test that get_result_by_move() transfers ownership */
594 static int s_test_future_get_result_by_move(struct aws_allocator *alloc, void *ctx) {
595 (void)ctx;
596 aws_io_library_init(alloc);
597
598 { /* AWS_FUTURE_T_POINTER_WITH_DESTROY */
599 bool destroyme_died = false;
600 struct aws_destroyme *original_destroyme = aws_destroyme_new(alloc, &destroyme_died);
601 struct aws_future_destroyme *future = aws_future_destroyme_new(alloc);
602 aws_future_destroyme_set_result_by_move(future, &original_destroyme);
603
604 /* transfer ownership out of future */
605 struct aws_destroyme *destroyme_from_future = aws_future_destroyme_get_result_by_move(future);
606 ASSERT_FALSE(destroyme_died);
607
608 /* result should stay alive after future is destroyed */
609 aws_future_destroyme_release(future);
610 ASSERT_FALSE(destroyme_died);
611
612 /* clean up */
613 aws_destroyme_destroy(destroyme_from_future);
614 ASSERT_TRUE(destroyme_died);
615 }
616
617 { /* AWS_FUTURE_T_POINTER_WITH_RELEASE */
618 bool refcountme_died = false;
619 struct aws_refcountme *original_refcountme = aws_refcountme_new(alloc, &refcountme_died);
620 struct aws_future_refcountme *future = aws_future_refcountme_new(alloc);
621 aws_future_refcountme_set_result_by_move(future, &original_refcountme);
622
623 /* transfer ownership out of future */
624 struct aws_refcountme *refcountme_from_future = aws_future_refcountme_get_result_by_move(future);
625 ASSERT_FALSE(refcountme_died);
626
627 /* result should stay alive after future is destroyed */
628 aws_future_refcountme_release(future);
629 ASSERT_FALSE(refcountme_died);
630
631 /* clean up */
632 aws_refcountme_release(refcountme_from_future);
633 ASSERT_TRUE(refcountme_died);
634 }
635
636 aws_io_library_clean_up();
637 return 0;
638 }
639 AWS_TEST_CASE(future_get_result_by_move, s_test_future_get_result_by_move)
640
641 /* Check that, if an incomplete future dies, the result's destructor doesn't run again.
642 * We know this works because the destructor for destroyme and refcountme will assert if NULL is passed in */
643 static int s_test_future_can_die_incomplete(struct aws_allocator *alloc, void *ctx) {
644 (void)ctx;
645 aws_io_library_init(alloc);
646
647 struct aws_future_destroyme *future_destroyme = aws_future_destroyme_new(alloc);
648 aws_future_destroyme_release(future_destroyme);
649
650 struct aws_future_refcountme *future_refcountme = aws_future_refcountme_new(alloc);
651 aws_future_refcountme_release(future_refcountme);
652
653 aws_io_library_clean_up();
654 return 0;
655 }
656 AWS_TEST_CASE(future_can_die_incomplete, s_test_future_can_die_incomplete)
657
658 /* Check aws_future<T*> will accept NULL as a result, and not consider it an error,
659 * and not try to run the result destructor. */
660 static int s_test_future_by_pointer_accepts_null_result(struct aws_allocator *alloc, void *ctx) {
661 (void)ctx;
662 {
663 struct aws_future_destroyme *future = aws_future_destroyme_new(alloc);
664 struct aws_destroyme *null_destroyme = NULL;
665 aws_future_destroyme_set_result_by_move(future, &null_destroyme);
666 ASSERT_TRUE(aws_future_destroyme_is_done(future));
667 ASSERT_INT_EQUALS(0, aws_future_destroyme_get_error(future));
668 ASSERT_NULL(aws_future_destroyme_peek_result(future));
669 aws_future_destroyme_release(future);
670 }
671 {
672 struct aws_future_refcountme *future = aws_future_refcountme_new(alloc);
673 struct aws_refcountme *null_refcountme = NULL;
674 aws_future_refcountme_set_result_by_move(future, &null_refcountme);
675 ASSERT_TRUE(aws_future_refcountme_is_done(future));
676 ASSERT_INT_EQUALS(0, aws_future_refcountme_get_error(future));
677 ASSERT_NULL(aws_future_refcountme_peek_result(future));
678 aws_future_refcountme_release(future);
679 }
680 aws_io_library_clean_up();
681 return 0;
682 }
683 AWS_TEST_CASE(future_by_pointer_accepts_null_result, s_test_future_by_pointer_accepts_null_result)
684
685 /* Check that, if an aws_future<T*> has a result set multiple times, only the 1st result sticks.
686 * Any 2nd or 3rd result will just get cleaned up. */
687 static int s_test_future_set_multiple_times(struct aws_allocator *alloc, void *ctx) {
688 (void)ctx;
689 struct aws_future_destroyme *future = aws_future_destroyme_new(alloc);
690
691 bool result1_destroyed = false;
692 struct aws_destroyme *result1 = aws_destroyme_new(alloc, &result1_destroyed);
693 struct aws_destroyme *result1_pointer_copy = result1;
694
695 bool result2_destroyed = false;
696 struct aws_destroyme *result2 = aws_destroyme_new(alloc, &result2_destroyed);
697
698 bool result3_destroyed = false;
699 struct aws_destroyme *result3 = aws_destroyme_new(alloc, &result3_destroyed);
700
701 /* the future now owns result1 */
702 aws_future_destroyme_set_result_by_move(future, &result1);
703 ASSERT_FALSE(result1_destroyed);
704
705 /* attempt to set result2.
706 * the future should continue treating result1 as the result
707 * result2 will simply be destroyed */
708 aws_future_destroyme_set_result_by_move(future, &result2);
709 ASSERT_PTR_EQUALS(result1_pointer_copy, aws_future_destroyme_peek_result(future));
710 ASSERT_FALSE(result1_destroyed);
711 ASSERT_NULL(result2);
712 ASSERT_TRUE(result2_destroyed);
713
714 /* likewise, result3 should be ignored and destroyed */
715 aws_future_destroyme_set_result_by_move(future, &result3);
716 ASSERT_PTR_EQUALS(result1_pointer_copy, aws_future_destroyme_peek_result(future));
717 ASSERT_FALSE(result1_destroyed);
718 ASSERT_NULL(result3);
719 ASSERT_TRUE(result3_destroyed);
720
721 /* setting an error is ignored, if there's already a result */
722 aws_future_destroyme_set_error(future, 999);
723 ASSERT_PTR_EQUALS(result1_pointer_copy, aws_future_destroyme_peek_result(future));
724 ASSERT_FALSE(result1_destroyed);
725 ASSERT_INT_EQUALS(0, aws_future_destroyme_get_error(future));
726
727 /* result1 should finally be destroyed when the future is destroyed */
728 aws_future_destroyme_release(future);
729 ASSERT_TRUE(result1_destroyed);
730
731 aws_io_library_clean_up();
732 return 0;
733 }
734 AWS_TEST_CASE(future_set_multiple_times, s_test_future_set_multiple_times)
735
736 static int s_test_future_set_error(struct aws_allocator *alloc, void *ctx) {
737 (void)ctx;
738 aws_io_library_init(alloc);
739
740 struct aws_future_destroyme *future = aws_future_destroyme_new(alloc);
741
742 /* Set error code */
743 aws_future_destroyme_set_error(future, 999);
744 ASSERT_TRUE(aws_future_destroyme_is_done(future));
745 ASSERT_INT_EQUALS(999, aws_future_destroyme_get_error(future));
746
747 /* Attempts to change the error should be ignored */
748 aws_future_destroyme_set_error(future, 222);
749 ASSERT_INT_EQUALS(999, aws_future_destroyme_get_error(future));
750
751 /* Attempts to set a result instead should be ignored (the new result should just get destroyed) */
752 bool result_destroyed = false;
753 struct aws_destroyme *result = aws_destroyme_new(alloc, &result_destroyed);
754 aws_future_destroyme_set_result_by_move(future, &result);
755 ASSERT_INT_EQUALS(999, aws_future_destroyme_get_error(future));
756 ASSERT_NULL(result);
757 ASSERT_TRUE(result_destroyed);
758
759 aws_future_destroyme_release(future);
760 aws_io_library_clean_up();
761 return 0;
762 }
763 AWS_TEST_CASE(future_set_error, s_test_future_set_error)