Debian Codegraph @

crt/aws-c-io/include/aws/testing/io_testing_channel.h

raw symbols SCIP
1 #ifndef AWS_TESTING_IO_TESTING_CHANNEL_H
2 #define AWS_TESTING_IO_TESTING_CHANNEL_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/common/clock.h>
8 #include <aws/common/task_scheduler.h>
9 #include <aws/io/channel.h>
10 #include <aws/io/event_loop.h>
11 #include <aws/io/logging.h>
12 #include <aws/io/statistics.h>
13 #include <aws/testing/aws_test_harness.h>
14
15 struct testing_loop {
16 struct aws_allocator *allocator;
17 struct aws_task_scheduler scheduler;
18 bool mock_on_callers_thread;
19 };
20
21 static int s_testing_loop_run(struct aws_event_loop *event_loop) {
22 (void)event_loop;
23 return AWS_OP_SUCCESS;
24 }
25
26 static int s_testing_loop_stop(struct aws_event_loop *event_loop) {
27 (void)event_loop;
28 return AWS_OP_SUCCESS;
29 }
30
31 static int s_testing_loop_wait_for_stop_completion(struct aws_event_loop *event_loop) {
32 (void)event_loop;
33 return AWS_OP_SUCCESS;
34 }
35
36 static void s_testing_loop_schedule_task_now(struct aws_event_loop *event_loop, struct aws_task *task) {
37 struct testing_loop *testing_loop = (struct testing_loop *)aws_event_loop_get_impl(event_loop);
38 aws_task_scheduler_schedule_now(&testing_loop->scheduler, task);
39 }
40
41 static void s_testing_loop_schedule_task_future(
42 struct aws_event_loop *event_loop,
43 struct aws_task *task,
44 uint64_t run_at_nanos) {
45
46 struct testing_loop *testing_loop = (struct testing_loop *)aws_event_loop_get_impl(event_loop);
47 aws_task_scheduler_schedule_future(&testing_loop->scheduler, task, run_at_nanos);
48 }
49
50 static void s_testing_loop_cancel_task(struct aws_event_loop *event_loop, struct aws_task *task) {
51 struct testing_loop *testing_loop = (struct testing_loop *)aws_event_loop_get_impl(event_loop);
52 aws_task_scheduler_cancel_task(&testing_loop->scheduler, task);
53 }
54
55 static bool s_testing_loop_is_on_callers_thread(struct aws_event_loop *event_loop) {
56 struct testing_loop *testing_loop = (struct testing_loop *)aws_event_loop_get_impl(event_loop);
57 return testing_loop->mock_on_callers_thread;
58 }
59
60 static void s_testing_loop_start_destroy(struct aws_event_loop *event_loop) {
61 (void)event_loop;
62 }
63
64 static void s_testing_loop_complete_destroy(struct aws_event_loop *event_loop) {
65 struct testing_loop *testing_loop = (struct testing_loop *)aws_event_loop_get_impl(event_loop);
66 struct aws_allocator *allocator = testing_loop->allocator;
67 aws_task_scheduler_clean_up(&testing_loop->scheduler);
68 aws_mem_release(allocator, testing_loop);
69 aws_event_loop_clean_up_base(event_loop);
70 aws_mem_release(allocator, event_loop);
71 }
72
73 static struct aws_event_loop_vtable s_testing_loop_vtable = {
74 .start_destroy = s_testing_loop_start_destroy,
75 .complete_destroy = s_testing_loop_complete_destroy,
76 .is_on_callers_thread = s_testing_loop_is_on_callers_thread,
77 .run = s_testing_loop_run,
78 .schedule_task_now = s_testing_loop_schedule_task_now,
79 .schedule_task_future = s_testing_loop_schedule_task_future,
80 .cancel_task = s_testing_loop_cancel_task,
81 .stop = s_testing_loop_stop,
82 .wait_for_stop_completion = s_testing_loop_wait_for_stop_completion,
83 };
84
85 static struct aws_event_loop *s_testing_loop_new(struct aws_allocator *allocator, aws_io_clock_fn clock) {
86 struct testing_loop *testing_loop =
87 (struct testing_loop *)aws_mem_calloc(allocator, 1, sizeof(struct testing_loop));
88
89 aws_task_scheduler_init(&testing_loop->scheduler, allocator);
90 testing_loop->mock_on_callers_thread = true;
91 testing_loop->allocator = allocator;
92
93 return aws_event_loop_new_base(allocator, clock, &s_testing_loop_vtable, testing_loop);
94 }
95
96 typedef void(testing_channel_handler_on_shutdown_fn)(
97 enum aws_channel_direction dir,
98 int error_code,
99 bool free_scarce_resources_immediately,
100 void *user_data);
101
102 struct testing_channel_handler {
103 struct aws_linked_list messages;
104 size_t latest_window_update;
105 size_t initial_window;
106 bool complete_write_immediately;
107 int complete_write_error_code;
108 testing_channel_handler_on_shutdown_fn *on_shutdown;
109 void *on_shutdown_user_data;
110 struct aws_crt_statistics_socket stats;
111 };
112
113 static int s_testing_channel_handler_process_read_message(
114 struct aws_channel_handler *handler,
115 struct aws_channel_slot *slot,
116 struct aws_io_message *message) {
117 (void)handler;
118 (void)slot;
119 (void)message;
120
121 struct testing_channel_handler *testing_handler = (struct testing_channel_handler *)handler->impl;
122 aws_linked_list_push_back(&testing_handler->messages, &message->queueing_handle);
123 return AWS_OP_SUCCESS;
124 }
125
126 static int s_testing_channel_handler_process_write_message(
127 struct aws_channel_handler *handler,
128 struct aws_channel_slot *slot,
129 struct aws_io_message *message) {
130 (void)slot;
131
132 struct testing_channel_handler *testing_handler = (struct testing_channel_handler *)handler->impl;
133 aws_linked_list_push_back(&testing_handler->messages, &message->queueing_handle);
134
135 /* Invoke completion callback if this is the left-most handler */
136 if (message->on_completion && !slot->adj_left && testing_handler->complete_write_immediately) {
137 message->on_completion(slot->channel, message, testing_handler->complete_write_error_code, message->user_data);
138 message->on_completion = NULL;
139 }
140
141 return AWS_OP_SUCCESS;
142 }
143
144 static int s_testing_channel_handler_increment_read_window(
145 struct aws_channel_handler *handler,
146 struct aws_channel_slot *slot,
147 size_t size) {
148 (void)slot;
149
150 struct testing_channel_handler *testing_handler = (struct testing_channel_handler *)handler->impl;
151 testing_handler->latest_window_update = size;
152 return AWS_OP_SUCCESS;
153 }
154
155 static int s_testing_channel_handler_shutdown(
156 struct aws_channel_handler *handler,
157 struct aws_channel_slot *slot,
158 enum aws_channel_direction dir,
159 int error_code,
160 bool free_scarce_resources_immediately) {
161
162 struct testing_channel_handler *testing_handler = (struct testing_channel_handler *)handler->impl;
163
164 /* If user has registered a callback, invoke it */
165 if (testing_handler->on_shutdown) {
166 testing_handler->on_shutdown(
167 dir, error_code, free_scarce_resources_immediately, testing_handler->on_shutdown_user_data);
168 }
169
170 if (dir == AWS_CHANNEL_DIR_WRITE) {
171 if (!slot->adj_left) {
172 /* Invoke the on_completion callbacks for any queued messages */
173 struct aws_linked_list_node *node = aws_linked_list_begin(&testing_handler->messages);
174 while (node != aws_linked_list_end(&testing_handler->messages)) {
175 struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle);
176
177 if (msg->on_completion) {
178 msg->on_completion(slot->channel, msg, AWS_IO_SOCKET_CLOSED, msg->user_data);
179 msg->on_completion = NULL;
180 }
181
182 node = aws_linked_list_next(node);
183 }
184 }
185 }
186
187 return aws_channel_slot_on_handler_shutdown_complete(slot, dir, error_code, free_scarce_resources_immediately);
188 }
189
190 static size_t s_testing_channel_handler_initial_window_size(struct aws_channel_handler *handler) {
191 struct testing_channel_handler *testing_handler = (struct testing_channel_handler *)handler->impl;
192 return testing_handler->initial_window;
193 }
194
195 static size_t s_testing_channel_handler_message_overhead(struct aws_channel_handler *handler) {
196 (void)handler;
197 return 0;
198 }
199
200 static void s_testing_channel_handler_destroy(struct aws_channel_handler *handler) {
201 struct testing_channel_handler *testing_handler = (struct testing_channel_handler *)handler->impl;
202
203 while (!aws_linked_list_empty(&testing_handler->messages)) {
204 struct aws_linked_list_node *node = aws_linked_list_pop_front(&testing_handler->messages);
205 struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle);
206 aws_mem_release(msg->allocator, msg);
207 }
208
209 aws_mem_release(handler->alloc, testing_handler);
210 aws_mem_release(handler->alloc, handler);
211 }
212
213 static void s_testing_channel_handler_reset_statistics(struct aws_channel_handler *handler) {
214 struct testing_channel_handler *testing_handler = (struct testing_channel_handler *)handler->impl;
215
216 aws_crt_statistics_socket_reset(&testing_handler->stats);
217 }
218
219 static void s_testing_channel_handler_gather_statistics(
220 struct aws_channel_handler *handler,
221 struct aws_array_list *stats) {
222 struct testing_channel_handler *testing_handler = (struct testing_channel_handler *)handler->impl;
223
224 void *stats_base = &testing_handler->stats;
225 aws_array_list_push_back(stats, &stats_base);
226 }
227
228 static struct aws_channel_handler_vtable s_testing_channel_handler_vtable = {
229 .process_read_message = s_testing_channel_handler_process_read_message,
230 .process_write_message = s_testing_channel_handler_process_write_message,
231 .increment_read_window = s_testing_channel_handler_increment_read_window,
232 .shutdown = s_testing_channel_handler_shutdown,
233 .initial_window_size = s_testing_channel_handler_initial_window_size,
234 .message_overhead = s_testing_channel_handler_message_overhead,
235 .destroy = s_testing_channel_handler_destroy,
236 .gather_statistics = s_testing_channel_handler_gather_statistics,
237 .reset_statistics = s_testing_channel_handler_reset_statistics,
238 };
239
240 static struct aws_channel_handler *s_new_testing_channel_handler(
241 struct aws_allocator *allocator,
242 size_t initial_window) {
243 struct aws_channel_handler *handler =
244 (struct aws_channel_handler *)aws_mem_calloc(allocator, 1, sizeof(struct aws_channel_handler));
245 struct testing_channel_handler *testing_handler =
246 (struct testing_channel_handler *)aws_mem_calloc(allocator, 1, sizeof(struct testing_channel_handler));
247 aws_linked_list_init(&testing_handler->messages);
248 testing_handler->initial_window = initial_window;
249 testing_handler->latest_window_update = 0;
250 testing_handler->complete_write_immediately = true;
251 testing_handler->complete_write_error_code = AWS_ERROR_SUCCESS;
252 handler->impl = testing_handler;
253 handler->vtable = &s_testing_channel_handler_vtable;
254 handler->alloc = allocator;
255
256 return handler;
257 }
258
259 struct testing_channel {
260 struct aws_event_loop *loop;
261 struct testing_loop *loop_impl;
262 struct aws_channel *channel;
263 struct testing_channel_handler *left_handler_impl;
264 struct testing_channel_handler *right_handler_impl;
265 struct aws_channel_slot *left_handler_slot;
266 struct aws_channel_slot *right_handler_slot;
267
268 void (*channel_shutdown)(int error_code, void *user_data);
269 void *channel_shutdown_user_data;
270
271 bool channel_setup_completed;
272 bool channel_shutdown_completed;
273 int channel_shutdown_error_code;
274 };
275
276 static void s_testing_channel_on_setup_completed(struct aws_channel *channel, int error_code, void *user_data) {
277 (void)channel;
278 (void)error_code;
279 struct testing_channel *testing = (struct testing_channel *)user_data;
280 testing->channel_setup_completed = true;
281 }
282
283 static void s_testing_channel_on_shutdown_completed(struct aws_channel *channel, int error_code, void *user_data) {
284 (void)channel;
285 (void)error_code;
286 struct testing_channel *testing = (struct testing_channel *)user_data;
287 testing->channel_shutdown_completed = true;
288 testing->channel_shutdown_error_code = error_code;
289
290 if (testing->channel_shutdown) {
291 testing->channel_shutdown(error_code, testing->channel_shutdown_user_data);
292 }
293 }
294
295 /** API for testing, use this for testing purely your channel handlers and nothing else. Because of that, the s_
296 * convention isn't used on the functions (since they're intended for you to call). */
297
298 /** when you want to test the read path of your handler, call this with the message you want it to read. */
299 static inline int testing_channel_push_read_message(struct testing_channel *testing, struct aws_io_message *message) {
300 return aws_channel_slot_send_message(testing->left_handler_slot, message, AWS_CHANNEL_DIR_READ);
301 }
302
303 /** when you want to test the write path of your handler, call this with the message you want it to write.
304 * A downstream handler must have been installed */
305 static inline int testing_channel_push_write_message(struct testing_channel *testing, struct aws_io_message *message) {
306 ASSERT_NOT_NULL(testing->right_handler_slot);
307 return aws_channel_slot_send_message(testing->right_handler_slot, message, AWS_CHANNEL_DIR_WRITE);
308 }
309
310 /** when you want to test the write output of your handler, call this, get the queue and iterate the messages. */
311 static inline struct aws_linked_list *testing_channel_get_written_message_queue(struct testing_channel *testing) {
312 return &testing->left_handler_impl->messages;
313 }
314
315 /** Set whether written messages have their on_complete callbacks invoked immediately.
316 * The on_complete callback will be cleared after it is invoked. */
317 static inline void testing_channel_complete_written_messages_immediately(
318 struct testing_channel *testing,
319 bool complete_immediately,
320 int complete_error_code) {
321
322 testing->left_handler_impl->complete_write_immediately = complete_immediately;
323 testing->left_handler_impl->complete_write_error_code = complete_error_code;
324 }
325
326 /** when you want to test the read output of your handler, call this, get the queue and iterate the messages.
327 * A downstream handler must have been installed */
328 static inline struct aws_linked_list *testing_channel_get_read_message_queue(struct testing_channel *testing) {
329 AWS_ASSERT(testing->right_handler_impl);
330 return &testing->right_handler_impl->messages;
331 }
332
333 /** When you want to see what the latest window update issues from your channel handler was, call this. */
334 static inline size_t testing_channel_last_window_update(struct testing_channel *testing) {
335 return testing->left_handler_impl->latest_window_update;
336 }
337
338 /** When you want the downstream handler to issue a window update */
339 static inline int testing_channel_increment_read_window(struct testing_channel *testing, size_t size) {
340 ASSERT_NOT_NULL(testing->right_handler_slot);
341 return aws_channel_slot_increment_read_window(testing->right_handler_slot, size);
342 }
343
344 /** Executes all currently scheduled tasks whose time has come.
345 * Use testing_channel_drain_queued_tasks() to repeatedly run tasks until only future-tasks remain.
346 */
347 static inline void testing_channel_run_currently_queued_tasks(struct testing_channel *testing) {
348 AWS_ASSERT(aws_channel_thread_is_callers_thread(testing->channel));
349
350 uint64_t now = 0;
351 aws_event_loop_current_clock_time(testing->loop, &now);
352 aws_task_scheduler_run_all(&testing->loop_impl->scheduler, now);
353 }
354
355 /** Repeatedly executes scheduled tasks until only those in the future remain.
356 * This covers the common case where there's a chain reaction of now-tasks scheduling further now-tasks.
357 */
358 static inline void testing_channel_drain_queued_tasks(struct testing_channel *testing) {
359 AWS_ASSERT(aws_channel_thread_is_callers_thread(testing->channel));
360
361 uint64_t now = 0;
362 uint64_t next_task_time = 0;
363 size_t count = 0;
364
365 while (true) {
366 aws_event_loop_current_clock_time(testing->loop, &now);
367 if (aws_task_scheduler_has_tasks(&testing->loop_impl->scheduler, &next_task_time) && (next_task_time <= now)) {
368 aws_task_scheduler_run_all(&testing->loop_impl->scheduler, now);
369 } else {
370 break;
371 }
372
373 /* NOTE: This will loop infinitely if there's a task the perpetually re-schedules another task.
374 * Consider capping the number of loops if we want to support that behavior. */
375 if ((++count % 1000) == 0) {
376 AWS_LOGF_WARN(
377 AWS_LS_IO_CHANNEL,
378 "id=%p: testing_channel_drain_queued_tasks() has looped %zu times.",
379 (void *)testing->channel,
380 count);
381 }
382 }
383 }
384 /** When you want to force the "not on channel thread path" for your handler, set 'on_users_thread' to false.
385 * when you want to undo that, set it back to true. If you set it to false, you'll need to call
386 * 'testing_channel_execute_queued_tasks()' to invoke the tasks that ended up being scheduled. */
387 static inline void testing_channel_set_is_on_users_thread(struct testing_channel *testing, bool on_users_thread) {
388 testing->loop_impl->mock_on_callers_thread = on_users_thread;
389 }
390
391 struct aws_testing_channel_options {
392 aws_io_clock_fn *clock_fn;
393 };
394
395 static inline int testing_channel_init(
396 struct testing_channel *testing,
397 struct aws_allocator *allocator,
398 struct aws_testing_channel_options *options) {
399 AWS_ZERO_STRUCT(*testing);
400
401 testing->loop = s_testing_loop_new(allocator, options->clock_fn);
402 testing->loop_impl = (struct testing_loop *)aws_event_loop_get_impl(testing->loop);
403
404 struct aws_channel_options args = {
405 .on_setup_completed = s_testing_channel_on_setup_completed,
406 .on_shutdown_completed = s_testing_channel_on_shutdown_completed,
407 .setup_user_data = testing,
408 .shutdown_user_data = testing,
409 .event_loop = testing->loop,
410 .enable_read_back_pressure = true,
411 };
412
413 testing->channel = aws_channel_new(allocator, &args);
414
415 /* Wait for channel to finish setup */
416 testing_channel_drain_queued_tasks(testing);
417 ASSERT_TRUE(testing->channel_setup_completed);
418
419 testing->left_handler_slot = aws_channel_slot_new(testing->channel);
420 struct aws_channel_handler *handler =
421 (struct aws_channel_handler *)s_new_testing_channel_handler(allocator, 16 * 1024);
422 testing->left_handler_impl = (struct testing_channel_handler *)handler->impl;
423 ASSERT_SUCCESS(aws_channel_slot_set_handler(testing->left_handler_slot, handler));
424
425 return AWS_OP_SUCCESS;
426 }
427
428 static inline int testing_channel_clean_up(struct testing_channel *testing) {
429 aws_channel_shutdown(testing->channel, AWS_ERROR_SUCCESS);
430
431 /* Wait for channel to finish shutdown */
432 testing_channel_drain_queued_tasks(testing);
433 ASSERT_TRUE(testing->channel_shutdown_completed);
434
435 aws_channel_destroy(testing->channel);
436
437 /* event_loop can't be destroyed from its own thread */
438 testing_channel_set_is_on_users_thread(testing, false);
439 aws_event_loop_destroy(testing->loop);
440
441 return AWS_OP_SUCCESS;
442 }
443
444 /** When you want to test your handler with a downstream handler installed to the right. */
445 static inline int testing_channel_install_downstream_handler(struct testing_channel *testing, size_t initial_window) {
446 ASSERT_NULL(testing->right_handler_slot);
447
448 testing->right_handler_slot = aws_channel_slot_new(testing->channel);
449 ASSERT_NOT_NULL(testing->right_handler_slot);
450 ASSERT_SUCCESS(aws_channel_slot_insert_end(testing->channel, testing->right_handler_slot));
451
452 struct aws_channel_handler *handler =
453 s_new_testing_channel_handler(testing->left_handler_slot->alloc, initial_window);
454 ASSERT_NOT_NULL(handler);
455 testing->right_handler_impl = (struct testing_channel_handler *)handler->impl;
456 ASSERT_SUCCESS(aws_channel_slot_set_handler(testing->right_handler_slot, handler));
457
458 return AWS_OP_SUCCESS;
459 }
460
461 /** Return whether channel is completely shut down */
462 static inline bool testing_channel_is_shutdown_completed(const struct testing_channel *testing) {
463 return testing->channel_shutdown_completed;
464 }
465
466 /** Return channel's shutdown error_code */
467 static inline int testing_channel_get_shutdown_error_code(const struct testing_channel *testing) {
468 AWS_ASSERT(testing->channel_shutdown_completed);
469 return testing->channel_shutdown_error_code;
470 }
471
472 /**
473 * Set a callback which is invoked during the handler's shutdown,
474 * once in the read direction and again in the write direction.
475 * Use this to inject actions that might occur in the middle of channel shutdown.
476 */
477 static inline void testing_channel_set_downstream_handler_shutdown_callback(
478 struct testing_channel *testing,
479 testing_channel_handler_on_shutdown_fn *on_shutdown,
480 void *user_data) {
481
482 AWS_ASSERT(testing->right_handler_impl);
483 testing->right_handler_impl->on_shutdown = on_shutdown;
484 testing->right_handler_impl->on_shutdown_user_data = user_data;
485 }
486
487 /* Pop first message from queue and compare its contents to expected data. */
488 static inline int testing_channel_check_written_message(
489 struct testing_channel *channel,
490 struct aws_byte_cursor expected) {
491 struct aws_linked_list *msgs = testing_channel_get_written_message_queue(channel);
492 ASSERT_TRUE(!aws_linked_list_empty(msgs));
493 struct aws_linked_list_node *node = aws_linked_list_pop_front(msgs);
494 struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle);
495
496 ASSERT_BIN_ARRAYS_EQUALS(expected.ptr, expected.len, msg->message_data.buffer, msg->message_data.len);
497
498 aws_mem_release(msg->allocator, msg);
499
500 return AWS_OP_SUCCESS;
501 }
502
503 /* Pop first message from queue and compare its contents to expected data. */
504 static inline int testing_channel_check_written_message_str(struct testing_channel *channel, const char *expected) {
505 return testing_channel_check_written_message(channel, aws_byte_cursor_from_c_str(expected));
506 }
507
508 /* copies all messages in a list into a buffer, cleans up messages*/
509 static inline int testing_channel_drain_messages(struct aws_linked_list *msgs, struct aws_byte_buf *buffer) {
510
511 while (!aws_linked_list_empty(msgs)) {
512 struct aws_linked_list_node *node = aws_linked_list_pop_front(msgs);
513 struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle);
514
515 struct aws_byte_cursor msg_cursor = aws_byte_cursor_from_buf(&msg->message_data);
516 aws_byte_buf_append_dynamic(buffer, &msg_cursor);
517
518 aws_mem_release(msg->allocator, msg);
519 }
520
521 return AWS_OP_SUCCESS;
522 }
523
524 /* Pop all messages from queue and compare their contents to expected data */
525 static inline int testing_channel_check_messages_ex(
526 struct aws_linked_list *msgs,
527 struct aws_allocator *allocator,
528 struct aws_byte_cursor expected) {
529 struct aws_byte_buf all_msgs;
530 ASSERT_SUCCESS(aws_byte_buf_init(&all_msgs, allocator, 1024));
531
532 ASSERT_SUCCESS(testing_channel_drain_messages(msgs, &all_msgs));
533
534 ASSERT_BIN_ARRAYS_EQUALS(expected.ptr, expected.len, all_msgs.buffer, all_msgs.len);
535 aws_byte_buf_clean_up(&all_msgs);
536 return AWS_OP_SUCCESS;
537 }
538
539 /* Check contents of all messages sent in the write direction. */
540 static inline int testing_channel_check_written_messages(
541 struct testing_channel *channel,
542 struct aws_allocator *allocator,
543 struct aws_byte_cursor expected) {
544
545 struct aws_linked_list *msgs = testing_channel_get_written_message_queue(channel);
546 return testing_channel_check_messages_ex(msgs, allocator, expected);
547 }
548
549 /* Check contents of all messages sent in the write direction. */
550 static inline int testing_channel_check_written_messages_str(
551 struct testing_channel *channel,
552 struct aws_allocator *allocator,
553 const char *expected) {
554
555 return testing_channel_check_written_messages(channel, allocator, aws_byte_cursor_from_c_str(expected));
556 }
557
558 /* Extract contents of all messages sent in the write direction. */
559 static inline int testing_channel_drain_written_messages(struct testing_channel *channel, struct aws_byte_buf *output) {
560 struct aws_linked_list *msgs = testing_channel_get_written_message_queue(channel);
561 ASSERT_SUCCESS(testing_channel_drain_messages(msgs, output));
562
563 return AWS_OP_SUCCESS;
564 }
565
566 /* Check contents of all read-messages sent in the read direction by a midchannel http-handler */
567 static inline int testing_channel_check_midchannel_read_messages(
568 struct testing_channel *channel,
569 struct aws_allocator *allocator,
570 struct aws_byte_cursor expected) {
571
572 struct aws_linked_list *msgs = testing_channel_get_read_message_queue(channel);
573 return testing_channel_check_messages_ex(msgs, allocator, expected);
574 }
575
576 /* Check contents of all read-messages sent in the read direction by a midchannel http-handler */
577 static inline int testing_channel_check_midchannel_read_messages_str(
578 struct testing_channel *channel,
579 struct aws_allocator *allocator,
580 const char *expected) {
581
582 return testing_channel_check_midchannel_read_messages(channel, allocator, aws_byte_cursor_from_c_str(expected));
583 }
584
585 /* For sending an aws_io_message into the channel, in the write or read direction */
586 static inline int testing_channel_send_data(
587 struct testing_channel *channel,
588 struct aws_byte_cursor data,
589 enum aws_channel_direction dir,
590 bool ignore_send_message_errors) {
591
592 struct aws_io_message *msg =
593 aws_channel_acquire_message_from_pool(channel->channel, AWS_IO_MESSAGE_APPLICATION_DATA, data.len);
594 ASSERT_NOT_NULL(msg);
595
596 ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(&msg->message_data, data));
597
598 int err;
599 if (dir == AWS_CHANNEL_DIR_READ) {
600 err = testing_channel_push_read_message(channel, msg);
601 } else {
602 err = testing_channel_push_write_message(channel, msg);
603 }
604
605 if (err) {
606 /* If an error happens, clean the message here. Else, the recipient of the message will take the ownership */
607 aws_mem_release(msg->allocator, msg);
608 }
609
610 if (!ignore_send_message_errors) {
611 ASSERT_SUCCESS(err);
612 }
613
614 return AWS_OP_SUCCESS;
615 }
616
617 /** Create an aws_io_message, containing the following data, and pushes it up the channel in the read direction */
618 static inline int testing_channel_push_read_data(struct testing_channel *channel, struct aws_byte_cursor data) {
619 return testing_channel_send_data(channel, data, AWS_CHANNEL_DIR_READ, false);
620 }
621
622 /** Create an aws_io_message, containing the following data, and pushes it up the channel in the read direction */
623 static inline int testing_channel_push_read_str(struct testing_channel *channel, const char *str) {
624 return testing_channel_send_data(channel, aws_byte_cursor_from_c_str(str), AWS_CHANNEL_DIR_READ, false);
625 }
626
627 /** Create an aws_io_message, containing the following data.
628 * Tries to push it up the channel in the read direction, but don't assert if the message can't be sent.
629 * Useful for testing data that arrives during handler shutdown */
630 static inline int testing_channel_push_read_str_ignore_errors(struct testing_channel *channel, const char *str) {
631 return testing_channel_send_data(channel, aws_byte_cursor_from_c_str(str), AWS_CHANNEL_DIR_READ, true);
632 }
633
634 /** Create an aws_io_message, containing the following data, and pushes it up the channel in the write direction */
635 static inline int testing_channel_push_write_data(struct testing_channel *channel, struct aws_byte_cursor data) {
636 return testing_channel_send_data(channel, data, AWS_CHANNEL_DIR_WRITE, false);
637 }
638
639 /** Create an aws_io_message, containing the following data, and pushes it up the channel in the write direction */
640 static inline int testing_channel_push_write_str(struct testing_channel *channel, const char *str) {
641 return testing_channel_send_data(channel, aws_byte_cursor_from_c_str(str), AWS_CHANNEL_DIR_WRITE, false);
642 }
643
644 #endif /* AWS_TESTING_IO_TESTING_CHANNEL_H */