Debian Codegraph @

crt/aws-c-s3/tests/s3_max_active_connections_override_test.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/s3/private/s3_client_impl.h"
7 #include "aws/s3/private/s3_meta_request_impl.h"
8 #include "s3_tester.h"
9 #include <aws/testing/aws_test_harness.h>
10
11 #define TEST_CASE(NAME) \
12 AWS_TEST_CASE(NAME, s_test_##NAME); \
13 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
14
15 /**
16 * Test that max_active_connections_override in meta request options is respected
17 * and takes precedence over client-level settings
18 */
19 TEST_CASE(s3_meta_request_max_active_connections_override) {
20 (void)ctx;
21
22 struct aws_s3_tester tester;
23 AWS_ZERO_STRUCT(tester);
24 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
25
26 struct aws_s3_tester_client_options client_options = {
27 .part_size = 5 * 1024 * 1024,
28 /* Set client-level override to 5 */
29 .max_active_connections_override = 5,
30 };
31
32 struct aws_s3_client *client = NULL;
33 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
34
35 /* Verify client-level max_active_connections_override is set */
36 ASSERT_UINT_EQUALS(5, client->max_active_connections_override);
37
38 /* Test 1: Meta request without override should use client's max */
39 {
40 struct aws_http_message *message = aws_s3_test_get_object_request_new(
41 allocator,
42 aws_byte_cursor_from_c_str("s3.us-east-1.amazonaws.com"),
43 aws_byte_cursor_from_c_str("/test-object"));
44
45 struct aws_s3_meta_request_options options = {
46 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT, .message = message,
47 /* No max_active_connections_override set - should use client's */
48 };
49
50 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &options);
51 ASSERT_NOT_NULL(meta_request);
52
53 /* Meta request should not have override set */
54 ASSERT_UINT_EQUALS(0, meta_request->max_active_connections_override);
55
56 /* When meta request has no override, should use client's max (5) */
57 uint32_t max_conns = aws_s3_client_get_max_active_connections(client, meta_request);
58 ASSERT_UINT_EQUALS(5, max_conns);
59
60 aws_s3_meta_request_release(meta_request);
61 aws_http_message_release(message);
62 }
63
64 /* Test 2: Meta request with override should use its own value */
65 {
66 struct aws_http_message *message = aws_s3_test_get_object_request_new(
67 allocator,
68 aws_byte_cursor_from_c_str("s3.us-east-1.amazonaws.com"),
69 aws_byte_cursor_from_c_str("/test-object"));
70
71 struct aws_s3_meta_request_options options = {
72 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
73 .message = message,
74 /* Set meta request override to 2 - should take precedence over client's 5 */
75 .max_active_connections_override = 2,
76 };
77
78 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &options);
79 ASSERT_NOT_NULL(meta_request);
80
81 /* Meta request should have its override set */
82 ASSERT_UINT_EQUALS(2, meta_request->max_active_connections_override);
83
84 /* Should use meta request's override (2), not client's (5) */
85 uint32_t max_conns = aws_s3_client_get_max_active_connections(client, meta_request);
86 ASSERT_UINT_EQUALS(2, max_conns);
87
88 aws_s3_meta_request_release(meta_request);
89 aws_http_message_release(message);
90 }
91
92 /* Test 3: Meta request override is capped at client level */
93 {
94 struct aws_http_message *message = aws_s3_test_get_object_request_new(
95 allocator,
96 aws_byte_cursor_from_c_str("s3.us-east-1.amazonaws.com"),
97 aws_byte_cursor_from_c_str("/test-object"));
98
99 struct aws_s3_meta_request_options options = {
100 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
101 .message = message,
102 /* Set meta request override to 100 - larger than client's 50 */
103 .max_active_connections_override = 100,
104 };
105
106 struct aws_s3_meta_request *meta_request = aws_s3_client_make_meta_request(client, &options);
107 ASSERT_NOT_NULL(meta_request);
108
109 /* Meta request should have its override set */
110 ASSERT_UINT_EQUALS(100, meta_request->max_active_connections_override);
111
112 /* Should be capped at client's max (5), not meta request's (100) */
113 uint32_t max_conns = aws_s3_client_get_max_active_connections(client, meta_request);
114 ASSERT_UINT_EQUALS(5, max_conns);
115
116 aws_s3_meta_request_release(meta_request);
117 aws_http_message_release(message);
118 }
119
120 aws_s3_client_release(client);
121 aws_s3_tester_clean_up(&tester);
122
123 return AWS_OP_SUCCESS;
124 }
125
126 /**
127 * Test data for tracking concurrent connection acquisitions
128 */
129 struct max_connections_test_data {
130 struct aws_allocator *allocator;
131 struct aws_atomic_var current_connections;
132 struct aws_atomic_var peak_connections;
133 uint32_t expected_max;
134 };
135
136 /**
137 * Patched acquire_http_connection that tracks concurrent acquisitions
138 */
139 static void s_acquire_http_connection_track_concurrency(
140 struct aws_http_connection_manager *conn_manager,
141 aws_http_connection_manager_on_connection_setup_fn *callback,
142 void *user_data) {
143
144 struct aws_s3_connection *connection = user_data;
145 struct aws_s3_request *request = connection->request;
146 struct aws_s3_meta_request *meta_request = request->meta_request;
147 struct aws_s3_client *client = meta_request->client;
148 struct aws_s3_tester *tester = client->shutdown_callback_user_data;
149 struct max_connections_test_data *test_data = tester->user_data;
150
151 struct aws_http_manager_metrics metrics;
152 AWS_ZERO_STRUCT(metrics);
153 aws_http_connection_manager_fetch_metrics(conn_manager, &metrics);
154 /* Fetch how much concurrency is leased now. */
155 size_t current = metrics.leased_concurrency + 1;
156
157 /* Update peak if necessary */
158 size_t peak = aws_atomic_load_int(&test_data->peak_connections);
159 while (current > peak) {
160 if (aws_atomic_compare_exchange_int(&test_data->peak_connections, &peak, current)) {
161 break;
162 }
163 }
164
165 /* Get original vtable and call original function */
166 struct aws_s3_client_vtable *original_vtable = aws_s3_tester_get_client_vtable_patch(tester, 0)->original_vtable;
167
168 original_vtable->acquire_http_connection(conn_manager, callback, user_data);
169
170 /* Decrement when done (approximate, but good enough for peak tracking) */
171 aws_atomic_fetch_sub(&test_data->current_connections, 1);
172 }
173
174 /**
175 * Test that max_active_connections_override actually limits concurrent connections
176 */
177 TEST_CASE(s3_max_active_connections_override_enforced) {
178 (void)ctx;
179
180 struct aws_s3_tester tester;
181 AWS_ZERO_STRUCT(tester);
182 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
183
184 /* Set up test data for tracking */
185 struct max_connections_test_data test_data;
186 AWS_ZERO_STRUCT(test_data);
187 test_data.allocator = allocator;
188 aws_atomic_init_int(&test_data.current_connections, 0);
189 aws_atomic_init_int(&test_data.peak_connections, 0);
190 tester.user_data = &test_data;
191
192 /* Create client with default ideal connection count (would be ~25) */
193 struct aws_s3_tester_client_options client_options = {
194 .part_size = 5 * 1024 * 1024,
195 /* Don't override at client level - use defaults */
196 };
197
198 struct aws_s3_client *client = NULL;
199 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
200
201 /* Patch the vtable to track connection acquisitions */
202 struct aws_s3_client_vtable *patched_vtable = aws_s3_tester_patch_client_vtable(&tester, client, NULL);
203 patched_vtable->acquire_http_connection = s_acquire_http_connection_track_concurrency;
204
205 struct aws_string *host_name =
206 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
207
208 /* Create a 200MiB upload with 5MiB parts (40 parts total) to test concurrent connection limit */
209 size_t object_size = 200 * 1024 * 1024;
210 struct aws_byte_buf path_buf;
211 AWS_ZERO_STRUCT(path_buf);
212 ASSERT_SUCCESS(aws_s3_tester_upload_file_path_init(
213 tester.allocator, &path_buf, aws_byte_cursor_from_c_str("/test-upload-200MB")));
214
215 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_buf(&path_buf);
216 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_string(host_name);
217
218 struct aws_input_stream *input_stream = aws_s3_test_input_stream_new(allocator, object_size);
219
220 struct aws_http_message *message = aws_s3_test_put_object_request_new(
221 allocator, &host_cursor, test_object_path, g_test_body_content_type, input_stream, 0 /*flags*/);
222
223 struct aws_s3_meta_request_options options = {
224 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
225 .message = message,
226 .max_active_connections_override = 3,
227 };
228
229 struct aws_s3_meta_request_test_results test_results;
230 aws_s3_meta_request_test_results_init(&test_results, allocator);
231
232 /* Send the request */
233 ASSERT_SUCCESS(aws_s3_tester_send_meta_request(
234 &tester, client, &options, &test_results, AWS_S3_TESTER_SEND_META_REQUEST_EXPECT_SUCCESS));
235
236 /* Verify the peak connections never exceeded the override */
237 size_t peak = aws_atomic_load_int(&test_data.peak_connections);
238 /**
239 * TODO: this test seems a bit flaky. Sometime the peak we collect is like one more than expected. Maybe some race
240 * conditions that release and acquire happening. Check it against either the expected or expected + 1 for now.
241 */
242 ASSERT_TRUE(peak == options.max_active_connections_override || peak == options.max_active_connections_override + 1);
243
244 aws_input_stream_destroy(input_stream);
245 aws_string_destroy(host_name);
246 aws_byte_buf_clean_up(&path_buf);
247 aws_s3_meta_request_test_results_clean_up(&test_results);
248 aws_http_message_release(message);
249 aws_s3_client_release(client);
250 aws_s3_tester_clean_up(&tester);
251
252 return AWS_OP_SUCCESS;
253 }