| 1 |
#ifndef AWS_TESTING_IO_TESTING_CHANNEL_H |
| 2 |
#define AWS_TESTING_IO_TESTING_CHANNEL_H |
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 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 |
|
| 304 |
|
| 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 |
|
| 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 |
|
| 316 |
|
| 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 |
|
| 327 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 345 |
|
| 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 |
|
| 356 |
|
| 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 |
|
| 374 |
|
| 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 |
|
| 385 |
|
| 386 |
|
| 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 |
|
| 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 |
|
| 432 |
testing_channel_drain_queued_tasks(testing); |
| 433 |
ASSERT_TRUE(testing->channel_shutdown_completed); |
| 434 |
|
| 435 |
aws_channel_destroy(testing->channel); |
| 436 |
|
| 437 |
|
| 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 |
|
| 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 |
|
| 462 |
static inline bool testing_channel_is_shutdown_completed(const struct testing_channel *testing) { |
| 463 |
return testing->channel_shutdown_completed; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 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 |
|
| 474 |
|
| 475 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 628 |
|
| 629 |
|
| 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 |
|
| 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 |
|
| 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 |