Debian Codegraph @

crt/aws-c-http/tests/test_stream_manager.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 "h2_test_helper.h"
7 #include "stream_test_helper.h"
8
9 #include <aws/http/http2_stream_manager.h>
10
11 #include <aws/http/connection.h>
12 #include <aws/http/connection_manager.h>
13 #include <aws/http/private/connection_impl.h>
14 #include <aws/http/private/connection_manager_system_vtable.h>
15 #include <aws/http/private/h1_stream.h>
16 #include <aws/http/private/h2_connection.h>
17 #include <aws/http/private/http2_stream_manager_impl.h>
18 #include <aws/http/private/proxy_impl.h>
19 #include <aws/http/proxy.h>
20 #include <aws/http/statistics.h>
21
22 #include <aws/io/stream.h>
23 #include <aws/io/uri.h>
24
25 #include <aws/common/byte_buf.h>
26 #include <aws/common/condition_variable.h>
27 #include <aws/common/string.h>
28
29 #include <aws/testing/aws_test_harness.h>
30 #include <aws/testing/io_testing_channel.h>
31
32 #define TEST_CASE(NAME) \
33 AWS_TEST_CASE(NAME, s_test_##NAME); \
34 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
35
36 #define DEFINE_HEADER(NAME, VALUE) \
37 { \
38 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \
39 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \
40 }
41
42 struct sm_tester_options {
43 struct aws_allocator *alloc;
44 struct aws_http_connection_manager_system_vtable *mock_table;
45 bool no_http2;
46 size_t max_connections;
47 size_t ideal_concurrent_streams_per_connection;
48 size_t max_concurrent_streams_per_connection;
49
50 const struct aws_http_connection_monitoring_options *monitor_opt;
51
52 struct aws_byte_cursor *uri_cursor;
53 const enum aws_log_level *log_level;
54 bool prior_knowledge;
55 bool close_connection_on_server_error;
56 size_t connection_ping_period_ms;
57 size_t connection_ping_timeout_ms;
58
59 /* HTTP/2 initial settings */
60 const struct aws_http2_setting *initial_settings_array;
61 size_t num_initial_settings;
62
63 bool manual_window;
64 size_t initial_window_size;
65 };
66
67 static struct aws_logger s_logger;
68 struct sm_tester {
69 struct aws_allocator *allocator;
70 struct aws_event_loop_group *event_loop_group;
71 struct aws_host_resolver *host_resolver;
72
73 struct aws_client_bootstrap *client_bootstrap;
74
75 struct aws_http2_stream_manager *stream_manager;
76 struct aws_http_connection_manager *connection_manager;
77
78 struct aws_uri endpoint;
79 struct aws_tls_ctx *tls_ctx;
80 struct aws_tls_ctx_options tls_ctx_options;
81 struct aws_tls_connection_options tls_connection_options;
82 struct aws_http_proxy_options *verify_proxy_options;
83
84 struct aws_mutex lock;
85 struct aws_condition_variable signal;
86
87 struct aws_array_list streams;
88 size_t wait_for_stream_acquire_count;
89 size_t acquiring_stream_errors;
90 int error_code;
91
92 size_t wait_for_stream_completed_count;
93 size_t stream_completed_count;
94 struct aws_atomic_var stream_destroyed_count;
95 size_t stream_complete_errors;
96 size_t stream_200_count;
97 size_t stream_status_not_200_count;
98 int stream_completed_error_code;
99
100 bool is_shutdown_complete;
101
102 /* Fake HTTP/2 connection */
103 size_t wait_for_fake_connection_count;
104
105 size_t bad_connection_to_offer;
106 size_t offer_bad_connection_count;
107
108 /* Mock will wait for delay_finished to offer connections synced. Once flip async finished to true, you should offer
109 * the count connections */
110 size_t delay_offer_connection_count;
111 bool delay_finished;
112
113 struct aws_array_list fake_connections;
114 bool release_sm_during_connection_acquiring;
115
116 uint32_t max_con_stream_remote;
117
118 /* To invoke the real on_setup */
119 aws_http_on_client_connection_setup_fn *on_setup;
120
121 size_t length_sent;
122 };
123
124 static struct sm_tester s_tester;
125
126 struct sm_fake_connection {
127 struct testing_channel testing_channel;
128 struct h2_fake_peer peer;
129 struct aws_http_client_connection_options options;
130 struct aws_http_connection *connection;
131 size_t last_completed_frame_index; /* Track the last frame index we've completed */
132 };
133
134 static void s_testing_channel_shutdown(int error_code, void *user_data) {
135 struct sm_fake_connection *fake_connection = (struct sm_fake_connection *)user_data;
136 if (!fake_connection->connection) {
137 /* If there is no connection, which means the fake_connection is a bad connection and we should not invoke on
138 * shutdown as setup failed for them */
139 return;
140 }
141 if (fake_connection->options.on_shutdown) {
142 /* In real world, this is trigger by the bootstrap */
143 fake_connection->options.on_shutdown(
144 fake_connection->connection, error_code, fake_connection->options.user_data);
145 }
146 }
147
148 static struct sm_fake_connection *s_get_fake_connection(size_t i) {
149 AWS_FATAL_ASSERT(aws_array_list_length(&s_tester.fake_connections) > i);
150 struct sm_fake_connection *fake_connection = NULL;
151 aws_array_list_get_at(&s_tester.fake_connections, &fake_connection, i);
152 return fake_connection;
153 }
154
155 static struct sm_fake_connection *s_sm_fake_connection_new(void) {
156 struct sm_fake_connection *fake_connection =
157 aws_mem_calloc(s_tester.allocator, 1, sizeof(struct sm_fake_connection));
158
159 struct aws_testing_channel_options options = {.clock_fn = aws_high_res_clock_get_ticks};
160
161 AWS_FATAL_ASSERT(
162 testing_channel_init(&fake_connection->testing_channel, s_tester.allocator, &options) == AWS_OP_SUCCESS);
163 fake_connection->testing_channel.channel_shutdown_user_data = fake_connection;
164 fake_connection->testing_channel.channel_shutdown = s_testing_channel_shutdown;
165 struct h2_fake_peer_options peer_options = {
166 .alloc = s_tester.allocator,
167 .testing_channel = &fake_connection->testing_channel,
168 .is_server = true,
169 };
170 AWS_FATAL_ASSERT(h2_fake_peer_init(&fake_connection->peer, &peer_options) == AWS_OP_SUCCESS);
171
172 return fake_connection;
173 }
174
175 static void s_sm_fake_connection_destroy(struct sm_fake_connection *fake_connection) {
176
177 AWS_FATAL_ASSERT(testing_channel_clean_up(&fake_connection->testing_channel) == AWS_OP_SUCCESS);
178 aws_mem_release(s_tester.allocator, fake_connection);
179 }
180
181 static bool s_is_shutdown_complete(void *context) {
182 (void)context;
183 return s_tester.is_shutdown_complete;
184 }
185
186 static int s_wait_on_shutdown_complete(void) {
187 ASSERT_SUCCESS(aws_mutex_lock(&s_tester.lock));
188
189 int signal_error = aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_is_shutdown_complete, NULL);
190
191 ASSERT_SUCCESS(aws_mutex_unlock(&s_tester.lock));
192 return signal_error;
193 }
194
195 static void s_sm_tester_on_sm_shutdown_complete(void *user_data) {
196 struct sm_tester *tester = user_data;
197 AWS_FATAL_ASSERT(tester == &s_tester);
198
199 aws_mutex_lock(&s_tester.lock);
200 s_tester.is_shutdown_complete = true;
201 aws_mutex_unlock(&s_tester.lock);
202 aws_condition_variable_notify_one(&s_tester.signal);
203 }
204
205 static int s_tester_init(struct sm_tester_options *options) {
206 struct aws_allocator *alloc = options->alloc;
207 aws_http_library_init(alloc);
208
209 s_tester.allocator = alloc;
210
211 struct aws_logger_standard_options logger_options = {
212 .level = options->log_level ? *options->log_level : AWS_LOG_LEVEL_TRACE,
213 .file = stderr,
214 };
215
216 aws_logger_init_standard(&s_logger, alloc, &logger_options);
217 aws_logger_set(&s_logger);
218
219 ASSERT_SUCCESS(aws_mutex_init(&s_tester.lock));
220 ASSERT_SUCCESS(aws_condition_variable_init(&s_tester.signal));
221
222 s_tester.event_loop_group = aws_event_loop_group_new_default(alloc, 0, NULL);
223
224 ASSERT_SUCCESS(aws_array_list_init_dynamic(&s_tester.streams, alloc, 1, sizeof(struct aws_http_stream *)));
225 ASSERT_SUCCESS(
226 aws_array_list_init_dynamic(&s_tester.fake_connections, alloc, 3, sizeof(struct sm_fake_connection *)));
227
228 struct aws_host_resolver_default_options resolver_options = {
229 .el_group = s_tester.event_loop_group,
230 .max_entries = 8,
231 };
232
233 s_tester.host_resolver = aws_host_resolver_new_default(s_tester.allocator, &resolver_options);
234 struct aws_client_bootstrap_options bootstrap_options = {
235 .event_loop_group = s_tester.event_loop_group,
236 .host_resolver = s_tester.host_resolver,
237 };
238 s_tester.client_bootstrap = aws_client_bootstrap_new(s_tester.allocator, &bootstrap_options);
239 ASSERT_NOT_NULL(s_tester.client_bootstrap);
240
241 struct aws_socket_options socket_options = {
242 .type = AWS_SOCKET_STREAM,
243 .domain = AWS_SOCKET_IPV4,
244 .connect_timeout_ms = (uint32_t)aws_timestamp_convert(10, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL),
245 };
246
247 if (options->uri_cursor) {
248 ASSERT_SUCCESS(aws_uri_init_parse(&s_tester.endpoint, alloc, options->uri_cursor));
249 } else {
250 struct aws_byte_cursor default_host = aws_byte_cursor_from_c_str("https://www.amazon.com");
251 ASSERT_SUCCESS(aws_uri_init_parse(&s_tester.endpoint, alloc, &default_host));
252 }
253
254 bool use_tls = true;
255 uint32_t port = 443;
256 if (!s_tester.endpoint.scheme.len && (s_tester.endpoint.port == 80 || s_tester.endpoint.port == 3280)) {
257 use_tls = false;
258 } else {
259 if (aws_byte_cursor_eq_c_str_ignore_case(&s_tester.endpoint.scheme, "http")) {
260 use_tls = false;
261 }
262 }
263 if (s_tester.endpoint.port) {
264 port = s_tester.endpoint.port;
265 } else if (aws_byte_cursor_eq_c_str_ignore_case(&s_tester.endpoint.scheme, "http")) {
266 port = 80;
267 }
268
269 if (use_tls) {
270 aws_tls_ctx_options_init_default_client(&s_tester.tls_ctx_options, alloc);
271 if (!options->no_http2) {
272 ASSERT_SUCCESS(aws_tls_ctx_options_set_alpn_list(&s_tester.tls_ctx_options, "h2"));
273 }
274 if (aws_byte_cursor_eq_c_str_ignore_case(&s_tester.endpoint.host_name, "localhost")) {
275 /* Turn off peer verification as a localhost cert used */
276 s_tester.tls_ctx_options.verify_peer = false;
277 }
278 s_tester.tls_ctx = aws_tls_client_ctx_new(alloc, &s_tester.tls_ctx_options);
279
280 ASSERT_NOT_NULL(s_tester.tls_ctx);
281 aws_tls_connection_options_init_from_ctx(&s_tester.tls_connection_options, s_tester.tls_ctx);
282 aws_tls_connection_options_set_server_name(
283 &s_tester.tls_connection_options, alloc, &s_tester.endpoint.host_name);
284 }
285 struct aws_http2_stream_manager_options sm_options = {
286 .bootstrap = s_tester.client_bootstrap,
287 .socket_options = &socket_options,
288 .tls_connection_options = use_tls ? &s_tester.tls_connection_options : NULL,
289 .host = s_tester.endpoint.host_name,
290 .port = port,
291 .ideal_concurrent_streams_per_connection = options->ideal_concurrent_streams_per_connection,
292 .max_concurrent_streams_per_connection = options->max_concurrent_streams_per_connection,
293 .max_connections = options->max_connections,
294 .shutdown_complete_user_data = &s_tester,
295 .shutdown_complete_callback = s_sm_tester_on_sm_shutdown_complete,
296 .monitoring_options = options->monitor_opt,
297 .close_connection_on_server_error = options->close_connection_on_server_error,
298 .connection_ping_period_ms = options->connection_ping_period_ms,
299 .connection_ping_timeout_ms = options->connection_ping_timeout_ms,
300 .http2_prior_knowledge = options->prior_knowledge,
301 .initial_settings_array = options->initial_settings_array,
302 .num_initial_settings = options->num_initial_settings,
303 .initial_window_size = options->initial_window_size,
304 .enable_read_back_pressure = options->manual_window,
305 };
306 s_tester.stream_manager = aws_http2_stream_manager_new(alloc, &sm_options);
307
308 s_tester.max_con_stream_remote = 100;
309 aws_atomic_init_int(&s_tester.stream_destroyed_count, 0);
310
311 return AWS_OP_SUCCESS;
312 }
313
314 static void s_release_all_streams(void) {
315
316 AWS_FATAL_ASSERT(aws_mutex_lock(&s_tester.lock) == AWS_OP_SUCCESS);
317 size_t release_count = aws_array_list_length(&s_tester.streams);
318 for (size_t i = 0; i < release_count; ++i) {
319 struct aws_http_stream *stream = NULL;
320 if (aws_array_list_back(&s_tester.streams, &stream)) {
321 continue;
322 }
323 aws_http_stream_release(stream);
324 aws_array_list_pop_back(&s_tester.streams);
325 }
326 AWS_FATAL_ASSERT(aws_mutex_unlock(&s_tester.lock) == AWS_OP_SUCCESS);
327 }
328
329 static int s_fake_connection_get_stream_received(struct sm_fake_connection *fake_connection) {
330 AWS_FATAL_ASSERT(h2_fake_peer_decode_messages_from_testing_channel(&fake_connection->peer) == AWS_OP_SUCCESS);
331 size_t frames_count = h2_decode_tester_frame_count(&fake_connection->peer.decode);
332 int streams_received = 0;
333 for (size_t i = 0; i < frames_count; ++i) {
334 struct h2_decoded_frame *frame = h2_decode_tester_get_frame(&fake_connection->peer.decode, i);
335 if (frame->end_stream) {
336 ++streams_received;
337 }
338 }
339 return streams_received;
340 }
341
342 /* complete first num_streams_to_complete. If num_streams_to_complete is zero, complete all the streams. */
343 static void s_fake_connection_complete_streams(
344 struct sm_fake_connection *fake_connection,
345 int num_streams_to_complete,
346 bool with_data) {
347 if (!fake_connection->connection) {
348 return;
349 }
350
351 testing_channel_drain_queued_tasks(&fake_connection->testing_channel);
352
353 AWS_FATAL_ASSERT(h2_fake_peer_decode_messages_from_testing_channel(&fake_connection->peer) == AWS_OP_SUCCESS);
354 struct aws_http_header response_headers_src[] = {
355 DEFINE_HEADER(":status", "404"),
356 DEFINE_HEADER("date", "Wed, 01 Apr 2020 23:02:49 GMT"),
357 };
358 struct aws_http_headers *response_headers = aws_http_headers_new(s_tester.allocator);
359 aws_http_headers_add_array(response_headers, response_headers_src, AWS_ARRAY_SIZE(response_headers_src));
360 size_t frames_count = h2_decode_tester_frame_count(&fake_connection->peer.decode);
361 int streams_completed = 0;
362 /* Start from the last completed frame index to avoid re-completing streams */
363 for (size_t i = fake_connection->last_completed_frame_index; i < frames_count; ++i) {
364 struct h2_decoded_frame *frame = h2_decode_tester_get_frame(&fake_connection->peer.decode, i);
365 if (frame->end_stream) {
366 struct aws_h2_frame *response_frame = aws_h2_frame_new_headers(
367 s_tester.allocator, frame->stream_id, response_headers, !with_data /*end_stream*/, 0, NULL);
368 AWS_FATAL_ASSERT(h2_fake_peer_send_frame(&fake_connection->peer, response_frame) == AWS_OP_SUCCESS);
369 if (with_data) {
370 AWS_FATAL_ASSERT(
371 h2_fake_peer_send_data_frame(
372 &fake_connection->peer,
373 frame->stream_id,
374 aws_byte_cursor_from_c_str("tests"),
375 true /*end_stream*/) == AWS_OP_SUCCESS);
376 }
377 /* Update the last completed frame index */
378 fake_connection->last_completed_frame_index = i + 1;
379 if (num_streams_to_complete && ++streams_completed >= num_streams_to_complete) {
380 break;
381 }
382 }
383 }
384 aws_http_headers_release(response_headers);
385 testing_channel_drain_queued_tasks(&fake_connection->testing_channel);
386 }
387
388 static void s_clean_fake_connections(void) {
389
390 size_t release_count = aws_array_list_length(&s_tester.fake_connections);
391 for (size_t i = 0; i < release_count; ++i) {
392 struct sm_fake_connection *fake_connection = NULL;
393 if (aws_array_list_back(&s_tester.fake_connections, &fake_connection)) {
394 continue;
395 }
396 aws_array_list_pop_back(&s_tester.fake_connections);
397 s_sm_fake_connection_destroy(fake_connection);
398 }
399 aws_array_list_clean_up(&s_tester.fake_connections);
400 }
401
402 static void s_drain_all_fake_connection_testing_channel(void) {
403 size_t count = aws_array_list_length(&s_tester.fake_connections);
404 for (size_t i = 0; i < count; ++i) {
405 struct sm_fake_connection *fake_connection = NULL;
406 aws_array_list_get_at(&s_tester.fake_connections, &fake_connection, i);
407 testing_channel_drain_queued_tasks(&fake_connection->testing_channel);
408 }
409 }
410
411 static void s_release_fake_connections(void) {
412 size_t count = aws_array_list_length(&s_tester.fake_connections);
413 for (size_t i = 0; i < count; ++i) {
414 struct sm_fake_connection *fake_connection = NULL;
415 aws_array_list_get_at(&s_tester.fake_connections, &fake_connection, i);
416 aws_http_connection_release(fake_connection->connection);
417 h2_fake_peer_clean_up(&fake_connection->peer);
418 }
419 s_drain_all_fake_connection_testing_channel();
420 }
421
422 static int s_complete_all_fake_connection_streams_impl(bool with_data) {
423 size_t count = aws_array_list_length(&s_tester.fake_connections);
424 for (size_t i = 0; i < count; ++i) {
425 struct sm_fake_connection *fake_connection = NULL;
426 ASSERT_SUCCESS(aws_array_list_get_at(&s_tester.fake_connections, &fake_connection, i));
427 /* complete all the streams from the fake connection */
428 s_fake_connection_complete_streams(fake_connection, 0 /*all streams*/, with_data);
429 testing_channel_drain_queued_tasks(&fake_connection->testing_channel);
430 }
431 return AWS_OP_SUCCESS;
432 }
433
434 static int s_complete_all_fake_connection_streams(void) {
435 return s_complete_all_fake_connection_streams_impl(false);
436 }
437
438 static int s_complete_all_fake_connection_streams_with_data(void) {
439 return s_complete_all_fake_connection_streams_impl(true);
440 }
441
442 static int s_tester_clean_up(void) {
443 s_release_all_streams();
444 if (s_tester.stream_manager) {
445 s_release_fake_connections();
446 aws_http2_stream_manager_release(s_tester.stream_manager);
447 }
448 s_drain_all_fake_connection_testing_channel();
449 s_wait_on_shutdown_complete();
450 s_clean_fake_connections();
451 aws_client_bootstrap_release(s_tester.client_bootstrap);
452
453 aws_host_resolver_release(s_tester.host_resolver);
454 aws_event_loop_group_release(s_tester.event_loop_group);
455
456 aws_tls_ctx_options_clean_up(&s_tester.tls_ctx_options);
457 aws_tls_connection_options_clean_up(&s_tester.tls_connection_options);
458 aws_tls_ctx_release(s_tester.tls_ctx);
459
460 aws_http_library_clean_up();
461
462 aws_mutex_clean_up(&s_tester.lock);
463 aws_condition_variable_clean_up(&s_tester.signal);
464 aws_array_list_clean_up(&s_tester.streams);
465 aws_uri_clean_up(&s_tester.endpoint);
466 aws_logger_clean_up(&s_logger);
467
468 return AWS_OP_SUCCESS;
469 }
470
471 static void s_sm_tester_on_stream_acquired(struct aws_http_stream *stream, int error_code, void *user_data) {
472 (void)user_data;
473
474 AWS_FATAL_ASSERT(aws_mutex_lock(&s_tester.lock) == AWS_OP_SUCCESS);
475
476 if (error_code) {
477 ++s_tester.acquiring_stream_errors;
478 ++s_tester.stream_completed_count; /* As the stream will never be completed through complete callback */
479 s_tester.error_code = error_code;
480 } else {
481 aws_array_list_push_back(&s_tester.streams, &stream);
482 }
483
484 aws_condition_variable_notify_one(&s_tester.signal);
485
486 AWS_FATAL_ASSERT(aws_mutex_unlock(&s_tester.lock) == AWS_OP_SUCCESS);
487 }
488
489 static bool s_is_stream_acquired_count_at_least(void *context) {
490 (void)context;
491 return s_tester.wait_for_stream_acquire_count <=
492 aws_array_list_length(&s_tester.streams) + s_tester.acquiring_stream_errors;
493 }
494
495 static int s_wait_on_streams_acquired_count(size_t count) {
496 ASSERT_SUCCESS(aws_mutex_lock(&s_tester.lock));
497
498 s_tester.wait_for_stream_acquire_count = count;
499 int signal_error =
500 aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_is_stream_acquired_count_at_least, NULL);
501
502 ASSERT_SUCCESS(aws_mutex_unlock(&s_tester.lock));
503 return signal_error;
504 }
505
506 static bool s_is_stream_completed_count_at_least(void *context) {
507 (void)context;
508 return s_tester.wait_for_stream_completed_count <= s_tester.stream_completed_count;
509 }
510
511 static int s_wait_on_streams_completed_count(size_t count) {
512 ASSERT_SUCCESS(aws_mutex_lock(&s_tester.lock));
513
514 s_tester.wait_for_stream_completed_count = count;
515 int signal_error =
516 aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_is_stream_completed_count_at_least, NULL);
517
518 ASSERT_SUCCESS(aws_mutex_unlock(&s_tester.lock));
519 return signal_error;
520 }
521
522 static void s_sm_tester_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
523 (void)user_data;
524 (void)stream;
525 AWS_FATAL_ASSERT(aws_mutex_lock(&s_tester.lock) == AWS_OP_SUCCESS);
526 if (error_code) {
527 ++s_tester.stream_complete_errors;
528 s_tester.stream_completed_error_code = error_code;
529 } else {
530 int status = 0;
531 if (aws_http_stream_get_incoming_response_status(stream, &status)) {
532 ++s_tester.stream_complete_errors;
533 s_tester.stream_completed_error_code = aws_last_error();
534 } else {
535 if (status / 100 == 2) {
536 ++s_tester.stream_200_count;
537 } else {
538 ++s_tester.stream_status_not_200_count;
539 }
540 }
541 }
542 ++s_tester.stream_completed_count;
543 aws_condition_variable_notify_one(&s_tester.signal);
544 AWS_FATAL_ASSERT(aws_mutex_unlock(&s_tester.lock) == AWS_OP_SUCCESS);
545 }
546
547 static void s_sm_tester_on_stream_destroy(void *user_data) {
548 (void)user_data;
549 aws_atomic_fetch_add(&s_tester.stream_destroyed_count, 1);
550 }
551
552 static int s_sm_stream_acquiring_customize_request(
553 int num_streams,
554 struct aws_http_make_request_options *request_options) {
555 struct aws_http2_stream_manager_acquire_stream_options acquire_stream_option = {
556 .options = request_options,
557 .callback = s_sm_tester_on_stream_acquired,
558 .user_data = &s_tester,
559 };
560 for (int i = 0; i < num_streams; ++i) {
561 /* TODO: Test the callback will always be fired asynced, as now the CM cannot ensure the callback happens
562 * asynchronously, we cannot ensure it as well. */
563 aws_http2_stream_manager_acquire_stream(s_tester.stream_manager, &acquire_stream_option);
564 }
565 return AWS_OP_SUCCESS;
566 }
567
568 static struct aws_byte_cursor s_default_empty_path = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/");
569
570 struct aws_byte_cursor s_normalize_path(struct aws_byte_cursor path) {
571 return path.len == 0 ? s_default_empty_path : path;
572 }
573
574 static int s_sm_stream_acquiring(int num_streams) {
575 struct aws_http_message *request = aws_http2_message_new_request(s_tester.allocator);
576 ASSERT_NOT_NULL(request);
577
578 struct aws_http_header request_headers_src[] = {
579 DEFINE_HEADER(":method", "GET"),
580 {
581 .name = aws_byte_cursor_from_c_str(":scheme"),
582 .value = *aws_uri_scheme(&s_tester.endpoint),
583 },
584 {
585 .name = aws_byte_cursor_from_c_str(":path"),
586 .value = s_normalize_path(*aws_uri_path(&s_tester.endpoint)),
587 },
588 {
589 .name = aws_byte_cursor_from_c_str(":authority"),
590 .value = *aws_uri_host_name(&s_tester.endpoint),
591 },
592 };
593 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
594 struct aws_http_make_request_options request_options = {
595 .self_size = sizeof(request_options),
596 .request = request,
597 .user_data = &s_tester,
598 .on_complete = s_sm_tester_on_stream_complete,
599 .on_destroy = s_sm_tester_on_stream_destroy,
600 };
601 int return_code = s_sm_stream_acquiring_customize_request(num_streams, &request_options);
602 aws_http_message_release(request);
603 return return_code;
604 }
605
606 /* Test the common setup/teardown used by all tests in this file */
607 TEST_CASE(h2_sm_sanity_check) {
608 (void)ctx;
609 struct sm_tester_options options = {
610 .max_connections = 5,
611 .alloc = allocator,
612 };
613 ASSERT_SUCCESS(s_tester_init(&options));
614 return s_tester_clean_up();
615 }
616
617 static bool s_is_fake_connection_count(void *context) {
618 (void)context;
619 return s_tester.wait_for_fake_connection_count <= aws_array_list_length(&s_tester.fake_connections);
620 }
621
622 static int s_wait_on_fake_connection_count(size_t count) {
623 ASSERT_SUCCESS(aws_mutex_lock(&s_tester.lock));
624
625 s_tester.wait_for_fake_connection_count = count;
626 int signal_error =
627 aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_is_fake_connection_count, NULL);
628
629 ASSERT_SUCCESS(aws_mutex_unlock(&s_tester.lock));
630 return signal_error;
631 }
632
633 static struct sm_fake_connection *s_sm_tester_fake_connection_new_from_options(
634 const struct aws_http_client_connection_options *options) {
635 struct sm_fake_connection *fake_connection = s_sm_fake_connection_new();
636 fake_connection->options = *options;
637 AWS_FATAL_ASSERT(aws_array_list_push_back(&s_tester.fake_connections, &fake_connection) == AWS_OP_SUCCESS);
638 if (s_tester.offer_bad_connection_count < s_tester.bad_connection_to_offer) {
639 /* Offer a bad connection */
640 s_tester.offer_bad_connection_count++;
641 return fake_connection;
642 }
643
644 struct aws_http_connection *connection = aws_http_connection_new_http2_client(
645 options->allocator, options->manual_window_management, 0 /*initial window size*/, options->http2_options);
646 AWS_FATAL_ASSERT(connection);
647 aws_http_connection_acquire(connection);
648
649 {
650 /* set connection user_data (handled by http-bootstrap in real world) */
651 connection->user_data = options->user_data;
652 /* re-enact marriage vows of http-connection and channel (handled by http-bootstrap in real world) */
653 struct aws_channel_slot *slot = aws_channel_slot_new(fake_connection->testing_channel.channel);
654 AWS_FATAL_ASSERT(slot);
655 AWS_FATAL_ASSERT(aws_channel_slot_insert_end(fake_connection->testing_channel.channel, slot) == AWS_OP_SUCCESS);
656 AWS_FATAL_ASSERT(aws_channel_slot_set_handler(slot, &connection->channel_handler) == AWS_OP_SUCCESS);
657 connection->vtable->on_channel_handler_installed(&connection->channel_handler, slot);
658 }
659 fake_connection->connection = connection;
660 return fake_connection;
661 }
662
663 static int s_sm_tester_finish_up_fake_connection_set_up(struct sm_fake_connection *fake_connection) {
664 if (!fake_connection->connection) {
665 fake_connection->options.on_setup(NULL, aws_last_error(), fake_connection->options.user_data);
666 return AWS_OP_SUCCESS;
667 }
668
669 /* Invoke callback outside lock */
670 fake_connection->options.on_setup(
671 fake_connection->connection, AWS_ERROR_SUCCESS, fake_connection->options.user_data);
672 testing_channel_drain_queued_tasks(&fake_connection->testing_channel);
673 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&fake_connection->peer));
674 struct aws_http2_setting settings_array[] = {
675 {
676 .id = AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS,
677 .value = s_tester.max_con_stream_remote,
678 },
679 };
680
681 struct aws_h2_frame *settings_frame =
682 aws_h2_frame_new_settings(s_tester.allocator, settings_array, AWS_ARRAY_SIZE(settings_array), false /*ack*/);
683 ASSERT_NOT_NULL(settings_frame);
684 ASSERT_SUCCESS(h2_fake_peer_send_frame(&fake_connection->peer, settings_frame));
685 struct aws_h2_frame *settings_ack = aws_h2_frame_new_settings(s_tester.allocator, NULL, 0, true /*ack*/);
686 ASSERT_NOT_NULL(settings_ack);
687 ASSERT_SUCCESS(h2_fake_peer_send_frame(&fake_connection->peer, settings_ack));
688 return AWS_OP_SUCCESS;
689 }
690
691 static int s_aws_http_connection_manager_create_connection_sync_mock(
692 const struct aws_http_client_connection_options *options) {
693 AWS_FATAL_ASSERT(aws_mutex_lock(&s_tester.lock) == AWS_OP_SUCCESS);
694 struct sm_fake_connection *fake_connection = s_sm_tester_fake_connection_new_from_options(options);
695 aws_condition_variable_notify_one(&s_tester.signal);
696 AWS_FATAL_ASSERT(aws_mutex_unlock(&s_tester.lock) == AWS_OP_SUCCESS);
697 ASSERT_SUCCESS(s_sm_tester_finish_up_fake_connection_set_up(fake_connection));
698 return AWS_OP_SUCCESS;
699 }
700
701 static int s_aws_http_connection_manager_create_connection_delay_mock(
702 const struct aws_http_client_connection_options *options) {
703
704 if (s_tester.delay_finished) {
705 return s_aws_http_connection_manager_create_connection_sync_mock(options);
706 }
707 AWS_FATAL_ASSERT(aws_mutex_lock(&s_tester.lock) == AWS_OP_SUCCESS);
708 ++s_tester.delay_offer_connection_count;
709 struct sm_fake_connection *fake_connection = s_sm_tester_fake_connection_new_from_options(options);
710 AWS_FATAL_ASSERT(aws_mutex_unlock(&s_tester.lock) == AWS_OP_SUCCESS);
711
712 /* don't do anything as it's delivered delay */
713 (void)fake_connection;
714 return AWS_OP_SUCCESS;
715 }
716
717 static int s_sm_tester_offer_waiting_connections(void) {
718
719 for (size_t i = 0; i < s_tester.delay_offer_connection_count; i++) {
720 struct sm_fake_connection *fake_connection = s_get_fake_connection(i);
721 ASSERT_SUCCESS(s_sm_tester_finish_up_fake_connection_set_up(fake_connection));
722 }
723 s_tester.delay_finished = true;
724 /* We are not haveing any threads. so, not invoking anything */
725 return AWS_OP_SUCCESS;
726 }
727
728 static struct aws_http_connection_manager_system_vtable s_mocks;
729
730 static void s_override_cm_connect_function(int (*fn)(const struct aws_http_client_connection_options *options)) {
731 s_mocks = *g_aws_http_connection_manager_default_system_vtable_ptr;
732 s_mocks.aws_http_client_connect = fn;
733 s_tester.connection_manager = s_tester.stream_manager->connection_manager;
734 aws_http_connection_manager_set_system_vtable(s_tester.connection_manager, &s_mocks);
735 }
736
737 TEST_CASE(h2_sm_mock_connection) {
738 (void)ctx;
739 struct sm_tester_options options = {
740 .max_connections = 5,
741 .alloc = allocator,
742 };
743 ASSERT_SUCCESS(s_tester_init(&options));
744 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
745 int num_to_acquire = 5;
746 ASSERT_SUCCESS(s_sm_stream_acquiring(num_to_acquire));
747 /* waiting for one fake connection made */
748 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
749 s_drain_all_fake_connection_testing_channel();
750 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(num_to_acquire));
751 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
752 size_t destroyed = aws_atomic_load_int(&s_tester.stream_destroyed_count);
753 ASSERT_INT_EQUALS(0, destroyed);
754 s_release_all_streams();
755 destroyed = aws_atomic_load_int(&s_tester.stream_destroyed_count);
756 ASSERT_INT_EQUALS(num_to_acquire, destroyed);
757
758 return s_tester_clean_up();
759 }
760
761 TEST_CASE(h2_sm_mock_multiple_connections) {
762 (void)ctx;
763 size_t max_concurrent_streams_per_connection = 3;
764 int num_streams_to_acquire = 9;
765 int num_expected_connection = num_streams_to_acquire / (int)max_concurrent_streams_per_connection;
766 if (num_streams_to_acquire % max_concurrent_streams_per_connection) {
767 ++num_expected_connection;
768 }
769 struct sm_tester_options options = {
770 .max_connections = 5,
771 .max_concurrent_streams_per_connection = max_concurrent_streams_per_connection,
772 .alloc = allocator,
773 };
774 ASSERT_SUCCESS(s_tester_init(&options));
775 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
776 ASSERT_SUCCESS(s_sm_stream_acquiring(num_streams_to_acquire));
777 /* waiting for one fake connection made */
778 ASSERT_SUCCESS(s_wait_on_fake_connection_count(num_expected_connection));
779 s_drain_all_fake_connection_testing_channel();
780 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(num_streams_to_acquire));
781 ASSERT_TRUE(aws_array_list_length(&s_tester.fake_connections) == (size_t)num_expected_connection);
782 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
783
784 return s_tester_clean_up();
785 }
786
787 /* Test stream manager got an bad connection and fail the expected number of stream requests. */
788 TEST_CASE(h2_sm_mock_bad_connection_acquired) {
789 (void)ctx;
790 struct sm_tester_options options = {
791 .max_connections = 5,
792 .ideal_concurrent_streams_per_connection = 2,
793 .max_concurrent_streams_per_connection = 5,
794 .alloc = allocator,
795 };
796 ASSERT_SUCCESS(s_tester_init(&options));
797 s_tester.bad_connection_to_offer = 2;
798 size_t good_connections_num = options.max_connections - s_tester.bad_connection_to_offer;
799 size_t streams_acquiring_num = 15;
800 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_delay_mock);
801 ASSERT_SUCCESS(s_sm_stream_acquiring((int)streams_acquiring_num));
802 /* The count should be max connection now */
803 ASSERT_UINT_EQUALS(s_tester.delay_offer_connection_count, options.max_connections);
804 /* Offer the connections waiting */
805 ASSERT_SUCCESS(s_sm_tester_offer_waiting_connections());
806 /* waiting for 3 fake connection made as the first two connection will fail */
807 ASSERT_SUCCESS(s_wait_on_fake_connection_count(good_connections_num));
808 s_drain_all_fake_connection_testing_channel();
809 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(streams_acquiring_num));
810 /* We fail the number of streams cannot fit into the health connections based on the ideal. */
811 ASSERT_INT_EQUALS(
812 streams_acquiring_num - options.ideal_concurrent_streams_per_connection * good_connections_num,
813 s_tester.acquiring_stream_errors);
814 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_STREAM_MANAGER_CONNECTION_ACQUIRE_FAILURE, s_tester.error_code);
815 ASSERT_TRUE(aws_array_list_length(&s_tester.streams) == 6);
816
817 /* Acquire more streams, which should succeed as we don't close the connection */
818 ASSERT_SUCCESS(s_sm_stream_acquiring(4));
819 /* waiting for the new connection */
820 ASSERT_SUCCESS(s_wait_on_fake_connection_count(options.max_connections + 2));
821 s_drain_all_fake_connection_testing_channel();
822 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(streams_acquiring_num + 4));
823 /* all the new streams succeed */
824 ASSERT_TRUE(aws_array_list_length(&s_tester.streams) == 10);
825 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
826
827 return s_tester_clean_up();
828 }
829
830 /* Test a connection offered, and before the stream was made, the connection dies. The stream should fail */
831 TEST_CASE(h2_sm_mock_connections_closed_before_request_made) {
832 (void)ctx;
833 struct sm_tester_options options = {
834 .max_connections = 1,
835 .max_concurrent_streams_per_connection = 3,
836 .alloc = allocator,
837 };
838 ASSERT_SUCCESS(s_tester_init(&options));
839 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
840 ASSERT_SUCCESS(s_sm_stream_acquiring(2));
841 /* waiting for one fake connection made */
842 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
843 s_drain_all_fake_connection_testing_channel();
844 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(2));
845 /* No error happens */
846 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
847 /* Now, we close the connection, the stream manager will fail the new stream, if the opening streams not completed.
848 */
849 struct sm_fake_connection *fake_connection = s_get_fake_connection(0);
850 aws_http_connection_close(fake_connection->connection);
851 ASSERT_SUCCESS(s_sm_stream_acquiring(1));
852 s_drain_all_fake_connection_testing_channel();
853 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(3));
854 /* ASSERT new one failed. */
855 ASSERT_INT_EQUALS(1, s_tester.acquiring_stream_errors);
856 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, s_tester.error_code);
857 /* Reset errors */
858 s_tester.acquiring_stream_errors = 0;
859 s_tester.error_code = 0;
860 s_drain_all_fake_connection_testing_channel();
861
862 /* As long as the connection finishes shutting down, we can still make more requests from new connection. */
863 ASSERT_SUCCESS(s_sm_stream_acquiring(2));
864 /* waiting for one fake connection made */
865 ASSERT_SUCCESS(s_wait_on_fake_connection_count(2));
866 s_drain_all_fake_connection_testing_channel();
867 /* No error happens */
868 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
869 /* We made 4 streams successfully */
870 ASSERT_INT_EQUALS(4, aws_array_list_length(&s_tester.streams));
871
872 /* Finish all the opening streams */
873 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
874
875 return s_tester_clean_up();
876 }
877
878 /* Test that the remote max concurrent streams setting hit */
879 TEST_CASE(h2_sm_mock_max_concurrent_streams_remote) {
880 (void)ctx;
881 struct sm_tester_options options = {
882 .max_connections = 5,
883 .alloc = allocator,
884 };
885 ASSERT_SUCCESS(s_tester_init(&options));
886 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
887 /* Set the remote max to be 2 */
888 s_tester.max_con_stream_remote = 2;
889 /* Acquire a stream to trigger */
890 ASSERT_SUCCESS(s_sm_stream_acquiring(1));
891 /* waiting for one fake connection made */
892 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
893 s_drain_all_fake_connection_testing_channel();
894 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(1));
895 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
896 ASSERT_INT_EQUALS(0, s_tester.stream_complete_errors);
897
898 /* Fake peer send settings that only allow 2 concurrent streams */
899 /* Acquire tow more streams */
900 ASSERT_SUCCESS(s_sm_stream_acquiring(2));
901 /* We created a new connection */
902 ASSERT_SUCCESS(s_wait_on_fake_connection_count(2));
903 s_drain_all_fake_connection_testing_channel();
904 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(1 + 2));
905 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
906
907 ASSERT_INT_EQUALS(2, aws_array_list_length(&s_tester.fake_connections));
908 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
909
910 return s_tester_clean_up();
911 }
912
913 /* Test that the remote max concurrent streams setting hit */
914 TEST_CASE(h2_sm_mock_fetch_metric) {
915 (void)ctx;
916 struct sm_tester_options options = {
917 .max_connections = 5,
918 .alloc = allocator,
919 };
920 ASSERT_SUCCESS(s_tester_init(&options));
921 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
922 /* Set the remote max to be 2 */
923 s_tester.max_con_stream_remote = 2;
924 /* Acquire a stream to trigger */
925 ASSERT_SUCCESS(s_sm_stream_acquiring(1));
926 /* waiting for one fake connection made */
927 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
928 s_drain_all_fake_connection_testing_channel();
929 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(1));
930 struct aws_http_manager_metrics out_metrics;
931 AWS_ZERO_STRUCT(out_metrics);
932
933 aws_http2_stream_manager_fetch_metrics(s_tester.stream_manager, &out_metrics);
934 /* Acquired 1 stream, and we hold one connection, the max streams per connection is 2. */
935 ASSERT_UINT_EQUALS(out_metrics.available_concurrency, 1);
936 ASSERT_UINT_EQUALS(out_metrics.pending_concurrency_acquires, 0);
937 ASSERT_UINT_EQUALS(out_metrics.leased_concurrency, 1);
938
939 ASSERT_SUCCESS(s_sm_stream_acquiring(1));
940
941 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
942 s_drain_all_fake_connection_testing_channel();
943 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(2));
944 aws_http2_stream_manager_fetch_metrics(s_tester.stream_manager, &out_metrics);
945 ASSERT_UINT_EQUALS(out_metrics.available_concurrency, 0);
946 ASSERT_UINT_EQUALS(out_metrics.pending_concurrency_acquires, 0);
947 ASSERT_UINT_EQUALS(out_metrics.leased_concurrency, 2);
948
949 ASSERT_SUCCESS(s_sm_stream_acquiring(10));
950 ASSERT_SUCCESS(s_wait_on_fake_connection_count(5));
951 s_drain_all_fake_connection_testing_channel();
952 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(10));
953 aws_http2_stream_manager_fetch_metrics(s_tester.stream_manager, &out_metrics);
954 ASSERT_UINT_EQUALS(out_metrics.available_concurrency, 0);
955 ASSERT_UINT_EQUALS(out_metrics.pending_concurrency_acquires, 2);
956 ASSERT_UINT_EQUALS(out_metrics.leased_concurrency, 10);
957
958 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
959 /* Still have two more streams that have not been completed */
960 s_drain_all_fake_connection_testing_channel();
961 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
962
963 return s_tester_clean_up();
964 }
965
966 /* Test that the stream completed will free the connection for more streams */
967 TEST_CASE(h2_sm_mock_complete_stream) {
968 (void)ctx;
969 struct sm_tester_options options = {
970 .max_connections = 5,
971 .ideal_concurrent_streams_per_connection = 2,
972 .max_concurrent_streams_per_connection = 2,
973 .alloc = allocator,
974 };
975 ASSERT_SUCCESS(s_tester_init(&options));
976 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
977 ASSERT_SUCCESS(s_sm_stream_acquiring(2));
978 /* waiting for one fake connection made */
979 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
980 s_drain_all_fake_connection_testing_channel();
981 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(2));
982 ASSERT_INT_EQUALS(1, aws_array_list_length(&s_tester.fake_connections));
983
984 /* Fake peer send settings that only allow 2 concurrent streams */
985 struct sm_fake_connection *fake_connection = s_get_fake_connection(0);
986 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&fake_connection->peer));
987 s_fake_connection_complete_streams(fake_connection, 1, false);
988
989 /* Acquire a new streams */
990 ASSERT_SUCCESS(s_sm_stream_acquiring(1));
991 s_drain_all_fake_connection_testing_channel();
992 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(2 + 1));
993 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
994 /* No error happens */
995 ASSERT_INT_EQUALS(0, s_tester.stream_complete_errors);
996 /* We have no extra connection made. */
997 ASSERT_INT_EQUALS(1, aws_array_list_length(&s_tester.fake_connections));
998
999 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1000
1001 return s_tester_clean_up();
1002 }
1003
1004 /* Test the soft limit from user works as we want */
1005 TEST_CASE(h2_sm_mock_ideal_num_streams) {
1006 (void)ctx;
1007 struct sm_tester_options options = {
1008 .max_connections = 5,
1009 .ideal_concurrent_streams_per_connection = 3,
1010 .max_concurrent_streams_per_connection = 5,
1011 .alloc = allocator,
1012 };
1013 ASSERT_SUCCESS(s_tester_init(&options));
1014 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1015 ASSERT_SUCCESS(s_sm_stream_acquiring(15));
1016 /* We will create 5 connections instead of 3 */
1017 ASSERT_SUCCESS(s_wait_on_fake_connection_count(5));
1018 s_drain_all_fake_connection_testing_channel();
1019 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(15));
1020 ASSERT_INT_EQUALS(5, aws_array_list_length(&s_tester.fake_connections));
1021
1022 s_drain_all_fake_connection_testing_channel();
1023 /* Check all the 5 fake connections received 3 streams each */
1024 for (size_t i = 0; i < aws_array_list_length(&s_tester.fake_connections); ++i) {
1025 struct sm_fake_connection *fake_connection = s_get_fake_connection(i);
1026 ASSERT_INT_EQUALS(
1027 s_fake_connection_get_stream_received(fake_connection), options.ideal_concurrent_streams_per_connection);
1028 }
1029
1030 /* Acquire 15 more, we can only have 25 (5*5) in total */
1031 ASSERT_SUCCESS(s_sm_stream_acquiring(15));
1032 s_drain_all_fake_connection_testing_channel();
1033 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(10));
1034
1035 s_drain_all_fake_connection_testing_channel();
1036 /* Check all the 5 fake connections received 5 streams each */
1037 for (size_t i = 0; i < aws_array_list_length(&s_tester.fake_connections); ++i) {
1038 struct sm_fake_connection *fake_connection = s_get_fake_connection(i);
1039 ASSERT_INT_EQUALS(
1040 s_fake_connection_get_stream_received(fake_connection), options.max_concurrent_streams_per_connection);
1041 }
1042
1043 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1044 s_drain_all_fake_connection_testing_channel();
1045 /* completed the remain streams */
1046 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1047 return s_tester_clean_up();
1048 }
1049
1050 TEST_CASE(h2_sm_mock_large_ideal_num_streams) {
1051 (void)ctx;
1052 struct sm_tester_options options = {
1053 .max_connections = 5,
1054 .ideal_concurrent_streams_per_connection = 3,
1055 .max_concurrent_streams_per_connection = 5,
1056 .alloc = allocator,
1057 };
1058 ASSERT_SUCCESS(s_tester_init(&options));
1059 /* Set the remote max to be 2 */
1060 s_tester.max_con_stream_remote = 2;
1061 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1062 ASSERT_SUCCESS(s_sm_stream_acquiring(6));
1063 /* We will create 3 connections instead of 2 */
1064 ASSERT_SUCCESS(s_wait_on_fake_connection_count(3));
1065 s_drain_all_fake_connection_testing_channel();
1066 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(6));
1067 ASSERT_INT_EQUALS(3, aws_array_list_length(&s_tester.fake_connections));
1068
1069 s_drain_all_fake_connection_testing_channel();
1070
1071 for (size_t i = 0; i < aws_array_list_length(&s_tester.fake_connections); ++i) {
1072 struct sm_fake_connection *fake_connection = s_get_fake_connection(i);
1073 ASSERT_INT_EQUALS(s_fake_connection_get_stream_received(fake_connection), s_tester.max_con_stream_remote);
1074 }
1075
1076 /* Acquire 15 more, we can only have 10 (2*5) in total. 21 acquisitions made */
1077 ASSERT_SUCCESS(s_sm_stream_acquiring(15));
1078 s_drain_all_fake_connection_testing_channel();
1079 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(10 - 6));
1080
1081 s_drain_all_fake_connection_testing_channel();
1082 for (size_t i = 0; i < aws_array_list_length(&s_tester.fake_connections); ++i) {
1083 struct sm_fake_connection *fake_connection = s_get_fake_connection(i);
1084 ASSERT_INT_EQUALS(s_fake_connection_get_stream_received(fake_connection), s_tester.max_con_stream_remote);
1085 }
1086 ASSERT_UINT_EQUALS(10, aws_array_list_length(&s_tester.streams));
1087 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1088 s_drain_all_fake_connection_testing_channel();
1089 /* Completed 10 streams, 10 more streams created */
1090 ASSERT_UINT_EQUALS(20, aws_array_list_length(&s_tester.streams));
1091 /* Completed remain 10 streams */
1092 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1093 s_drain_all_fake_connection_testing_channel();
1094 /* Should have 1 more streams made now, which will have all 21 made */
1095 ASSERT_UINT_EQUALS(21, aws_array_list_length(&s_tester.streams));
1096 /* Completed all of them again, and we are good */
1097 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1098 s_drain_all_fake_connection_testing_channel();
1099
1100 return s_tester_clean_up();
1101 }
1102
1103 /* Test that goaway received from peer, new connection will be made */
1104 TEST_CASE(h2_sm_mock_goaway) {
1105 (void)ctx;
1106 struct sm_tester_options options = {
1107 .max_connections = 5,
1108 .alloc = allocator,
1109 };
1110 ASSERT_SUCCESS(s_tester_init(&options));
1111 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1112 ASSERT_SUCCESS(s_sm_stream_acquiring(5));
1113 /* waiting for one fake connection made */
1114 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
1115 s_drain_all_fake_connection_testing_channel();
1116 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(5));
1117 ASSERT_INT_EQUALS(1, aws_array_list_length(&s_tester.fake_connections));
1118 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
1119
1120 /* Fake peer send goaway */
1121 struct sm_fake_connection *fake_connection = s_get_fake_connection(0);
1122 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&fake_connection->peer));
1123 struct aws_byte_cursor debug_info;
1124 AWS_ZERO_STRUCT(debug_info);
1125 struct aws_http_stream *stream = NULL;
1126 aws_array_list_front(&s_tester.streams, &stream);
1127 struct aws_h2_frame *peer_frame =
1128 aws_h2_frame_new_goaway(allocator, aws_http_stream_get_id(stream), AWS_HTTP2_ERR_NO_ERROR, debug_info);
1129 ASSERT_SUCCESS(h2_fake_peer_send_frame(&fake_connection->peer, peer_frame));
1130 testing_channel_drain_queued_tasks(&fake_connection->testing_channel);
1131
1132 /* Should be the streams with id larger than the first stream all completed with error */
1133 ASSERT_INT_EQUALS(4, s_tester.stream_complete_errors);
1134 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_GOAWAY_RECEIVED, s_tester.stream_completed_error_code);
1135
1136 /* When we create new streams, stream manager should create a new connection to use */
1137 ASSERT_SUCCESS(s_sm_stream_acquiring(5));
1138 /* waiting for one fake connection made */
1139 ASSERT_SUCCESS(s_wait_on_fake_connection_count(2));
1140 s_drain_all_fake_connection_testing_channel();
1141 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(5 + 5));
1142 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
1143 /* No more stream completed with error */
1144 ASSERT_INT_EQUALS(4, s_tester.stream_complete_errors);
1145 /* Two connection made */
1146 ASSERT_INT_EQUALS(2, aws_array_list_length(&s_tester.fake_connections));
1147 fake_connection = s_get_fake_connection(1);
1148 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&fake_connection->peer));
1149
1150 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1151
1152 return s_tester_clean_up();
1153 }
1154
1155 /* Test that cancelling a stream after GOAWAY does not crash from double stream completion.
1156 * Reproduces P428540711: cancel() schedules cross-thread task, but s_finish_shutdown already
1157 * completed the stream. The cross-thread task then invokes on_complete a second time. */
1158 TEST_CASE(h2_sm_mock_cancel_after_goaway_no_double_complete) {
1159 (void)ctx;
1160 struct sm_tester_options options = {
1161 .max_connections = 1,
1162 .max_concurrent_streams_per_connection = 1,
1163 .alloc = allocator,
1164 };
1165 ASSERT_SUCCESS(s_tester_init(&options));
1166 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1167
1168 /* Acquire 1 stream */
1169 ASSERT_SUCCESS(s_sm_stream_acquiring(1));
1170 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
1171 s_drain_all_fake_connection_testing_channel();
1172 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(1));
1173 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
1174
1175 struct sm_fake_connection *fake_connection = s_get_fake_connection(0);
1176 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&fake_connection->peer));
1177 testing_channel_drain_queued_tasks(&fake_connection->testing_channel);
1178
1179 /* Get the stream handle */
1180 struct aws_http_stream *stream = NULL;
1181 aws_array_list_front(&s_tester.streams, &stream);
1182 ASSERT_NOT_NULL(stream);
1183
1184 /* Send GOAWAY with last_stream_id=0, meaning our stream (id=1) is rejected */
1185 struct aws_byte_cursor debug_info;
1186 AWS_ZERO_STRUCT(debug_info);
1187 struct aws_h2_frame *goaway_frame =
1188 aws_h2_frame_new_goaway(allocator, 0 /*last_stream_id*/, AWS_HTTP2_ERR_INTERNAL_ERROR, debug_info);
1189 ASSERT_SUCCESS(h2_fake_peer_send_frame(&fake_connection->peer, goaway_frame));
1190
1191 /* Cancel the stream BEFORE draining tasks.
1192 * This schedules the cross-thread work task (reset_called=true).
1193 * When we drain, both the GOAWAY processing (which completes the stream)
1194 * and the cancel cross-thread task will run, causing double on_complete. */
1195 aws_http_stream_cancel_default_error(stream);
1196
1197 /* User releases their ref on the stream (simulates real usage: cancel then release).
1198 * The stream must stay alive until the cross-thread task completes. */
1199 aws_http_stream_release(stream);
1200 aws_array_list_clear(&s_tester.streams);
1201
1202 /* Drain tasks - this should NOT crash from double stream completion or use-after-free */
1203 testing_channel_drain_queued_tasks(&fake_connection->testing_channel);
1204
1205 /* Stream should be completed with error at least once */
1206 ASSERT_TRUE(s_tester.stream_completed_count >= 1);
1207 ASSERT_TRUE(s_tester.stream_complete_errors >= 1);
1208
1209 return s_tester_clean_up();
1210 }
1211
1212 /* Test that PING works as expected. */
1213 TEST_CASE(h2_sm_connection_ping) {
1214 (void)ctx;
1215 size_t connection_ping_timeout_ms = AWS_TIMESTAMP_MILLIS; /* 1 sec */
1216 struct sm_tester_options options = {
1217 .max_connections = 3,
1218 .alloc = allocator,
1219 .max_concurrent_streams_per_connection = 2,
1220 .connection_ping_period_ms = 2 * AWS_TIMESTAMP_MILLIS,
1221 .connection_ping_timeout_ms = connection_ping_timeout_ms,
1222 };
1223 ASSERT_SUCCESS(s_tester_init(&options));
1224 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1225 ASSERT_SUCCESS(s_sm_stream_acquiring(6));
1226 /* waiting for one fake connection made */
1227 ASSERT_SUCCESS(s_wait_on_fake_connection_count(3));
1228 s_drain_all_fake_connection_testing_channel();
1229 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(6));
1230 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
1231
1232 aws_thread_current_sleep(2 * AWS_TIMESTAMP_NANOS); /* Sleep 2 sec */
1233
1234 /* Check PING received for all the connections */
1235 struct sm_fake_connection *fake_connection_1 = s_get_fake_connection(0);
1236 struct sm_fake_connection *fake_connection_2 = s_get_fake_connection(1);
1237 struct sm_fake_connection *fake_connection_3 = s_get_fake_connection(2);
1238 testing_channel_drain_queued_tasks(&fake_connection_1->testing_channel);
1239 testing_channel_drain_queued_tasks(&fake_connection_2->testing_channel);
1240 testing_channel_drain_queued_tasks(&fake_connection_3->testing_channel);
1241 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&fake_connection_1->peer));
1242 struct h2_decoded_frame *ping_frame =
1243 h2_decode_tester_find_frame(&fake_connection_1->peer.decode, AWS_H2_FRAME_T_PING, 0, NULL);
1244 ASSERT_NOT_NULL(ping_frame);
1245
1246 /* Fake peer only send PINGACK to the first connection immediately */
1247 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&fake_connection_1->peer));
1248 struct aws_h2_frame *peer_frame = aws_h2_frame_new_ping(allocator, true /*ACK*/, ping_frame->ping_opaque_data);
1249 ASSERT_SUCCESS(h2_fake_peer_send_frame(&fake_connection_1->peer, peer_frame));
1250 testing_channel_drain_queued_tasks(&fake_connection_1->testing_channel);
1251 s_fake_connection_complete_streams(
1252 fake_connection_1, 0 /*all streams*/, false); /* Make sure the streams completed successfully */
1253
1254 /* Check fake connection 2 received PING */
1255 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&fake_connection_2->peer));
1256 ping_frame = h2_decode_tester_find_frame(&fake_connection_2->peer.decode, AWS_H2_FRAME_T_PING, 0, NULL);
1257 ASSERT_NOT_NULL(ping_frame);
1258 /* Check fake connection 3 received PING, but never send ping for connection 3 */
1259 ASSERT_SUCCESS(h2_fake_peer_decode_messages_from_testing_channel(&fake_connection_3->peer));
1260 ping_frame = h2_decode_tester_find_frame(&fake_connection_3->peer.decode, AWS_H2_FRAME_T_PING, 0, NULL);
1261 ASSERT_NOT_NULL(ping_frame);
1262
1263 aws_thread_current_sleep(AWS_TIMESTAMP_NANOS); /* Sleep 1 sec */
1264 testing_channel_drain_queued_tasks(&fake_connection_2->testing_channel);
1265 testing_channel_drain_queued_tasks(&fake_connection_3->testing_channel);
1266
1267 /* Send PINGACK for connection 2 after timeout has happened */
1268 ASSERT_SUCCESS(h2_fake_peer_send_connection_preface_default_settings(&fake_connection_2->peer));
1269 peer_frame = aws_h2_frame_new_ping(allocator, true /*ACK*/, ping_frame->ping_opaque_data);
1270 ASSERT_SUCCESS(h2_fake_peer_send_frame(&fake_connection_2->peer, peer_frame));
1271 testing_channel_drain_queued_tasks(&fake_connection_2->testing_channel);
1272
1273 /* The streams on second and third connection should failed to complete */
1274 ASSERT_INT_EQUALS(4, s_tester.stream_complete_errors);
1275 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, s_tester.stream_completed_error_code);
1276
1277 return s_tester_clean_up();
1278 }
1279
1280 /* Test that stream manager with flow control error if the server didn't respect the window */
1281 TEST_CASE(h2_sm_with_flow_control_err) {
1282 (void)ctx;
1283
1284 struct sm_tester_options options = {
1285 .max_connections = 5,
1286 .alloc = allocator,
1287 .manual_window = true,
1288 .initial_window_size = 0,
1289 };
1290
1291 ASSERT_SUCCESS(s_tester_init(&options));
1292 ASSERT_NOT_NULL(s_tester.stream_manager);
1293
1294 /* Verify stream manager works with initial settings */
1295 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1296 int num_to_acquire = 1;
1297 ASSERT_SUCCESS(s_sm_stream_acquiring(num_to_acquire));
1298 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
1299 s_drain_all_fake_connection_testing_channel();
1300 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(num_to_acquire));
1301 ASSERT_SUCCESS(s_complete_all_fake_connection_streams_with_data());
1302 ASSERT_INT_EQUALS(1, s_tester.stream_complete_errors);
1303 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, s_tester.stream_completed_error_code);
1304
1305 return s_tester_clean_up();
1306 }
1307
1308 /* Test that stream manager can be created with initial_settings_array configured */
1309 TEST_CASE(h2_sm_with_initial_settings) {
1310 (void)ctx;
1311 /* TODO: VALIDATE from the peer that those settings received. For now we test this with settings to override the
1312 * initial window. */
1313 /* Configure custom HTTP/2 initial settings */
1314 struct aws_http2_setting initial_settings[] = {
1315 {
1316 .id = AWS_HTTP2_SETTINGS_HEADER_TABLE_SIZE,
1317 .value = 8192,
1318 },
1319 {
1320 .id = AWS_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS,
1321 .value = 128,
1322 },
1323 {
1324 .id = AWS_HTTP2_SETTINGS_INITIAL_WINDOW_SIZE,
1325 .value = 65536,
1326 },
1327 };
1328
1329 struct sm_tester_options options = {
1330 .max_connections = 5,
1331 .alloc = allocator,
1332 .initial_settings_array = initial_settings,
1333 .num_initial_settings = AWS_ARRAY_SIZE(initial_settings),
1334 .manual_window = true,
1335 /* Set initial window size to 0, but the settings should override it to keep the data flow. */
1336 .initial_window_size = 0,
1337 };
1338
1339 ASSERT_SUCCESS(s_tester_init(&options));
1340 ASSERT_NOT_NULL(s_tester.stream_manager);
1341
1342 /* Verify stream manager works with initial settings */
1343 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1344 int num_to_acquire = 1;
1345 ASSERT_SUCCESS(s_sm_stream_acquiring(num_to_acquire));
1346 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
1347 s_drain_all_fake_connection_testing_channel();
1348 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(num_to_acquire));
1349 ASSERT_SUCCESS(s_complete_all_fake_connection_streams_with_data());
1350 /* Should complete without error */
1351 ASSERT_INT_EQUALS(0, s_tester.stream_complete_errors);
1352 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, s_tester.stream_completed_error_code);
1353
1354 return s_tester_clean_up();
1355 }
1356
1357 /* Test that max_concurrent_streams limits the total number of active streams */
1358 TEST_CASE(h2_sm_mock_max_concurrent_streams) {
1359 (void)ctx;
1360 size_t max_concurrent_streams = 5;
1361 struct sm_tester_options options = {
1362 .max_connections = 3,
1363 .max_concurrent_streams_per_connection = 10,
1364 .alloc = allocator,
1365 };
1366 ASSERT_SUCCESS(s_tester_init(&options));
1367
1368 /* Set max_concurrent_streams on the stream manager */
1369 s_tester.stream_manager->max_concurrent_streams = max_concurrent_streams;
1370
1371 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1372
1373 /* Try to acquire 10 streams, but only 5 should be created initially */
1374 int num_to_acquire = 10;
1375 ASSERT_SUCCESS(s_sm_stream_acquiring(num_to_acquire));
1376
1377 /* Wait for connection to be made */
1378 ASSERT_SUCCESS(s_wait_on_fake_connection_count(1));
1379 s_drain_all_fake_connection_testing_channel();
1380
1381 /* Should have acquired only max_concurrent_streams streams */
1382 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(max_concurrent_streams));
1383 ASSERT_UINT_EQUALS(max_concurrent_streams, aws_array_list_length(&s_tester.streams));
1384
1385 /* Complete 3 streams */
1386 struct sm_fake_connection *fake_connection = s_get_fake_connection(0);
1387 s_fake_connection_complete_streams(fake_connection, 3, false);
1388 s_drain_all_fake_connection_testing_channel();
1389
1390 /* Now 3 more streams should be created (total of 8 acquired) */
1391 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(8));
1392 ASSERT_UINT_EQUALS(8, aws_array_list_length(&s_tester.streams));
1393
1394 /* Complete 2 more streams */
1395 s_fake_connection_complete_streams(fake_connection, 2, false);
1396 s_drain_all_fake_connection_testing_channel();
1397
1398 /* The remaining 2 streams should now be created (total of 10) */
1399 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(10));
1400 ASSERT_UINT_EQUALS(10, aws_array_list_length(&s_tester.streams));
1401
1402 /* Complete all remaining streams */
1403 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1404 s_drain_all_fake_connection_testing_channel();
1405 /* Should complete without error */
1406 ASSERT_INT_EQUALS(0, s_tester.stream_complete_errors);
1407 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, s_tester.stream_completed_error_code);
1408
1409 return s_tester_clean_up();
1410 }
1411
1412 /* Test that max_concurrent_streams works with multiple connections */
1413 TEST_CASE(h2_sm_mock_max_concurrent_streams_multiple_connections) {
1414 (void)ctx;
1415 size_t max_concurrent_streams = 8;
1416 struct sm_tester_options options = {
1417 .max_connections = 4,
1418 .max_concurrent_streams_per_connection = 3,
1419 .alloc = allocator,
1420 };
1421 ASSERT_SUCCESS(s_tester_init(&options));
1422
1423 /* Set max_concurrent_streams */
1424 s_tester.stream_manager->max_concurrent_streams = max_concurrent_streams;
1425
1426 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1427
1428 /* Try to acquire 15 streams */
1429 int num_to_acquire = 15;
1430 ASSERT_SUCCESS(s_sm_stream_acquiring(num_to_acquire));
1431
1432 /* all 4 connections should be created for the pending streams, but only 8 streams will be created in total */
1433 ASSERT_SUCCESS(s_wait_on_fake_connection_count(3));
1434 s_drain_all_fake_connection_testing_channel();
1435
1436 /* Should have acquired only max_concurrent_streams streams */
1437 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(max_concurrent_streams));
1438 ASSERT_UINT_EQUALS(max_concurrent_streams, aws_array_list_length(&s_tester.streams));
1439
1440 /* Complete 4 streams from first 2 connections, since they must have at least 2 streams. */
1441 for (size_t i = 0; i < 2; ++i) {
1442 struct sm_fake_connection *fake_connection = s_get_fake_connection(i);
1443 s_fake_connection_complete_streams(fake_connection, 2, false);
1444 }
1445 s_drain_all_fake_connection_testing_channel();
1446
1447 /* 4 more streams should be created (total of 12) */
1448 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(12));
1449 ASSERT_UINT_EQUALS(12, aws_array_list_length(&s_tester.streams));
1450
1451 /* Complete remaining streams */
1452 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1453 s_drain_all_fake_connection_testing_channel();
1454
1455 /* All 15 streams should eventually be acquired */
1456 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(15));
1457 ASSERT_UINT_EQUALS(15, aws_array_list_length(&s_tester.streams));
1458
1459 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1460 s_drain_all_fake_connection_testing_channel();
1461 /* Should complete without error */
1462 ASSERT_INT_EQUALS(0, s_tester.stream_complete_errors);
1463 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, s_tester.stream_completed_error_code);
1464
1465 return s_tester_clean_up();
1466 }
1467
1468 /* Test that max_concurrent_streams = 0 means no limit (default behavior) */
1469 TEST_CASE(h2_sm_mock_max_concurrent_streams_zero_means_no_limit) {
1470 (void)ctx;
1471 struct sm_tester_options options = {
1472 .max_connections = 2,
1473 .max_concurrent_streams_per_connection = 5,
1474 .alloc = allocator,
1475 };
1476 ASSERT_SUCCESS(s_tester_init(&options));
1477
1478 /* max_concurrent_streams defaults to 0 (no limit) */
1479 ASSERT_UINT_EQUALS(0, s_tester.stream_manager->max_concurrent_streams);
1480
1481 s_override_cm_connect_function(s_aws_http_connection_manager_create_connection_sync_mock);
1482
1483 /* Acquire 10 streams - all should be created immediately */
1484 int num_to_acquire = 10;
1485 ASSERT_SUCCESS(s_sm_stream_acquiring(num_to_acquire));
1486
1487 ASSERT_SUCCESS(s_wait_on_fake_connection_count(2)); /* 2 connections needed for 10 streams */
1488 s_drain_all_fake_connection_testing_channel();
1489
1490 /* All 10 streams should be acquired without waiting */
1491 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(num_to_acquire));
1492 ASSERT_UINT_EQUALS(num_to_acquire, aws_array_list_length(&s_tester.streams));
1493
1494 ASSERT_SUCCESS(s_complete_all_fake_connection_streams());
1495
1496 return s_tester_clean_up();
1497 }
1498
1499 /*******************************************************************************
1500 * Net test, that makes real HTTP/2 connection and requests
1501 ******************************************************************************/
1502
1503 /* Test that makes real streams */
1504 TEST_CASE(h2_sm_acquire_stream) {
1505 (void)ctx;
1506 struct sm_tester_options options = {
1507 .max_connections = 5,
1508 .alloc = allocator,
1509 };
1510 ASSERT_SUCCESS(s_tester_init(&options));
1511 int num_to_acquire = 5;
1512 ASSERT_SUCCESS(s_sm_stream_acquiring(num_to_acquire));
1513 ASSERT_SUCCESS(s_wait_on_streams_completed_count(num_to_acquire));
1514 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
1515 ASSERT_INT_EQUALS(num_to_acquire, s_tester.stream_200_count);
1516
1517 return s_tester_clean_up();
1518 }
1519
1520 /* Test that makes real streams and trigger multiple connections to be created */
1521 TEST_CASE(h2_sm_acquire_stream_multiple_connections) {
1522 (void)ctx;
1523 struct sm_tester_options options = {
1524 .max_connections = 5,
1525 .alloc = allocator,
1526 .max_concurrent_streams_per_connection = 5,
1527 };
1528 ASSERT_SUCCESS(s_tester_init(&options));
1529
1530 int num_to_acquire = 20;
1531 ASSERT_SUCCESS(s_sm_stream_acquiring(num_to_acquire));
1532 ASSERT_SUCCESS(s_wait_on_streams_completed_count(num_to_acquire));
1533 ASSERT_INT_EQUALS(0, s_tester.acquiring_stream_errors);
1534 ASSERT_INT_EQUALS(num_to_acquire, s_tester.stream_200_count);
1535
1536 return s_tester_clean_up();
1537 }
1538
1539 static void s_sm_tester_on_connection_setup(struct aws_http_connection *connection, int error_code, void *user_data) {
1540 if (s_tester.release_sm_during_connection_acquiring) {
1541 aws_http2_stream_manager_release(s_tester.stream_manager);
1542 s_tester.stream_manager = NULL;
1543 }
1544 s_tester.on_setup(connection, error_code, user_data);
1545 }
1546
1547 static int s_aws_http_connection_manager_create_real_connection_sync(
1548 const struct aws_http_client_connection_options *options) {
1549 struct aws_http_client_connection_options local_options = *options;
1550 s_tester.on_setup = options->on_setup;
1551 local_options.on_setup = s_sm_tester_on_connection_setup;
1552 return aws_http_client_connect(&local_options);
1553 }
1554
1555 /* Test that the stream manager closing before connection acquired, all the pending stream acquiring should fail */
1556 TEST_CASE(h2_sm_closing_before_connection_acquired) {
1557 (void)ctx;
1558 struct sm_tester_options options = {
1559 .max_connections = 5,
1560 .max_concurrent_streams_per_connection = 2,
1561 .alloc = allocator,
1562 };
1563 ASSERT_SUCCESS(s_tester_init(&options));
1564 s_tester.release_sm_during_connection_acquiring = true;
1565 s_override_cm_connect_function(s_aws_http_connection_manager_create_real_connection_sync);
1566 /* only acquire one as the connection create happens synced, the stream manager refcount will be released as the
1567 * first stream acquiring */
1568 ASSERT_SUCCESS(s_sm_stream_acquiring(1));
1569 ASSERT_SUCCESS(s_wait_on_streams_acquired_count(1));
1570
1571 /* all acquiring stream failed */
1572 ASSERT_INT_EQUALS(1, s_tester.acquiring_stream_errors);
1573 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_STREAM_MANAGER_SHUTTING_DOWN, s_tester.error_code);
1574 return s_tester_clean_up();
1575 }
1576
1577 TEST_CASE(localhost_integ_h2_sm_close_connection_on_server_error) {
1578 (void)ctx;
1579 /* server that will return 500 status code all the time. */
1580 struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str("https://localhost:3443/echo");
1581 struct sm_tester_options options = {
1582 .max_connections = 1,
1583 .max_concurrent_streams_per_connection = 10,
1584 .alloc = allocator,
1585 .uri_cursor = &uri_cursor,
1586 .close_connection_on_server_error = true,
1587 };
1588 ASSERT_SUCCESS(s_tester_init(&options));
1589
1590 struct aws_http_message *request = aws_http2_message_new_request(s_tester.allocator);
1591 ASSERT_NOT_NULL(request);
1592
1593 struct aws_http_header request_headers_src[] = {
1594 DEFINE_HEADER(":method", "GET"),
1595 {
1596 .name = aws_byte_cursor_from_c_str(":scheme"),
1597 .value = *aws_uri_scheme(&s_tester.endpoint),
1598 },
1599 {
1600 .name = aws_byte_cursor_from_c_str(":path"),
1601 .value = s_normalize_path(*aws_uri_path(&s_tester.endpoint)),
1602 },
1603 {
1604 .name = aws_byte_cursor_from_c_str(":authority"),
1605 .value = *aws_uri_host_name(&s_tester.endpoint),
1606 },
1607 DEFINE_HEADER("x-expect-status", "500"),
1608 };
1609 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1610 struct aws_http_make_request_options request_options = {
1611 .self_size = sizeof(request_options),
1612 .request = request,
1613 .user_data = &s_tester,
1614 .on_complete = s_sm_tester_on_stream_complete,
1615 .on_destroy = s_sm_tester_on_stream_destroy,
1616 };
1617 int num_to_acquire = 50;
1618 ASSERT_SUCCESS(s_sm_stream_acquiring_customize_request(num_to_acquire, &request_options));
1619 aws_http_message_release(request);
1620
1621 ASSERT_SUCCESS(s_wait_on_streams_completed_count(num_to_acquire));
1622 ASSERT_INT_EQUALS(0, (int)s_tester.acquiring_stream_errors);
1623 ASSERT_INT_EQUALS(0, (int)s_tester.stream_200_count);
1624 ASSERT_INT_EQUALS(num_to_acquire, s_tester.stream_status_not_200_count);
1625
1626 return s_tester_clean_up();
1627 }
1628
1629 /* Test our http2 stream manager works with prior knowledge */
1630 TEST_CASE(localhost_integ_h2_sm_prior_knowledge) {
1631 (void)ctx;
1632 struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str("http://localhost:3280");
1633 struct sm_tester_options options = {
1634 .max_connections = 100,
1635 .max_concurrent_streams_per_connection = 100,
1636 .alloc = allocator,
1637 .uri_cursor = &uri_cursor,
1638 .prior_knowledge = true,
1639 };
1640 ASSERT_SUCCESS(s_tester_init(&options));
1641 int num_to_acquire = 2;
1642 ASSERT_SUCCESS(s_sm_stream_acquiring(num_to_acquire));
1643 ASSERT_SUCCESS(s_wait_on_streams_completed_count(num_to_acquire));
1644 ASSERT_TRUE((int)s_tester.acquiring_stream_errors == 0);
1645 ASSERT_TRUE((int)s_tester.stream_200_count == num_to_acquire);
1646
1647 return s_tester_clean_up();
1648 }
1649
1650 /* Test that makes tons of real streams against local host */
1651 TEST_CASE(localhost_integ_h2_sm_acquire_stream_stress) {
1652 (void)ctx;
1653 struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str("https://localhost:3443/echo");
1654 struct aws_http_connection_monitoring_options monitor_opt = {
1655 .allowable_throughput_failure_interval_seconds = 2,
1656 .minimum_throughput_bytes_per_second = 1000,
1657 };
1658 enum aws_log_level log_level = AWS_LOG_LEVEL_DEBUG;
1659 struct sm_tester_options options = {
1660 .max_connections = 50,
1661 .max_concurrent_streams_per_connection = 100,
1662 .connection_ping_period_ms = 100 * AWS_TIMESTAMP_MILLIS,
1663 .alloc = allocator,
1664 .uri_cursor = &uri_cursor,
1665 .monitor_opt = &monitor_opt,
1666 .log_level = &log_level,
1667 };
1668 ASSERT_SUCCESS(s_tester_init(&options));
1669 size_t num_to_acquire = 500 * 100;
1670 ASSERT_SUCCESS(s_sm_stream_acquiring((int)num_to_acquire));
1671 ASSERT_SUCCESS(s_wait_on_streams_completed_count(num_to_acquire));
1672 ASSERT_UINT_EQUALS(s_tester.acquiring_stream_errors, 0);
1673 ASSERT_UINT_EQUALS(s_tester.stream_200_count, num_to_acquire);
1674
1675 return s_tester_clean_up();
1676 }
1677
1678 static int s_tester_on_put_body(struct aws_http_stream *stream, const struct aws_byte_cursor *data, void *user_data) {
1679 (void)user_data;
1680 (void)stream;
1681 /* Response is JSON: {"bytes": 2000} - extract the number */
1682 const char *bytes_key = "\"bytes\": ";
1683 const char *json_str = (const char *)data->ptr;
1684 const char *bytes_pos = strstr(json_str, bytes_key);
1685 AWS_FATAL_ASSERT(bytes_pos != NULL);
1686 size_t num_received = (size_t)atoll(bytes_pos + strlen(bytes_key));
1687 AWS_FATAL_ASSERT(s_tester.length_sent == num_received);
1688
1689 return AWS_OP_SUCCESS;
1690 }
1691
1692 static int s_sm_stream_acquiring_with_body(int num_streams) {
1693 char content_length_sprintf_buffer[128] = "";
1694 snprintf(content_length_sprintf_buffer, sizeof(content_length_sprintf_buffer), "%zu", s_tester.length_sent);
1695
1696 struct aws_http_header request_headers_src[] = {
1697 DEFINE_HEADER(":method", "PUT"),
1698 {
1699 .name = aws_byte_cursor_from_c_str(":scheme"),
1700 .value = *aws_uri_scheme(&s_tester.endpoint),
1701 },
1702 {
1703 .name = aws_byte_cursor_from_c_str(":path"),
1704 .value = s_normalize_path(*aws_uri_path(&s_tester.endpoint)),
1705 },
1706 {
1707 .name = aws_byte_cursor_from_c_str(":authority"),
1708 .value = *aws_uri_host_name(&s_tester.endpoint),
1709 },
1710 {
1711 .name = aws_byte_cursor_from_c_str("content_length"),
1712 .value = aws_byte_cursor_from_c_str(content_length_sprintf_buffer),
1713 },
1714 DEFINE_HEADER("x-upload-test", "true"),
1715 };
1716 for (int i = 0; i < num_streams; ++i) {
1717 /* TODO: Test the callback will always be fired asynced, as now the CM cannot ensure the callback happens
1718 * asynchronously, we cannot ensure it as well. */
1719 struct aws_http_message *request = aws_http2_message_new_request(s_tester.allocator);
1720 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1721 struct aws_input_stream *body_stream =
1722 aws_input_stream_tester_upload_new(s_tester.allocator, s_tester.length_sent);
1723 aws_http_message_set_body_stream(request, body_stream);
1724 aws_input_stream_release(body_stream);
1725 struct aws_http_make_request_options request_options = {
1726 .self_size = sizeof(request_options),
1727 .request = request,
1728 .on_response_body = s_tester_on_put_body,
1729 .on_complete = s_sm_tester_on_stream_complete,
1730 };
1731
1732 struct aws_http2_stream_manager_acquire_stream_options acquire_stream_option = {
1733 .options = &request_options,
1734 .callback = s_sm_tester_on_stream_acquired,
1735 .user_data = &s_tester,
1736 };
1737 aws_http2_stream_manager_acquire_stream(s_tester.stream_manager, &acquire_stream_option);
1738 aws_http_message_release(request);
1739 }
1740 return AWS_OP_SUCCESS;
1741 }
1742
1743 /* Test that makes tons of real streams with body against local host */
1744 TEST_CASE(localhost_integ_h2_sm_acquire_stream_stress_with_body) {
1745 (void)ctx;
1746 struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str("https://localhost:3443/echo");
1747 enum aws_log_level log_level = AWS_LOG_LEVEL_DEBUG;
1748 struct sm_tester_options options = {
1749 .max_connections = 100,
1750 .max_concurrent_streams_per_connection = 100,
1751 .connection_ping_period_ms = 100 * AWS_TIMESTAMP_MILLIS,
1752 .alloc = allocator,
1753 .uri_cursor = &uri_cursor,
1754 .log_level = &log_level,
1755 };
1756 ASSERT_SUCCESS(s_tester_init(&options));
1757 s_tester.length_sent = 2000;
1758 int num_to_acquire = 500 * 100;
1759
1760 ASSERT_SUCCESS(s_sm_stream_acquiring_with_body(num_to_acquire));
1761 ASSERT_SUCCESS(s_wait_on_streams_completed_count(num_to_acquire));
1762 ASSERT_UINT_EQUALS(s_tester.acquiring_stream_errors, 0);
1763 ASSERT_UINT_EQUALS(s_tester.stream_200_count, num_to_acquire);
1764
1765 return s_tester_clean_up();
1766 }
1767
1768 /* Test that connection monitor works properly with HTTP/2 stream manager */
1769 TEST_CASE(localhost_integ_h2_sm_connection_monitor_kill_slow_connection) {
1770 (void)ctx;
1771 struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str("https://localhost:3443/echo");
1772 struct aws_http_connection_monitoring_options monitor_opt = {
1773 .allowable_throughput_failure_interval_seconds = 1,
1774 .minimum_throughput_bytes_per_second = 1000,
1775 };
1776 struct sm_tester_options options = {
1777 .max_connections = 100,
1778 .max_concurrent_streams_per_connection = 100,
1779 .alloc = allocator,
1780 .uri_cursor = &uri_cursor,
1781 .monitor_opt = &monitor_opt,
1782 };
1783 ASSERT_SUCCESS(s_tester_init(&options));
1784
1785 struct aws_http_message *request = aws_http2_message_new_request(s_tester.allocator);
1786 ASSERT_NOT_NULL(request);
1787
1788 struct aws_http_header request_headers_src[] = {
1789 DEFINE_HEADER(":method", "GET"),
1790 {
1791 .name = aws_byte_cursor_from_c_str(":scheme"),
1792 .value = *aws_uri_scheme(&s_tester.endpoint),
1793 },
1794 {
1795 .name = aws_byte_cursor_from_c_str(":path"),
1796 .value = s_normalize_path(*aws_uri_path(&s_tester.endpoint)),
1797 },
1798 {
1799 .name = aws_byte_cursor_from_c_str(":authority"),
1800 .value = *aws_uri_host_name(&s_tester.endpoint),
1801 },
1802 DEFINE_HEADER("x-repeat-data", "5000000"),
1803 DEFINE_HEADER("x-slow-response", "true"),
1804 };
1805 aws_http_message_add_header_array(request, request_headers_src, AWS_ARRAY_SIZE(request_headers_src));
1806 struct aws_http_make_request_options request_options = {
1807 .self_size = sizeof(request_options),
1808 .request = request,
1809 .user_data = &s_tester,
1810 .on_complete = s_sm_tester_on_stream_complete,
1811 .on_destroy = s_sm_tester_on_stream_destroy,
1812 };
1813 struct aws_http2_stream_manager_acquire_stream_options acquire_stream_option = {
1814 .options = &request_options,
1815 .callback = s_sm_tester_on_stream_acquired,
1816 .user_data = &s_tester,
1817 };
1818 aws_http2_stream_manager_acquire_stream(s_tester.stream_manager, &acquire_stream_option);
1819 aws_http_message_release(request);
1820
1821 ASSERT_SUCCESS(s_wait_on_streams_completed_count(1));
1822 /* Check the connection closed by connection monitor and the stream should completed with corresponding error */
1823 ASSERT_UINT_EQUALS(s_tester.stream_completed_error_code, AWS_ERROR_HTTP_CONNECTION_CLOSED);
1824
1825 return s_tester_clean_up();
1826 }