Debian Codegraph @

crt/aws-c-common/tests/task_scheduler_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/task_scheduler.h>
7 #include <aws/common/thread.h>
8 #include <aws/testing/aws_test_harness.h>
9
10 struct executed_task_data {
11 struct aws_task *task;
12 void *arg;
13 enum aws_task_status status;
14 };
15
16 static struct executed_task_data s_executed_tasks[16];
17
18 static size_t s_executed_tasks_n;
19
20 /* Updates tl_executed_tasks and tl_executed_task_n when function is executed */
21 static void s_task_n_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
22 if (s_executed_tasks_n > AWS_ARRAY_SIZE(s_executed_tasks)) {
23 AWS_ASSERT(0);
24 }
25
26 struct executed_task_data *data = &s_executed_tasks[s_executed_tasks_n++];
27 data->task = task;
28 data->arg = arg;
29 data->status = status;
30 }
31
32 static int s_test_scheduler_ordering(struct aws_allocator *allocator, void *ctx) {
33 (void)ctx;
34 s_executed_tasks_n = 0;
35
36 struct aws_task_scheduler scheduler;
37 aws_task_scheduler_init(&scheduler, allocator);
38
39 struct aws_task task2;
40 aws_task_init(&task2, s_task_n_fn, (void *)2, "scheduler_ordering_1");
41
42 /* schedule 250 ns in the future. */
43 uint64_t task2_timestamp = 250;
44 aws_task_scheduler_schedule_future(&scheduler, &task2, task2_timestamp);
45
46 struct aws_task task1;
47 aws_task_init(&task1, s_task_n_fn, (void *)1, "scheduler_ordering_2");
48
49 /* schedule now. */
50 aws_task_scheduler_schedule_now(&scheduler, &task1);
51
52 struct aws_task task3;
53 aws_task_init(&task3, s_task_n_fn, (void *)3, "scheduler_ordering_3");
54
55 /* schedule 500 ns in the future. */
56 uint64_t task3_timestamp = 500;
57 aws_task_scheduler_schedule_future(&scheduler, &task3, task3_timestamp);
58
59 /* run tasks 1 and 2 (but not 3) */
60 aws_task_scheduler_run_all(&scheduler, task2_timestamp);
61
62 ASSERT_UINT_EQUALS(2, s_executed_tasks_n);
63
64 struct executed_task_data *task_data = &s_executed_tasks[0];
65 ASSERT_PTR_EQUALS(&task1, task_data->task);
66 ASSERT_PTR_EQUALS(task1.arg, task_data->arg);
67 ASSERT_INT_EQUALS(AWS_TASK_STATUS_RUN_READY, task_data->status);
68
69 task_data = &s_executed_tasks[1];
70 ASSERT_PTR_EQUALS(&task2, task_data->task);
71 ASSERT_PTR_EQUALS(task2.arg, task_data->arg);
72 ASSERT_INT_EQUALS(AWS_TASK_STATUS_RUN_READY, task_data->status);
73
74 /* run task 3 */
75 aws_task_scheduler_run_all(&scheduler, task3.timestamp);
76
77 ASSERT_UINT_EQUALS(3, s_executed_tasks_n);
78
79 task_data = &s_executed_tasks[2];
80 ASSERT_PTR_EQUALS(&task3, task_data->task);
81 ASSERT_PTR_EQUALS(task3.arg, task_data->arg);
82 ASSERT_INT_EQUALS(AWS_TASK_STATUS_RUN_READY, task_data->status);
83
84 aws_task_scheduler_clean_up(&scheduler);
85 return 0;
86 }
87
88 static void s_null_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
89 (void)task;
90 (void)arg;
91 (void)status;
92 }
93
94 static int s_test_scheduler_has_tasks(struct aws_allocator *allocator, void *ctx) {
95 (void)ctx;
96
97 struct aws_task_scheduler scheduler;
98 aws_task_scheduler_init(&scheduler, allocator);
99
100 /* Check when no tasks scheduled */
101 uint64_t next_task_time = 123456;
102 ASSERT_FALSE(aws_task_scheduler_has_tasks(&scheduler, &next_task_time));
103 ASSERT_UINT_EQUALS(UINT64_MAX, next_task_time);
104
105 /* Check when a task is scheduled for the future */
106 struct aws_task timed_task;
107 aws_task_init(&timed_task, s_null_fn, (void *)1, "scheduler_has_tasks_1");
108
109 aws_task_scheduler_schedule_future(&scheduler, &timed_task, 10);
110 ASSERT_TRUE(aws_task_scheduler_has_tasks(&scheduler, &next_task_time));
111 ASSERT_UINT_EQUALS(10, next_task_time);
112
113 /* Check when a task is scheduled for now */
114 struct aws_task now_task;
115 aws_task_init(&now_task, s_null_fn, (void *)2, "scheduler_has_tasks_2");
116
117 aws_task_scheduler_schedule_now(&scheduler, &now_task);
118 ASSERT_TRUE(aws_task_scheduler_has_tasks(&scheduler, &next_task_time));
119 ASSERT_UINT_EQUALS(0, next_task_time);
120
121 aws_task_scheduler_clean_up(&scheduler);
122 return 0;
123 }
124
125 static int s_test_scheduler_pops_task_fashionably_late(struct aws_allocator *allocator, void *ctx) {
126 (void)ctx;
127 s_executed_tasks_n = 0;
128
129 struct aws_task_scheduler scheduler;
130 aws_task_scheduler_init(&scheduler, allocator);
131
132 struct aws_task task;
133 aws_task_init(&task, s_task_n_fn, (void *)0, "scheduler_pops_task_fashionably_late");
134
135 aws_task_scheduler_schedule_future(&scheduler, &task, 10);
136
137 /* Run scheduler before task is supposed to execute, check that it didn't execute */
138 aws_task_scheduler_run_all(&scheduler, 5);
139
140 ASSERT_UINT_EQUALS(0, s_executed_tasks_n);
141
142 /* Run scheduler long after task was due to execute, check that it executed */
143 aws_task_scheduler_run_all(&scheduler, 500);
144 ASSERT_UINT_EQUALS(1, s_executed_tasks_n);
145
146 struct executed_task_data *task_data = &s_executed_tasks[0];
147 ASSERT_PTR_EQUALS(&task, task_data->task);
148 ASSERT_PTR_EQUALS(task.arg, task_data->arg);
149 ASSERT_INT_EQUALS(AWS_TASK_STATUS_RUN_READY, task_data->status);
150
151 aws_task_scheduler_clean_up(&scheduler);
152 return 0;
153 }
154
155 /* container for running a task that schedules another task when it executes */
156 struct task_scheduler_reentrancy_args {
157 struct aws_task_scheduler *scheduler;
158 struct aws_task task;
159 bool executed;
160 enum aws_task_status status;
161 struct task_scheduler_reentrancy_args *next_task_args;
162 };
163
164 static void s_reentrancy_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
165 (void)task;
166 struct task_scheduler_reentrancy_args *reentrancy_args = (struct task_scheduler_reentrancy_args *)arg;
167
168 if (reentrancy_args->next_task_args) {
169 aws_task_scheduler_schedule_now(reentrancy_args->scheduler, &reentrancy_args->next_task_args->task);
170 }
171
172 reentrancy_args->executed = 1;
173 reentrancy_args->status = status;
174 }
175
176 static void s_reentrancy_args_init(
177 struct task_scheduler_reentrancy_args *args,
178 struct aws_task_scheduler *scheduler,
179 struct task_scheduler_reentrancy_args *next_task_args) {
180
181 AWS_ZERO_STRUCT(*args);
182 args->scheduler = scheduler;
183 aws_task_init(&args->task, s_reentrancy_fn, args, "scheduler_reentrancy");
184 args->status = -1;
185 args->next_task_args = next_task_args;
186 }
187
188 static int s_test_scheduler_reentrant_safe(struct aws_allocator *allocator, void *ctx) {
189 (void)ctx;
190
191 struct aws_task_scheduler scheduler;
192 aws_task_scheduler_init(&scheduler, allocator);
193
194 /* When task1 executes, it schedules task2 */
195 struct task_scheduler_reentrancy_args task2_args;
196 s_reentrancy_args_init(&task2_args, &scheduler, NULL);
197
198 struct task_scheduler_reentrancy_args task1_args;
199 s_reentrancy_args_init(&task1_args, &scheduler, &task2_args);
200
201 aws_task_scheduler_schedule_now(&scheduler, &task1_args.task);
202
203 /* Run, only task1 should have executed */
204 aws_task_scheduler_run_all(&scheduler, 100);
205
206 ASSERT_TRUE(task1_args.executed);
207 ASSERT_INT_EQUALS(AWS_TASK_STATUS_RUN_READY, task1_args.status);
208
209 ASSERT_FALSE(task2_args.executed);
210
211 /* Run again, task2 should execute */
212 aws_task_scheduler_run_all(&scheduler, 200);
213
214 ASSERT_TRUE(task2_args.executed);
215 ASSERT_INT_EQUALS(AWS_TASK_STATUS_RUN_READY, task2_args.status);
216
217 aws_task_scheduler_clean_up(&scheduler);
218 return 0;
219 }
220
221 struct cancellation_args {
222 enum aws_task_status status;
223 };
224
225 static void s_cancellation_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
226
227 (void)task;
228 struct cancellation_args *cancellation_args = (struct cancellation_args *)arg;
229
230 cancellation_args->status = status;
231 }
232
233 static int s_test_scheduler_cleanup_cancellation(struct aws_allocator *allocator, void *ctx) {
234 (void)ctx;
235
236 struct aws_task_scheduler scheduler;
237 aws_task_scheduler_init(&scheduler, allocator);
238
239 struct cancellation_args now_task_args = {.status = 100000};
240 struct aws_task now_task;
241 aws_task_init(&now_task, s_cancellation_fn, &now_task_args, "scheduler_cleanup_cancellation_1");
242 aws_task_scheduler_schedule_now(&scheduler, &now_task);
243
244 struct cancellation_args future_task_args = {.status = 100000};
245 struct aws_task future_task;
246 aws_task_init(&future_task, s_cancellation_fn, &future_task_args, "scheduler_cleanup_cancellation_2");
247 aws_task_scheduler_schedule_future(&scheduler, &future_task, 9999999999999);
248
249 aws_task_scheduler_clean_up(&scheduler);
250
251 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, now_task_args.status);
252 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, future_task_args.status);
253 return 0;
254 }
255
256 static int s_test_scheduler_cleanup_reentrants(struct aws_allocator *allocator, void *ctx) {
257 (void)ctx;
258
259 struct aws_task_scheduler scheduler;
260 aws_task_scheduler_init(&scheduler, allocator);
261
262 /* When now_task1 executes, it schedules now_task2 */
263 struct task_scheduler_reentrancy_args now_task2_args;
264 s_reentrancy_args_init(&now_task2_args, &scheduler, NULL);
265
266 struct task_scheduler_reentrancy_args now_task1_args;
267 s_reentrancy_args_init(&now_task1_args, &scheduler, &now_task2_args);
268
269 aws_task_scheduler_schedule_now(&scheduler, &now_task1_args.task);
270
271 /* When future_task1 executes, it schedules future_task2 */
272 struct task_scheduler_reentrancy_args future_task2_args;
273 s_reentrancy_args_init(&future_task2_args, &scheduler, NULL);
274
275 struct task_scheduler_reentrancy_args future_task1_args;
276 s_reentrancy_args_init(&future_task1_args, &scheduler, &future_task2_args);
277
278 aws_task_scheduler_schedule_future(&scheduler, &future_task1_args.task, 555555555555555555);
279
280 /* Clean up scheduler. All tasks should be executed with CANCELLED status */
281 aws_task_scheduler_clean_up(&scheduler);
282
283 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, now_task1_args.status);
284 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, now_task2_args.status);
285 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, future_task1_args.status);
286 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, future_task2_args.status);
287
288 return AWS_OP_SUCCESS;
289 }
290
291 struct task_cancelling_task_data {
292 struct aws_task_scheduler *scheduler;
293 struct aws_task *task_to_cancel;
294 };
295
296 static void s_task_cancelling_task(struct aws_task *task, void *arg, enum aws_task_status status) {
297 s_task_n_fn(task, arg, status);
298 struct task_cancelling_task_data *task_data = arg;
299 aws_task_scheduler_cancel_task(task_data->scheduler, task_data->task_to_cancel);
300 }
301
302 static int s_test_scheduler_schedule_cancellation(struct aws_allocator *allocator, void *ctx) {
303 (void)ctx;
304 s_executed_tasks_n = 0;
305
306 struct aws_task_scheduler scheduler;
307 aws_task_scheduler_init(&scheduler, allocator);
308
309 struct aws_task task2;
310 aws_task_init(&task2, s_task_n_fn, (void *)2, "scheduler_schedule_cancellation1");
311
312 /* schedule 250 ns in the future. */
313 uint64_t task2_timestamp = 250;
314 aws_task_scheduler_schedule_future(&scheduler, &task2, task2_timestamp);
315
316 struct aws_task task1;
317 aws_task_init(&task1, s_task_n_fn, (void *)1, "scheduler_schedule_cancellation2");
318
319 /* schedule now. */
320 aws_task_scheduler_schedule_now(&scheduler, &task1);
321
322 struct aws_task task5;
323 aws_task_init(&task5, s_task_n_fn, (void *)3, "scheduler_schedule_cancellation5");
324
325 /* schedule 500 ns in the future. */
326 uint64_t task5_timestamp = 500;
327 aws_task_scheduler_schedule_future(&scheduler, &task5, task5_timestamp);
328
329 struct aws_task task4;
330 aws_task_init(&task4, s_task_n_fn, (void *)5, "scheduler_schedule_cancellation4");
331
332 struct task_cancelling_task_data task_cancel_data = {
333 .scheduler = &scheduler,
334 .task_to_cancel = &task4,
335 };
336
337 struct aws_task task3;
338
339 aws_task_init(&task3, s_task_cancelling_task, &task_cancel_data, "scheduler_schedule_cancellation3");
340 aws_task_scheduler_schedule_now(&scheduler, &task3);
341 aws_task_scheduler_schedule_now(&scheduler, &task4);
342
343 aws_task_scheduler_cancel_task(&scheduler, &task1);
344 aws_task_scheduler_cancel_task(&scheduler, &task2);
345
346 aws_task_scheduler_run_all(&scheduler, task5_timestamp);
347
348 ASSERT_UINT_EQUALS(5, s_executed_tasks_n);
349
350 struct executed_task_data *task_data = &s_executed_tasks[0];
351 ASSERT_PTR_EQUALS(&task1, task_data->task);
352 ASSERT_PTR_EQUALS(task1.arg, task_data->arg);
353 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, task_data->status);
354
355 task_data = &s_executed_tasks[1];
356 ASSERT_PTR_EQUALS(&task2, task_data->task);
357 ASSERT_PTR_EQUALS(task2.arg, task_data->arg);
358 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, task_data->status);
359
360 task_data = &s_executed_tasks[2];
361 ASSERT_PTR_EQUALS(&task3, task_data->task);
362 ASSERT_PTR_EQUALS(task3.arg, task_data->arg);
363 ASSERT_INT_EQUALS(AWS_TASK_STATUS_RUN_READY, task_data->status);
364
365 task_data = &s_executed_tasks[3];
366 ASSERT_PTR_EQUALS(&task4, task_data->task);
367 ASSERT_PTR_EQUALS(task4.arg, task_data->arg);
368 ASSERT_INT_EQUALS(AWS_TASK_STATUS_CANCELED, task_data->status);
369
370 task_data = &s_executed_tasks[4];
371 ASSERT_PTR_EQUALS(&task5, task_data->task);
372 ASSERT_PTR_EQUALS(task5.arg, task_data->arg);
373 ASSERT_INT_EQUALS(AWS_TASK_STATUS_RUN_READY, task_data->status);
374
375 aws_task_scheduler_clean_up(&scheduler);
376 return 0;
377 }
378
379 static int s_test_scheduler_cleanup_idempotent(struct aws_allocator *allocator, void *ctx) {
380 (void)ctx;
381
382 struct aws_task_scheduler scheduler;
383 ASSERT_SUCCESS(aws_task_scheduler_init(&scheduler, allocator));
384 aws_task_scheduler_clean_up(&scheduler);
385 aws_task_scheduler_clean_up(&scheduler);
386 return 0;
387 }
388
389 static void s_delete_myself_fn(struct aws_task *task, void *arg, enum aws_task_status status) {
390 (void)status;
391
392 struct aws_allocator *allocator = arg;
393 aws_mem_release(allocator, task);
394 }
395
396 static int s_test_scheduler_task_delete_on_run(struct aws_allocator *allocator, void *ctx) {
397 (void)ctx;
398
399 struct aws_task_scheduler scheduler;
400 aws_task_scheduler_init(&scheduler, allocator);
401
402 /* Check when no tasks scheduled */
403 uint64_t task_time = 10;
404
405 /* Check when a task is scheduled for the future */
406 struct aws_task *self_deleting_task = aws_mem_calloc(allocator, 1, sizeof(struct aws_task));
407 aws_task_init(self_deleting_task, s_delete_myself_fn, allocator, "self_deleting_task");
408
409 aws_task_scheduler_schedule_future(&scheduler, self_deleting_task, task_time);
410 ASSERT_TRUE(aws_task_scheduler_has_tasks(&scheduler, &task_time));
411
412 aws_task_scheduler_run_all(&scheduler, task_time);
413
414 aws_task_scheduler_clean_up(&scheduler);
415
416 return 0;
417 }
418
419 AWS_TEST_CASE(scheduler_pops_task_late_test, s_test_scheduler_pops_task_fashionably_late);
420 AWS_TEST_CASE(scheduler_ordering_test, s_test_scheduler_ordering);
421 AWS_TEST_CASE(scheduler_has_tasks_test, s_test_scheduler_has_tasks);
422 AWS_TEST_CASE(scheduler_reentrant_safe, s_test_scheduler_reentrant_safe);
423 AWS_TEST_CASE(scheduler_cleanup_cancellation, s_test_scheduler_cleanup_cancellation);
424 AWS_TEST_CASE(scheduler_cleanup_reentrants, s_test_scheduler_cleanup_reentrants);
425 AWS_TEST_CASE(scheduler_schedule_cancellation, s_test_scheduler_schedule_cancellation);
426 AWS_TEST_CASE(scheduler_cleanup_idempotent, s_test_scheduler_cleanup_idempotent);
427 AWS_TEST_CASE(scheduler_task_delete_on_run, s_test_scheduler_task_delete_on_run);