Debian Codegraph @

crt/aws-c-http/tests/test_connection_monitor.c

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