1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/thread.h>
#include <aws/http/connection.h>
#include <aws/http/private/connection_monitor.h>
#include <aws/http/private/h1_connection.h>
#include <aws/http/request_response.h>
#include <aws/http/statistics.h>
#include <aws/io/channel.h>
#include <aws/io/statistics.h>
#include <aws/io/stream.h>
#include <aws/testing/aws_test_harness.h>
#include <aws/testing/io_testing_channel.h>
static int s_test_http_connection_monitor_options_is_valid(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
(void)allocator;
struct aws_http_connection_monitoring_options options;
AWS_ZERO_STRUCT(options);
ASSERT_FALSE(aws_http_connection_monitoring_options_is_valid(NULL));
ASSERT_FALSE(aws_http_connection_monitoring_options_is_valid(&options));
options.allowable_throughput_failure_interval_seconds = 5;
ASSERT_FALSE(aws_http_connection_monitoring_options_is_valid(&options));
options.allowable_throughput_failure_interval_seconds = 0;
options.minimum_throughput_bytes_per_second = 1000;
ASSERT_FALSE(aws_http_connection_monitoring_options_is_valid(&options));
options.allowable_throughput_failure_interval_seconds = 2;
ASSERT_TRUE(aws_http_connection_monitoring_options_is_valid(&options));
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_options_is_valid, s_test_http_connection_monitor_options_is_valid);
static void s_testing_channel_shutdown_callback(int error_code, void *user_data) {
(void)error_code;
(void)user_data;
}
enum monitor_test_event_type { MTET_EMPTY, MTET_STATS };
struct http_monitor_test_stats_event {
uint64_t timestamp;
uint64_t expected_throughput;
struct aws_crt_statistics_socket socket_stats;
struct aws_crt_statistics_http1_channel http_stats;
enum monitor_test_event_type event_type;
uint32_t expected_consecutive_failure_time_ms;
};
struct http_request_info {
struct aws_http_message *request;
struct aws_http_stream *stream;
struct aws_input_stream *body;
bool response_completed;
};
struct monitor_test_context {
struct aws_allocator *allocator;
struct testing_channel test_channel;
struct aws_http_connection *connection;
struct aws_crt_statistics_handler *monitor;
struct aws_array_list requests;
struct aws_byte_buf large_body_buf;
};
static struct monitor_test_context s_test_context;
static uint64_t s_clock_value = 0;
static int s_mock_clock(uint64_t *timestamp) {
*timestamp = s_clock_value;
return AWS_OP_SUCCESS;
}
/* big enough to spill into a second io message when headers/method included */
#define TICK_BODY_SIZE 16384
#define MAX_BODY_SIZE (1024 * 1024)
static int s_init_monitor_test(struct aws_allocator *allocator, struct aws_crt_statistics_handler *monitor) {
aws_http_library_init(allocator);
s_clock_value = 0;
AWS_ZERO_STRUCT(s_test_context);
s_test_context.allocator = allocator;
struct aws_testing_channel_options test_channel_options = {.clock_fn = s_mock_clock};
testing_channel_init(&s_test_context.test_channel, allocator, &test_channel_options);
s_test_context.test_channel.channel_shutdown = s_testing_channel_shutdown_callback;
s_test_context.test_channel.channel_shutdown_user_data = &s_test_context;
struct aws_http1_connection_options http1_options;
AWS_ZERO_STRUCT(http1_options);
struct aws_http_connection *connection =
aws_http_connection_new_http1_1_client(allocator, true, SIZE_MAX, &http1_options);
ASSERT_NOT_NULL(connection);
connection->next_stream_id = 1;
struct aws_channel_slot *slot = aws_channel_slot_new(s_test_context.test_channel.channel);
ASSERT_NOT_NULL(slot);
ASSERT_SUCCESS(aws_channel_slot_insert_end(s_test_context.test_channel.channel, slot));
ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, &connection->channel_handler));
connection->vtable->on_channel_handler_installed(&connection->channel_handler, slot);
s_test_context.connection = connection;
testing_channel_drain_queued_tasks(&s_test_context.test_channel);
s_test_context.monitor = monitor;
aws_channel_set_statistics_handler(s_test_context.test_channel.channel, s_test_context.monitor);
ASSERT_SUCCESS(
aws_array_list_init_dynamic(&s_test_context.requests, allocator, 1, sizeof(struct http_request_info)));
aws_byte_buf_init(&s_test_context.large_body_buf, allocator, MAX_BODY_SIZE);
memset(s_test_context.large_body_buf.buffer, '0', MAX_BODY_SIZE);
s_test_context.large_body_buf.len = MAX_BODY_SIZE;
return AWS_OP_SUCCESS;
}
static void s_clean_up_monitor_test(void) {
size_t request_count = aws_array_list_length(&s_test_context.requests);
for (size_t i = 0; i < request_count; ++i) {
struct http_request_info *request_info = NULL;
aws_array_list_get_at_ptr(&s_test_context.requests, (void **)&request_info, i);
if (request_info) {
aws_http_message_destroy(request_info->request);
aws_http_stream_release(request_info->stream);
aws_input_stream_release(request_info->body);
}
}
aws_http_connection_release(s_test_context.connection);
testing_channel_clean_up(&s_test_context.test_channel);
aws_array_list_clean_up(&s_test_context.requests);
aws_byte_buf_clean_up(&s_test_context.large_body_buf);
aws_http_library_clean_up();
}
static void s_apply_stats_event_to_testing_channel(struct http_monitor_test_stats_event *event) {
(void)event;
struct aws_channel *channel = s_test_context.test_channel.channel;
struct aws_channel_slot *first_slot = aws_channel_get_first_slot(channel);
struct aws_channel_handler *first_handler = first_slot->handler;
struct testing_channel_handler *testing_handler = first_handler->impl;
testing_handler->stats = event->socket_stats;
struct aws_channel_handler *second_handler = first_slot->adj_right->handler;
struct aws_http_connection *connection = second_handler->impl;
struct aws_crt_statistics_http1_channel *h1_stats = aws_h1_connection_get_statistics(connection);
*h1_stats = event->http_stats;
}
/*
Test Pattern 1 (monitor calculations and side affect):
Create a testing channel
Create and attach a (1000, 1) http connection monitor
Loop over test-specific event list: [(t_i, stats_i)]
Inject socket and http statistics
SetCurrentChannelTime(t_i)
cause ProcessStatistics() to be invoked by running channel tasks
verify monitor's state is as expected
if met the monitoring failure condition
verify the channel was shutdown
*/
static int s_do_http_monitoring_test(
struct aws_allocator *allocator,
struct aws_http_connection_monitoring_options *monitoring_options,
struct http_monitor_test_stats_event *events,
size_t event_count) {
s_clock_value = 0;
s_init_monitor_test(
allocator, aws_crt_statistics_handler_new_http_connection_monitor(allocator, monitoring_options));
struct aws_statistics_handler_http_connection_monitor_impl *monitor_impl = s_test_context.monitor->impl;
for (size_t i = 0; i < event_count; ++i) {
struct http_monitor_test_stats_event *event = events + i;
s_clock_value = aws_timestamp_convert(event->timestamp, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
switch (event->event_type) {
case MTET_EMPTY:
break;
case MTET_STATS:
s_apply_stats_event_to_testing_channel(event);
break;
}
testing_channel_drain_queued_tasks(&s_test_context.test_channel);
ASSERT_TRUE(monitor_impl->throughput_failure_time_ms == event->expected_consecutive_failure_time_ms);
ASSERT_TRUE(monitor_impl->last_measured_throughput == event->expected_throughput);
if (monitor_impl->throughput_failure_time_ms >
aws_timestamp_convert(
monitoring_options->allowable_throughput_failure_interval_seconds,
AWS_TIMESTAMP_SECS,
AWS_TIMESTAMP_MILLIS,
NULL)) {
ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_test_context.test_channel));
}
}
s_clean_up_monitor_test();
return AWS_OP_SUCCESS;
}
static struct aws_http_connection_monitoring_options s_test_options = {
.allowable_throughput_failure_interval_seconds = 1,
.minimum_throughput_bytes_per_second = 1000,
};
/*
* A test where the combined read and write throughput stays above the threshold
*/
static struct http_monitor_test_stats_event s_test_rw_above_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 500,
.bytes_written = 500,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 3,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
{
.event_type = MTET_STATS,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 500,
.bytes_written = 500,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 3,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
};
struct observer_cb_data {
bool invoked;
size_t nonce;
size_t number_of_stats;
struct aws_crt_statistics_socket socket_stats;
struct aws_crt_statistics_http1_channel http_stats;
};
static void s_observer_cb(size_t connection_nonce, const struct aws_array_list *stats, void *user_data) {
struct observer_cb_data *cb_data = user_data;
cb_data->invoked = true;
cb_data->nonce = connection_nonce;
cb_data->number_of_stats = aws_array_list_length(stats);
for (size_t i = 0; i < cb_data->number_of_stats; ++i) {
struct aws_crt_statistics_base *base_ptr = NULL;
aws_array_list_get_at(stats, (void **)&base_ptr, i);
if (base_ptr->category == AWSCRT_STAT_CAT_SOCKET) {
cb_data->socket_stats = *(struct aws_crt_statistics_socket *)base_ptr;
}
if (base_ptr->category == AWSCRT_STAT_CAT_HTTP1_CHANNEL) {
cb_data->http_stats = *(struct aws_crt_statistics_http1_channel *)base_ptr;
}
}
}
static int s_test_http_connection_monitor_rw_above(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
struct observer_cb_data cb_data;
AWS_ZERO_STRUCT(cb_data);
s_test_options.statistics_observer_fn = s_observer_cb;
s_test_options.statistics_observer_user_data = &cb_data;
int result = s_do_http_monitoring_test(
allocator, &s_test_options, s_test_rw_above_events, AWS_ARRAY_SIZE(s_test_rw_above_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
ASSERT_TRUE(cb_data.invoked);
ASSERT_TRUE(cb_data.nonce > 0);
ASSERT_UINT_EQUALS(2U, cb_data.number_of_stats);
ASSERT_UINT_EQUALS(s_test_rw_above_events[0].socket_stats.bytes_written, cb_data.socket_stats.bytes_written);
ASSERT_UINT_EQUALS(s_test_rw_above_events[0].socket_stats.bytes_read, cb_data.socket_stats.bytes_read);
ASSERT_UINT_EQUALS(
s_test_rw_above_events[0].http_stats.current_outgoing_stream_id, cb_data.http_stats.current_outgoing_stream_id);
ASSERT_UINT_EQUALS(
s_test_rw_above_events[0].http_stats.current_incoming_stream_id, cb_data.http_stats.current_incoming_stream_id);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_rw_above, s_test_http_connection_monitor_rw_above);
/*
* A test where the read throughput stays above the threshold
*/
static struct http_monitor_test_stats_event s_test_r_above_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 1000,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 3,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
{
.event_type = MTET_STATS,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 1000,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 3,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
};
static int s_test_http_connection_monitor_r_above(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator, &s_test_options, s_test_r_above_events, AWS_ARRAY_SIZE(s_test_r_above_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_r_above, s_test_http_connection_monitor_r_above);
/*
* A test where the write throughput stays above the threshold
*/
static struct http_monitor_test_stats_event s_test_w_above_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_written = 1000,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 3,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
{
.event_type = MTET_STATS,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_written = 1000,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 3,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
};
static int s_test_http_connection_monitor_w_above(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator, &s_test_options, s_test_w_above_events, AWS_ARRAY_SIZE(s_test_w_above_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_w_above, s_test_http_connection_monitor_w_above);
/*
* A more realistic test where the write throughput stays above and then the read throughput stays above
* A fractional event in the middle contains both read and writes
*/
static struct http_monitor_test_stats_event s_test_write_then_read_above_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_written = 1000,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
{
.event_type = MTET_STATS,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_written = 1000,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
{
.event_type = MTET_STATS,
.timestamp = 3 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_written = 100,
.bytes_read = 500,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = 1000,
.pending_outgoing_stream_ms = 200,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
{
.event_type = MTET_STATS,
.timestamp = 4 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 1000,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
};
static int s_test_http_connection_monitor_write_then_read_above(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator,
&s_test_options,
s_test_write_then_read_above_events,
AWS_ARRAY_SIZE(s_test_write_then_read_above_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_write_then_read_above, s_test_http_connection_monitor_write_then_read_above);
/*
* A test where the throughput is below the threshold but the requests do not last long enough to register the
* failure.
*/
static struct http_monitor_test_stats_event s_test_below_but_undetectable_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_written = 100,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 100,
},
{
.event_type = MTET_STATS,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 100,
.bytes_written = 100,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 3,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 200,
},
{
.event_type = MTET_STATS,
.timestamp = 3 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 100,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 3,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 100,
},
};
static int s_test_http_connection_monitor_below_but_undetectable(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator,
&s_test_options,
s_test_below_but_undetectable_events,
AWS_ARRAY_SIZE(s_test_below_but_undetectable_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(
test_http_connection_monitor_below_but_undetectable,
s_test_http_connection_monitor_below_but_undetectable);
/*
* A test where we drop below the threshold with a combination of read and write io
*/
static struct http_monitor_test_stats_event s_test_below_rw_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 500,
.bytes_written = 500,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1000,
},
{
.event_type = MTET_STATS,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 249,
.bytes_written = 125,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = 500,
.pending_outgoing_stream_ms = 250,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 500,
.expected_throughput = 998,
},
};
static int s_test_http_connection_monitor_rw_below(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator, &s_test_options, s_test_below_rw_events, AWS_ARRAY_SIZE(s_test_below_rw_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_rw_below, s_test_http_connection_monitor_rw_below);
/*
* A test where we drop below the threshold then recover
*/
static struct http_monitor_test_stats_event s_test_below_then_above_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_written = 1500,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1500,
},
{
.event_type = MTET_STATS,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 499,
.bytes_written = 250,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = 500,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = AWS_TIMESTAMP_MILLIS,
.expected_throughput = 999,
},
{
.event_type = MTET_STATS,
.timestamp = 3 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 2000,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 2000,
},
};
static int s_test_http_connection_monitor_below_then_above(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator, &s_test_options, s_test_below_then_above_events, AWS_ARRAY_SIZE(s_test_below_then_above_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_below_then_above, s_test_http_connection_monitor_below_then_above);
/*
* Test that verifies that the failure time is reset when there's no streams
*
*/
static struct http_monitor_test_stats_event s_test_failure_reset_when_empty_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_written = 1500,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1500,
},
{
.event_type = MTET_STATS,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 499,
.bytes_written = 250,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = 500,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = AWS_TIMESTAMP_MILLIS,
.expected_throughput = 999,
},
{
.event_type = MTET_STATS,
.timestamp = 3 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 0,
},
};
static int s_test_http_connection_monitor_failure_reset_when_empty(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator,
&s_test_options,
s_test_failure_reset_when_empty_events,
AWS_ARRAY_SIZE(s_test_failure_reset_when_empty_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(
test_http_connection_monitor_failure_reset_when_empty,
s_test_http_connection_monitor_failure_reset_when_empty);
/*
* Edge case test when throughput calculations overflow
*/
static struct http_monitor_test_stats_event s_test_bytes_overflow_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = UINT64_MAX / 2 + 10,
.bytes_written = UINT64_MAX / 2 + 10,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = UINT64_MAX,
},
};
static int s_test_http_connection_monitor_bytes_overflow(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator, &s_test_options, s_test_bytes_overflow_events, AWS_ARRAY_SIZE(s_test_bytes_overflow_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_bytes_overflow, s_test_http_connection_monitor_bytes_overflow);
/*
* Another edge case test when throughput calculations overflow due to time scaling
*/
static struct http_monitor_test_stats_event s_test_time_overflow_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = UINT64_MAX / 2 + 10,
.bytes_written = UINT64_MAX / 2 - 10,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = UINT64_MAX,
},
};
static int s_test_http_connection_monitor_time_overflow(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator, &s_test_options, s_test_time_overflow_events, AWS_ARRAY_SIZE(s_test_time_overflow_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_time_overflow, s_test_http_connection_monitor_time_overflow);
/*
* Test that verifies the channel shuts down when we exceed the failure time threshold
*/
static struct http_monitor_test_stats_event s_test_shutdown_events[] = {
{
.event_type = MTET_STATS,
.timestamp = AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_written = 1500,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 0,
.expected_throughput = 1500,
},
{
.event_type = MTET_STATS,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 499,
.bytes_written = 250,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.pending_outgoing_stream_ms = 500,
.current_incoming_stream_id = 1,
.current_outgoing_stream_id = 1,
},
.expected_consecutive_failure_time_ms = AWS_TIMESTAMP_MILLIS,
.expected_throughput = 999,
},
{
.event_type = MTET_STATS,
.timestamp = 3 * AWS_TIMESTAMP_MILLIS,
.socket_stats =
{
.category = AWSCRT_STAT_CAT_SOCKET,
.bytes_read = 250,
},
.http_stats =
{
.category = AWSCRT_STAT_CAT_HTTP1_CHANNEL,
.pending_incoming_stream_ms = AWS_TIMESTAMP_MILLIS,
.current_incoming_stream_id = 1,
},
.expected_consecutive_failure_time_ms = 2000,
.expected_throughput = 250,
},
};
static int s_test_http_connection_monitor_shutdown(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_monitoring_test(
allocator, &s_test_options, s_test_shutdown_events, AWS_ARRAY_SIZE(s_test_shutdown_events));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_connection_monitor_shutdown, s_test_http_connection_monitor_shutdown);
/*
Pattern 2 (http statistics verification)
Create an io testing channel (test_handler <-> http_handler)
Create and attach a mock stats handler
Loop over h1 connection events [(t_i, http_event)]
SetCurrentChannelTime(t_i)
ApplyEvent(http_event)
where events include instances of verification of mock-captured http stat state
*/
enum monitor_test_http_stats_event_type {
MTHSET_ADD_OUTGOING_STREAM,
MTHSET_ADD_RESPONSE_DATA,
MTHSET_FLUSH,
MTHSET_TICK,
MTHSET_VERIFY
};
struct test_http_stats_event {
uint64_t timestamp;
enum monitor_test_http_stats_event_type event_type;
const char *response_stream_data;
size_t request_body_size;
struct aws_crt_statistics_http1_channel expected_stats;
};
struct mock_http_connection_monitor_impl {
struct aws_http_connection_monitoring_options options;
struct aws_crt_statistics_http1_channel last_seen_stats;
};
static void s_mock_process_statistics(
struct aws_crt_statistics_handler *handler,
struct aws_crt_statistics_sample_interval *interval,
struct aws_array_list *stats_list,
void *context) {
(void)interval;
(void)context;
struct mock_http_connection_monitor_impl *impl = handler->impl;
size_t stats_count = aws_array_list_length(stats_list);
for (size_t i = 0; i < stats_count; ++i) {
struct aws_crt_statistics_base *stats_base = NULL;
if (aws_array_list_get_at(stats_list, &stats_base, i)) {
continue;
}
switch (stats_base->category) {
case AWSCRT_STAT_CAT_HTTP1_CHANNEL: {
struct aws_crt_statistics_http1_channel *http1_stats =
(struct aws_crt_statistics_http1_channel *)stats_base;
impl->last_seen_stats = *http1_stats;
break;
}
default:
break;
}
}
}
static void s_mock_destroy(struct aws_crt_statistics_handler *handler) {
if (handler == NULL) {
return;
}
aws_mem_release(handler->allocator, handler);
}
static uint64_t s_mock_get_report_interval_ms(struct aws_crt_statistics_handler *handler) {
(void)handler;
return 1000;
}
static struct aws_crt_statistics_handler_vtable s_http_mock_monitor_vtable = {
.process_statistics = s_mock_process_statistics,
.destroy = s_mock_destroy,
.get_report_interval_ms = s_mock_get_report_interval_ms,
};
static struct aws_crt_statistics_handler *s_aws_crt_statistics_handler_new_http_mock(struct aws_allocator *allocator) {
struct aws_crt_statistics_handler *handler = NULL;
struct mock_http_connection_monitor_impl *impl = NULL;
if (!aws_mem_acquire_many(
allocator,
2,
&handler,
sizeof(struct aws_crt_statistics_handler),
&impl,
sizeof(struct mock_http_connection_monitor_impl))) {
return NULL;
}
AWS_ZERO_STRUCT(*handler);
AWS_ZERO_STRUCT(*impl);
handler->vtable = &s_http_mock_monitor_vtable;
handler->allocator = allocator;
handler->impl = impl;
return handler;
}
static int s_aws_http_on_incoming_body(
struct aws_http_stream *stream,
const struct aws_byte_cursor *data,
void *user_data) {
(void)stream;
(void)data;
(void)user_data;
return AWS_OP_SUCCESS;
}
static void s_aws_http_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
(void)stream;
(void)error_code;
size_t request_index = (size_t)user_data;
struct http_request_info *request_info = NULL;
aws_array_list_get_at_ptr(&s_test_context.requests, (void **)&request_info, request_index);
if (request_info != NULL) {
request_info->response_completed = true;
}
}
static void s_add_outgoing_stream(struct test_http_stats_event *event) {
(void)event;
struct http_request_info request_info;
AWS_ZERO_STRUCT(request_info);
request_info.request = aws_http_message_new_request(s_test_context.allocator);
aws_http_message_set_request_method(request_info.request, aws_byte_cursor_from_c_str("GET"));
struct aws_http_header host_header = {
.name = aws_byte_cursor_from_c_str("host"),
.value = aws_byte_cursor_from_c_str("www.derp.com"),
};
aws_http_message_add_header(request_info.request, host_header);
aws_http_message_set_request_path(request_info.request, aws_byte_cursor_from_c_str("/index.html?queryparam=value"));
AWS_FATAL_ASSERT(event->request_body_size <= MAX_BODY_SIZE);
if (event->request_body_size > 0) {
char cl_buffer[256];
snprintf(cl_buffer, sizeof(cl_buffer), "%zu", event->request_body_size);
struct aws_http_header content_length_header = {
.name = aws_byte_cursor_from_c_str("content-length"),
.value = aws_byte_cursor_from_c_str(cl_buffer),
};
aws_http_message_add_header(request_info.request, content_length_header);
struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&s_test_context.large_body_buf);
body_cursor.len = event->request_body_size;
request_info.body = aws_input_stream_new_from_cursor(s_test_context.allocator, &body_cursor);
aws_http_message_set_body_stream(request_info.request, request_info.body);
}
struct aws_http_make_request_options request_options;
AWS_ZERO_STRUCT(request_options);
request_options.request = request_info.request;
request_options.on_complete = s_aws_http_on_stream_complete;
request_options.on_response_body = s_aws_http_on_incoming_body;
request_options.self_size = sizeof(struct aws_http_make_request_options);
request_options.user_data = (void *)aws_array_list_length(&s_test_context.requests);
request_info.stream = aws_http_connection_make_request(s_test_context.connection, &request_options);
aws_http_stream_activate(request_info.stream);
aws_array_list_push_back(&s_test_context.requests, &request_info);
}
static void s_add_response_data(struct test_http_stats_event *event) {
testing_channel_push_read_str(&s_test_context.test_channel, event->response_stream_data);
}
static int s_do_http_statistics_test(
struct aws_allocator *allocator,
struct test_http_stats_event *events,
size_t event_count) {
s_clock_value = 0;
s_init_monitor_test(allocator, s_aws_crt_statistics_handler_new_http_mock(allocator));
struct mock_http_connection_monitor_impl *monitor_impl = s_test_context.monitor->impl;
for (size_t i = 0; i < event_count; ++i) {
struct test_http_stats_event *event = events + i;
s_clock_value = aws_timestamp_convert(event->timestamp, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
switch (event->event_type) {
case MTHSET_FLUSH:
testing_channel_drain_queued_tasks(&s_test_context.test_channel);
break;
case MTHSET_ADD_OUTGOING_STREAM:
s_add_outgoing_stream(event);
break;
case MTHSET_ADD_RESPONSE_DATA:
s_add_response_data(event);
break;
case MTHSET_TICK:
testing_channel_run_currently_queued_tasks(&s_test_context.test_channel);
break;
case MTHSET_VERIFY:
ASSERT_TRUE(
event->expected_stats.pending_incoming_stream_ms ==
monitor_impl->last_seen_stats.pending_incoming_stream_ms);
ASSERT_TRUE(
event->expected_stats.pending_outgoing_stream_ms ==
monitor_impl->last_seen_stats.pending_outgoing_stream_ms);
ASSERT_TRUE(
event->expected_stats.current_incoming_stream_id ==
monitor_impl->last_seen_stats.current_incoming_stream_id);
ASSERT_TRUE(
event->expected_stats.current_outgoing_stream_id ==
monitor_impl->last_seen_stats.current_outgoing_stream_id);
break;
}
}
size_t request_count = aws_array_list_length(&s_test_context.requests);
for (size_t i = 0; i < request_count; ++i) {
struct http_request_info *request_info = NULL;
aws_array_list_get_at_ptr(&s_test_context.requests, (void **)&request_info, i);
ASSERT_TRUE(request_info && request_info->response_completed);
}
s_clean_up_monitor_test();
return AWS_OP_SUCCESS;
}
static struct test_http_stats_event s_http_stats_test_trivial[] = {
{
.event_type = MTHSET_VERIFY,
.timestamp = 0,
.expected_stats =
{
.pending_outgoing_stream_ms = 0,
.pending_incoming_stream_ms = 0,
.current_outgoing_stream_id = 0,
.current_incoming_stream_id = 0,
},
},
};
static int s_test_http_stats_trivial(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result =
s_do_http_statistics_test(allocator, s_http_stats_test_trivial, AWS_ARRAY_SIZE(s_http_stats_test_trivial));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_stats_trivial, s_test_http_stats_trivial);
static struct test_http_stats_event s_http_stats_test_basic_request[] = {
{
.event_type = MTHSET_ADD_OUTGOING_STREAM,
.timestamp = 100,
.request_body_size = TICK_BODY_SIZE,
},
{
.event_type = MTHSET_TICK,
.timestamp = 100,
},
{
.event_type = MTHSET_FLUSH,
.timestamp = 200,
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 500,
.response_stream_data = "HTTP/1.1 200 OK\r\n",
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 700,
.response_stream_data = "Content-Length: 9\r\n\r\nSomething",
},
{
.event_type = MTHSET_FLUSH,
.timestamp = 700,
},
{
.event_type = MTHSET_FLUSH,
.timestamp = AWS_TIMESTAMP_MILLIS,
},
{
.event_type = MTHSET_VERIFY,
.timestamp = AWS_TIMESTAMP_MILLIS,
.expected_stats =
{
.pending_outgoing_stream_ms = 100, /* [100, 200] */
.pending_incoming_stream_ms = 600, /* [100, 700] */
.current_outgoing_stream_id = 0,
.current_incoming_stream_id = 0,
},
},
};
static int s_test_http_stats_basic_request(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_statistics_test(
allocator, s_http_stats_test_basic_request, AWS_ARRAY_SIZE(s_http_stats_test_basic_request));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_stats_basic_request, s_test_http_stats_basic_request);
static struct test_http_stats_event s_http_stats_test_split_across_gather_boundary[] = {
{
.event_type = MTHSET_ADD_OUTGOING_STREAM,
.timestamp = AWS_TIMESTAMP_MILLIS - 100,
.request_body_size = 2 * TICK_BODY_SIZE,
},
{
.event_type = MTHSET_TICK,
.timestamp = AWS_TIMESTAMP_MILLIS - 100,
},
{
.event_type = MTHSET_TICK,
.timestamp = AWS_TIMESTAMP_MILLIS,
},
{
.event_type = MTHSET_VERIFY,
.timestamp = AWS_TIMESTAMP_MILLIS,
.expected_stats =
{
.pending_outgoing_stream_ms = 100,
.pending_incoming_stream_ms = 100,
.current_outgoing_stream_id = 1,
.current_incoming_stream_id = 1,
},
},
{
.event_type = MTHSET_FLUSH,
.timestamp = AWS_TIMESTAMP_MILLIS + 100,
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = AWS_TIMESTAMP_MILLIS + 500,
.response_stream_data = "HTTP/1.1 200 OK\r\n",
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = AWS_TIMESTAMP_MILLIS + 700,
.response_stream_data = "Content-Length: 9\r\n\r\nSomething",
},
{
.event_type = MTHSET_FLUSH,
.timestamp = AWS_TIMESTAMP_MILLIS + 700,
},
{
.event_type = MTHSET_FLUSH,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
},
{
.event_type = MTHSET_VERIFY,
.timestamp = 2 * AWS_TIMESTAMP_MILLIS,
.expected_stats =
{
.pending_outgoing_stream_ms = 100,
.pending_incoming_stream_ms = 700,
.current_outgoing_stream_id = 0,
.current_incoming_stream_id = 0,
},
},
};
static int s_test_http_stats_split_across_gather_boundary(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_statistics_test(
allocator,
s_http_stats_test_split_across_gather_boundary,
AWS_ARRAY_SIZE(s_http_stats_test_split_across_gather_boundary));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_stats_split_across_gather_boundary, s_test_http_stats_split_across_gather_boundary);
/*
* Pipeline 3 requests before beginning response data.
*
* The request body sizes have a total length of 4 * TICK_BODY_SIZE which means it will take 5 ticks
* to completely "write" them.
*/
static struct test_http_stats_event s_http_stats_test_pipelined[] = {
{
.event_type = MTHSET_ADD_OUTGOING_STREAM,
.timestamp = 100,
.request_body_size = 2 * TICK_BODY_SIZE,
},
{
.event_type = MTHSET_TICK,
.timestamp = 100,
},
{
.event_type = MTHSET_ADD_OUTGOING_STREAM,
.timestamp = 200,
.request_body_size = 1 * TICK_BODY_SIZE,
},
{
.event_type = MTHSET_ADD_OUTGOING_STREAM,
.timestamp = 300,
.request_body_size = 1 * TICK_BODY_SIZE,
},
{
.event_type = MTHSET_TICK,
.timestamp = 400,
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 500,
.response_stream_data = "HTTP/1.1 200 OK\r\n",
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 600,
.response_stream_data = "Content-Length: 9\r\n\r\nSomething",
},
{
.event_type = MTHSET_FLUSH,
.timestamp = 690,
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 700,
.response_stream_data = "HTTP/1.1 200 OK\r\n",
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 800,
.response_stream_data = "Content-Length: 9\r\n\r\nSomethingHTTP/1.1 200 OK\r\n",
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 900,
.response_stream_data = "Content-Length: 9\r\n\r\nSomething",
},
{
.event_type = MTHSET_TICK,
.timestamp = AWS_TIMESTAMP_MILLIS,
},
{
.event_type = MTHSET_VERIFY,
.timestamp = AWS_TIMESTAMP_MILLIS,
.expected_stats =
{
.pending_outgoing_stream_ms = 590, /* [100, 690] */
.pending_incoming_stream_ms = 800, /* [100, 900] */
.current_outgoing_stream_id = 0,
.current_incoming_stream_id = 0,
},
},
};
static int s_test_http_stats_pipelined(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result =
s_do_http_statistics_test(allocator, s_http_stats_test_pipelined, AWS_ARRAY_SIZE(s_http_stats_test_pipelined));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_stats_pipelined, s_test_http_stats_pipelined);
static struct test_http_stats_event s_http_stats_test_multiple_requests_with_gap[] = {
{
.event_type = MTHSET_ADD_OUTGOING_STREAM,
.timestamp = 100,
.request_body_size = TICK_BODY_SIZE,
},
{
.event_type = MTHSET_TICK,
.timestamp = 100,
},
{
.event_type = MTHSET_FLUSH,
.timestamp = 200,
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 300,
.response_stream_data = "HTTP/1.1 200 OK\r\n",
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 400,
.response_stream_data = "Content-Length: 9\r\n\r\nSomething",
},
{
.event_type = MTHSET_FLUSH,
.timestamp = 400,
},
{
.event_type = MTHSET_ADD_OUTGOING_STREAM,
.timestamp = 500,
.request_body_size = TICK_BODY_SIZE,
},
{
.event_type = MTHSET_TICK,
.timestamp = 500,
},
{
.event_type = MTHSET_FLUSH,
.timestamp = 600,
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 700,
.response_stream_data = "HTTP/1.1 200 OK\r\n",
},
{
.event_type = MTHSET_ADD_RESPONSE_DATA,
.timestamp = 800,
.response_stream_data = "Content-Length: 9\r\n\r\nSomething",
},
{
.event_type = MTHSET_FLUSH,
.timestamp = AWS_TIMESTAMP_MILLIS,
},
{
.event_type = MTHSET_VERIFY,
.timestamp = AWS_TIMESTAMP_MILLIS,
.expected_stats =
{
.pending_outgoing_stream_ms = 200, /* [100, 200] + [500, 600]*/
.pending_incoming_stream_ms = 600, /* [100, 400] + [500, 800] */
.current_outgoing_stream_id = 0,
.current_incoming_stream_id = 0,
},
},
};
static int s_test_http_stats_multiple_requests_with_gap(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
int result = s_do_http_statistics_test(
allocator,
s_http_stats_test_multiple_requests_with_gap,
AWS_ARRAY_SIZE(s_http_stats_test_multiple_requests_with_gap));
ASSERT_TRUE(result == AWS_OP_SUCCESS);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(test_http_stats_multiple_requests_with_gap, s_test_http_stats_multiple_requests_with_gap);
|