Debian Codegraph @

crt/aws-c-s3/tests/s3_client_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_request.h"
8 #include "aws/s3/private/s3_util.h"
9 #include "s3_tester.h"
10
11 #include <aws/common/clock.h>
12 #include <aws/testing/aws_test_harness.h>
13
14 #define TEST_CASE(NAME) \
15 AWS_TEST_CASE(NAME, s_test_##NAME); \
16 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
17
18 #define DEFINE_HEADER(NAME, VALUE) \
19 { \
20 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \
21 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \
22 }
23
24 static void s_init_mock_s3_request_upload_part_timeout(
25 struct aws_s3_request *mock_request,
26 uint64_t original_upload_timeout_ms,
27 uint64_t request_time_ns,
28 uint64_t response_to_first_byte_time_ns) {
29 mock_request->upload_timeout_ms = (size_t)original_upload_timeout_ms;
30 struct aws_s3_request_metrics *metrics = mock_request->send_data.metrics;
31
32 metrics->time_metrics.send_start_timestamp_ns = 0;
33 metrics->time_metrics.send_end_timestamp_ns = 0;
34 metrics->time_metrics.receive_end_timestamp_ns = request_time_ns;
35 metrics->time_metrics.receive_start_timestamp_ns = response_to_first_byte_time_ns;
36 }
37
38 static int s_starts_upload_retry(struct aws_s3_client *client, struct aws_s3_request *mock_request) {
39 uint64_t average_time_ns = aws_timestamp_convert(
40 300, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); /* 0.3 Secs, average for upload a part */
41 AWS_ZERO_STRUCT(client->synced_data.upload_part_stats);
42
43 s_init_mock_s3_request_upload_part_timeout(mock_request, 0, average_time_ns, average_time_ns);
44 size_t init_count = client->ideal_connection_count;
45 size_t p90_count = init_count / 10 + 1;
46 for (size_t i = 0; i < init_count - p90_count; i++) {
47 /* With 90% of the average request time. */
48 aws_s3_client_update_upload_part_timeout(client, mock_request, AWS_ERROR_SUCCESS);
49 }
50
51 uint64_t one_sec_time_ns = aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); /* 1 Secs */
52 s_init_mock_s3_request_upload_part_timeout(mock_request, 0, one_sec_time_ns, one_sec_time_ns);
53 for (size_t i = 0; i < p90_count; i++) {
54 /* 10 percent of the request takes 1 sec */
55 aws_s3_client_update_upload_part_timeout(client, mock_request, AWS_ERROR_SUCCESS);
56 }
57 /* Check that retry should be turned off */
58 ASSERT_FALSE(client->synced_data.upload_part_stats.stop_timeout);
59 size_t current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
60 /* We start the retry with a default 1 sec timeout */
61 ASSERT_UINT_EQUALS(1000, current_timeout_ms);
62 return AWS_OP_SUCCESS;
63 }
64
65 /* Test the aws_s3_client_update_upload_part_timeout works as expected */
66 TEST_CASE(client_update_upload_part_timeout) {
67 (void)ctx;
68 struct aws_s3_tester tester;
69 AWS_ZERO_STRUCT(tester);
70 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
71 struct aws_s3_client *client = NULL;
72 struct aws_s3_tester_client_options client_options = {
73 .part_size = MB_TO_BYTES(8),
74 .tls_usage = AWS_S3_TLS_DISABLED,
75 };
76 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
77 struct aws_s3_request mock_request;
78 struct aws_s3_request_metrics metrics;
79 AWS_ZERO_STRUCT(mock_request);
80 AWS_ZERO_STRUCT(metrics);
81 mock_request.send_data.metrics = &metrics;
82
83 uint64_t large_time_ns =
84 aws_timestamp_convert(5500, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); /* 5.5 Secs, larger than 5 secs */
85
86 uint64_t average_time_ns = aws_timestamp_convert(
87 250, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); /* 0.25 Secs, close to average for upload a part */
88
89 size_t init_count = client->ideal_connection_count;
90 {
91 /* 1. If the request time is larger than 5 secs, we don't do retry */
92 AWS_ZERO_STRUCT(client->synced_data.upload_part_stats);
93 s_init_mock_s3_request_upload_part_timeout(&mock_request, 0, large_time_ns, average_time_ns);
94
95 /* If request timeout happened before the retry started, it has no effects. */
96 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
97 for (size_t i = 0; i < init_count; i++) {
98 /* Mock a number of requests completed with the large time for the request */
99 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_SUCCESS);
100 }
101
102 ASSERT_TRUE(client->synced_data.upload_part_stats.stop_timeout);
103 size_t current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
104 ASSERT_UINT_EQUALS(0, current_timeout_ms);
105 /* clean up */
106 if (client->synced_data.upload_part_stats.initial_request_time.collecting_p90) {
107 aws_priority_queue_clean_up(&client->synced_data.upload_part_stats.initial_request_time.p90_samples);
108 client->synced_data.upload_part_stats.initial_request_time.collecting_p90 = false;
109 }
110 }
111 {
112 /* 2.1. Test that the P90 of the init samples are used correctly and at least 1 sec */
113 AWS_ZERO_STRUCT(client->synced_data.upload_part_stats);
114 /* Hack around to set the ideal connection time for testing. */
115 size_t test_init_connection = 1000;
116 *(uint32_t *)(void *)&client->ideal_connection_count = (uint32_t)test_init_connection;
117 for (size_t i = 0; i < test_init_connection; i++) {
118 /* Mock a number of requests completed with the large time for the request */
119 uint64_t time_ns = aws_timestamp_convert(i, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
120 s_init_mock_s3_request_upload_part_timeout(&mock_request, 0, time_ns, time_ns);
121 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_SUCCESS);
122 }
123
124 size_t current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
125 /* the P90 of the results is 900, but it has to be at least 1000 */
126 ASSERT_UINT_EQUALS(1000, current_timeout_ms);
127 /* clean up */
128 if (client->synced_data.upload_part_stats.initial_request_time.collecting_p90) {
129 aws_priority_queue_clean_up(&client->synced_data.upload_part_stats.initial_request_time.p90_samples);
130 client->synced_data.upload_part_stats.initial_request_time.collecting_p90 = false;
131 }
132 /* Change it back */
133 *(uint32_t *)(void *)&client->ideal_connection_count = (uint32_t)init_count;
134 }
135 {
136 /* 2.2. Test that the P90 of the init samples are used correctly */
137 AWS_ZERO_STRUCT(client->synced_data.upload_part_stats);
138 /* Hack around to set the ideal connection time for testing. */
139 size_t test_init_connection = 10000;
140 *(uint32_t *)(void *)&client->ideal_connection_count = (uint32_t)test_init_connection;
141 for (size_t i = 0; i < test_init_connection; i++) {
142 /* Mock a number of requests completed with the large time for the request */
143 uint64_t time_ns = aws_timestamp_convert(i, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
144 s_init_mock_s3_request_upload_part_timeout(&mock_request, 0, time_ns, time_ns);
145 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_SUCCESS);
146 }
147
148 size_t current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
149 /* P90 is 9000 */
150 ASSERT_UINT_EQUALS(9000, current_timeout_ms);
151 /* clean up */
152 if (client->synced_data.upload_part_stats.initial_request_time.collecting_p90) {
153 aws_priority_queue_clean_up(&client->synced_data.upload_part_stats.initial_request_time.p90_samples);
154 client->synced_data.upload_part_stats.initial_request_time.collecting_p90 = false;
155 }
156 /* Change it back */
157 *(uint32_t *)(void *)&client->ideal_connection_count = (uint32_t)init_count;
158 }
159
160 {
161 ASSERT_SUCCESS(s_starts_upload_retry(client, &mock_request));
162 /**
163 * 3. Once a request finishes without timeout, use the average response_to_first_byte_time +
164 * g_expect_timeout_offset_ms as our expected timeout. (TODO: The real expected timeout should be a P99 of
165 * all the requests.)
166 * 3.1 Adjust the current timeout against the expected timeout, via 0.99 * <current timeout> + 0.01 * <expected
167 * timeout> to get closer to the expected timeout.
168 */
169 s_init_mock_s3_request_upload_part_timeout(
170 &mock_request,
171 aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL),
172 average_time_ns,
173 average_time_ns);
174
175 /* After 1000 runs, we have the timeout match the "expected" (average time + g_expect_timeout_offset_ms) timeout
176 */
177 for (size_t i = 0; i < 1000; i++) {
178 /* Mock a number of requests completed with the large time for the request */
179 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_SUCCESS);
180 }
181 size_t current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
182 ASSERT_UINT_EQUALS(
183 aws_timestamp_convert(average_time_ns, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL) +
184 g_expect_timeout_offset_ms,
185 current_timeout_ms);
186
187 /* will not change after another 1k run */
188 for (size_t i = 0; i < 1000; i++) {
189 /* Mock a number of requests completed with the large time for the request */
190 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_SUCCESS);
191 }
192 ASSERT_FALSE(client->synced_data.upload_part_stats.stop_timeout);
193 current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
194 /* After 1000 runs, we have the timeout match the "expected" (average time + g_expect_timeout_offset_ms) timeout
195 */
196 ASSERT_UINT_EQUALS(
197 aws_timestamp_convert(average_time_ns, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL) +
198 g_expect_timeout_offset_ms,
199 current_timeout_ms);
200 /* clean up */
201 if (client->synced_data.upload_part_stats.initial_request_time.collecting_p90) {
202 aws_priority_queue_clean_up(&client->synced_data.upload_part_stats.initial_request_time.p90_samples);
203 client->synced_data.upload_part_stats.initial_request_time.collecting_p90 = false;
204 }
205 }
206
207 {
208 ASSERT_SUCCESS(s_starts_upload_retry(client, &mock_request));
209 /**
210 * 4.1 If timeout rate is larger than 0.1%, we increase the timeout by 100ms (Check the timeout when the
211 * request was made, if the updated timeout is larger than the expected, skip update).
212 */
213 /* Set current timeout rate to be around 0.1% */
214 client->synced_data.upload_part_stats.timeout_rate_tracking.num_completed = 800;
215 client->synced_data.upload_part_stats.timeout_rate_tracking.num_failed = 1;
216
217 /* Update the timeout as the rate is larger than 0.1% */
218 s_init_mock_s3_request_upload_part_timeout(&mock_request, 1000 /*original_upload_timeout_ms*/, 0, 0);
219 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
220 size_t current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
221 /* 1.1 secs */
222 ASSERT_UINT_EQUALS(1100, current_timeout_ms);
223 /* The same timeout applied to multiple requests made before, and the timeout happened right after we already
224 * updated it. The timeout will not be updated again. */
225 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
226 ASSERT_FALSE(client->synced_data.upload_part_stats.stop_timeout);
227 current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
228 /* 1.1 secs, still */
229 ASSERT_UINT_EQUALS(1100, current_timeout_ms);
230 /* clean up */
231 if (client->synced_data.upload_part_stats.initial_request_time.collecting_p90) {
232 aws_priority_queue_clean_up(&client->synced_data.upload_part_stats.initial_request_time.p90_samples);
233 client->synced_data.upload_part_stats.initial_request_time.collecting_p90 = false;
234 }
235 }
236
237 {
238 ASSERT_SUCCESS(s_starts_upload_retry(client, &mock_request));
239 /**
240 * 4.2 If timeout rate is larger than 1%, we increase the timeout by 1 secs (If needed). And clear the rate
241 * to get the exact rate with new timeout.
242 */
243
244 /* Assume our first batch requests all failed with the 1 sec timeout. As the request around 3 secs to
245 * complete */
246
247 uint64_t real_response_time_ns =
248 aws_timestamp_convert(3000 - g_expect_timeout_offset_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
249 s_init_mock_s3_request_upload_part_timeout(
250 &mock_request, 1000 /*original_upload_timeout_ms*/, real_response_time_ns, real_response_time_ns);
251
252 /* First failure will not change the timeout, as we use the ceiling of 1% rate */
253 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
254 size_t current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
255 ASSERT_UINT_EQUALS(1000, current_timeout_ms);
256
257 /* Updated at the second timeout */
258 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
259 current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
260 ASSERT_UINT_EQUALS(2000, current_timeout_ms);
261 /* The rest of the batch failure will not affect the timeout */
262 for (size_t i = 0; i < 10; i++) {
263 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
264 }
265 current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
266 /* still 2 secs */
267 ASSERT_UINT_EQUALS(2000, current_timeout_ms);
268
269 /* The 2 secs will still fail the whole batch */
270 s_init_mock_s3_request_upload_part_timeout(
271 &mock_request,
272 current_timeout_ms /*original_upload_timeout_ms*/,
273 real_response_time_ns,
274 real_response_time_ns);
275 for (size_t i = 0; i < 10; i++) {
276 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
277 }
278 current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
279 /* 3 secs now */
280 ASSERT_UINT_EQUALS(3000, current_timeout_ms);
281
282 /* 3 secs will result in around 0.1% failure, and we are okay with that */
283 s_init_mock_s3_request_upload_part_timeout(
284 &mock_request,
285 current_timeout_ms /*original_upload_timeout_ms*/,
286 real_response_time_ns,
287 real_response_time_ns);
288 /* 1 failure, and others all succeed */
289 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
290 for (size_t i = 0; i < 10; i++) {
291 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_SUCCESS);
292 }
293 /* still 3 secs */
294 current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
295 ASSERT_UINT_EQUALS(3000, current_timeout_ms);
296 ASSERT_FALSE(client->synced_data.upload_part_stats.stop_timeout);
297 /* clean up */
298 if (client->synced_data.upload_part_stats.initial_request_time.collecting_p90) {
299 aws_priority_queue_clean_up(&client->synced_data.upload_part_stats.initial_request_time.p90_samples);
300 client->synced_data.upload_part_stats.initial_request_time.collecting_p90 = false;
301 }
302 }
303
304 {
305 ASSERT_SUCCESS(s_starts_upload_retry(client, &mock_request));
306 /* 4.3 Once the timeout is larger than 5 secs, we stop the process. */
307 s_init_mock_s3_request_upload_part_timeout(&mock_request, 1000 /*original_upload_timeout_ms*/, 0, 0);
308
309 for (size_t i = 0; i < 10; i++) {
310 /* Make two continuous timeout request with updated timeout */
311 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
312 aws_s3_client_update_upload_part_timeout(client, &mock_request, AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT);
313 size_t current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
314 s_init_mock_s3_request_upload_part_timeout(
315 &mock_request, current_timeout_ms /*original_upload_timeout_ms*/, 0, 0);
316 }
317 /* Timeout stopped */
318 size_t current_timeout_ms = aws_atomic_load_int(&client->upload_timeout_ms);
319 ASSERT_UINT_EQUALS(0, current_timeout_ms);
320 ASSERT_TRUE(client->synced_data.upload_part_stats.stop_timeout);
321 }
322
323 aws_s3_client_release(client);
324 aws_s3_tester_clean_up(&tester);
325 return AWS_OP_SUCCESS;
326 }
327
328 /* Test meta request can override the part size as expected */
329 TEST_CASE(client_meta_request_override_part_size) {
330 (void)ctx;
331 struct aws_s3_tester tester;
332 AWS_ZERO_STRUCT(tester);
333 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
334 struct aws_s3_client *client = NULL;
335 struct aws_s3_tester_client_options client_options = {
336 .part_size = MB_TO_BYTES(8),
337 .tls_usage = AWS_S3_TLS_DISABLED,
338 };
339 ASSERT_SUCCESS(aws_s3_tester_client_new(&tester, &client_options, &client));
340
341 struct aws_string *host_name =
342 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
343 struct aws_byte_cursor host_cur = aws_byte_cursor_from_string(host_name);
344 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_c_str("/mytest");
345
346 size_t override_part_size = MB_TO_BYTES(10);
347 size_t content_length =
348 MB_TO_BYTES(20); /* Let the content length larger than the override part size to make sure we do MPU */
349
350 /* MPU put object */
351 struct aws_input_stream_tester_options stream_options = {
352 .autogen_length = content_length,
353 };
354 struct aws_input_stream *input_stream = aws_input_stream_new_tester(allocator, &stream_options);
355
356 struct aws_http_message *put_messages = aws_s3_test_put_object_request_new(
357 allocator, &host_cur, g_test_body_content_type, test_object_path, input_stream, 0 /*flags*/);
358
359 struct aws_s3_meta_request_options meta_request_options = {
360 .message = put_messages,
361 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
362 .part_size = override_part_size,
363 };
364 struct aws_s3_meta_request *put_meta_request = client->vtable->meta_request_factory(client, &meta_request_options);
365 ASSERT_UINT_EQUALS(put_meta_request->part_size, override_part_size);
366
367 /* auto ranged Get Object */
368 struct aws_http_message *get_message = aws_s3_test_get_object_request_new(
369 allocator, aws_byte_cursor_from_string(host_name), g_pre_existing_object_1MB);
370
371 struct aws_s3_meta_request_options get_meta_request_options = {
372 .message = get_message,
373 .type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT,
374 .part_size = override_part_size,
375 };
376
377 struct aws_s3_meta_request *get_meta_request =
378 client->vtable->meta_request_factory(client, &get_meta_request_options);
379 ASSERT_UINT_EQUALS(get_meta_request->part_size, override_part_size);
380
381 aws_http_message_release(put_messages);
382 aws_s3_meta_request_release(put_meta_request);
383 aws_http_message_release(get_message);
384 aws_s3_meta_request_release(get_meta_request);
385 aws_string_destroy(host_name);
386 aws_s3_client_release(client);
387 aws_input_stream_release(input_stream);
388 aws_s3_tester_clean_up(&tester);
389
390 return AWS_OP_SUCCESS;
391 }
392
393 /* Test meta request can override the multipart upload threshold as expected */
394 TEST_CASE(client_meta_request_override_multipart_upload_threshold) {
395 (void)ctx;
396 struct aws_s3_tester tester;
397 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
398
399 struct aws_s3_client_config client_config = {
400 .part_size = MB_TO_BYTES(8),
401 .multipart_upload_threshold = MB_TO_BYTES(15),
402 };
403
404 ASSERT_SUCCESS(aws_s3_tester_bind_client(
405 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
406
407 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
408
409 ASSERT_TRUE(client != NULL);
410
411 struct aws_string *host_name =
412 aws_s3_tester_build_endpoint_string(allocator, &g_test_bucket_name, &g_test_s3_region);
413 struct aws_byte_cursor host_cur = aws_byte_cursor_from_string(host_name);
414 struct aws_byte_cursor test_object_path = aws_byte_cursor_from_c_str("/mytest");
415
416 size_t override_multipart_upload_threshold = MB_TO_BYTES(20);
417 size_t content_length =
418 MB_TO_BYTES(20); /* Let the content length larger than the override part size to make sure we do MPU */
419
420 /* MPU put object */
421 struct aws_input_stream_tester_options stream_options = {
422 .autogen_length = content_length,
423 };
424 struct aws_input_stream *input_stream = aws_input_stream_new_tester(allocator, &stream_options);
425
426 struct aws_http_message *put_messages = aws_s3_test_put_object_request_new(
427 allocator, &host_cur, g_test_body_content_type, test_object_path, input_stream, 0 /*flags*/);
428
429 {
430 /* Content length is smaller than the override multipart_upload_threshold */
431 struct aws_s3_meta_request_options meta_request_options = {
432 .message = put_messages,
433 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
434 .multipart_upload_threshold = override_multipart_upload_threshold,
435 };
436 struct aws_s3_meta_request *put_meta_request =
437 client->vtable->meta_request_factory(client, &meta_request_options);
438
439 /* Part size will be 0, as we don't use MPU */
440 ASSERT_UINT_EQUALS(put_meta_request->part_size, 0);
441 aws_s3_meta_request_release(put_meta_request);
442 }
443
444 {
445 /* meta request override the part size, so the override part size will be used as the multipart upload threshold
446 */
447 struct aws_s3_meta_request_options meta_request_options = {
448 .message = put_messages,
449 .type = AWS_S3_META_REQUEST_TYPE_PUT_OBJECT,
450 .part_size = override_multipart_upload_threshold,
451 };
452 struct aws_s3_meta_request *put_meta_request =
453 client->vtable->meta_request_factory(client, &meta_request_options);
454
455 /* Part size will be 0, as we don't use MPU */
456 ASSERT_UINT_EQUALS(put_meta_request->part_size, 0);
457 aws_s3_meta_request_release(put_meta_request);
458 }
459
460 aws_http_message_release(put_messages);
461 aws_string_destroy(host_name);
462 aws_s3_client_release(client);
463 aws_input_stream_release(input_stream);
464 aws_s3_tester_clean_up(&tester);
465
466 return AWS_OP_SUCCESS;
467 }
468
469 /* Test that optimal range size is calculated and stored correctly in client config */
470 TEST_CASE(s3_calculate_client_optimal_range_size) {
471 (void)ctx;
472 struct aws_s3_tester tester;
473 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
474
475 /* Test with default configuration */
476 {
477 struct aws_s3_client_config client_config = {
478 .part_size = MB_TO_BYTES(8),
479 .throughput_target_gbps = 10.0,
480 .memory_limit_in_bytes = GB_TO_BYTES(2),
481 };
482
483 ASSERT_SUCCESS(aws_s3_tester_bind_client(
484 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
485
486 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
487 ASSERT_TRUE(client != NULL);
488
489 /* Verify optimal range size is calculated and within expected bounds */
490 ASSERT_TRUE(client->optimal_range_size >= MB_TO_BYTES(8)); /* At least 8MiB minimum */
491 ASSERT_TRUE(client->optimal_range_size <= GB_TO_BYTES(1)); /* At most 1GB maximum */
492
493 /* Verify it's properly initialized (not zero) */
494 ASSERT_TRUE(client->optimal_range_size > 0);
495
496 aws_s3_client_release(client);
497 }
498
499 /* Test with high memory limit */
500 {
501 struct aws_s3_client_config client_config = {
502 .part_size = MB_TO_BYTES(8),
503 .throughput_target_gbps = 10.0,
504 .memory_limit_in_bytes = GB_TO_BYTES(16),
505 };
506
507 ASSERT_SUCCESS(aws_s3_tester_bind_client(
508 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
509
510 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
511 ASSERT_TRUE(client != NULL);
512
513 /* With higher memory limit, optimal range size should be larger but capped at 1GB */
514 ASSERT_TRUE(client->optimal_range_size >= MB_TO_BYTES(8));
515 ASSERT_TRUE(client->optimal_range_size <= GB_TO_BYTES(1)); /* Capped at 1GB */
516
517 aws_s3_client_release(client);
518 }
519
520 /* Test with low memory limit */
521 {
522 struct aws_s3_client_config client_config = {
523 .part_size = MB_TO_BYTES(8),
524 .throughput_target_gbps = 100.0, /* High throughput = more connections */
525 .memory_limit_in_bytes = MB_TO_BYTES(512), /* Low memory */
526 };
527
528 ASSERT_SUCCESS(aws_s3_tester_bind_client(
529 &tester, &client_config, AWS_S3_TESTER_BIND_CLIENT_REGION | AWS_S3_TESTER_BIND_CLIENT_SIGNING));
530
531 struct aws_s3_client *client = aws_s3_client_new(allocator, &client_config);
532 ASSERT_TRUE(client != NULL);
533
534 /* With low memory and high connection count, should still respect 8MiB minimum */
535 ASSERT_TRUE(client->optimal_range_size >= MB_TO_BYTES(8));
536
537 aws_s3_client_release(client);
538 }
539
540 aws_s3_tester_clean_up(&tester);
541 return AWS_OP_SUCCESS;
542 }