Debian Codegraph @

crt/aws-c-s3/tests/s3_parallel_read_stream_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_default_buffer_pool.h"
7 #include "aws/s3/private/s3_parallel_input_stream.h"
8 #include "aws/s3/private/s3_part_streaming_input_stream.h"
9 #include "aws/s3/private/s3_util.h"
10 #include "aws/s3/s3_client.h"
11 #include "s3_tester.h"
12 #include <aws/common/clock.h>
13 #include <aws/common/file.h>
14 #include <aws/common/string.h>
15 #include <aws/io/event_loop.h>
16 #include <aws/io/future.h>
17 #include <aws/io/stream.h>
18 #include <aws/io/uri.h>
19 #include <aws/testing/aws_test_harness.h>
20 #include <inttypes.h>
21
22 #include <sys/stat.h>
23
24 #define TEST_CASE(NAME) \
25 AWS_TEST_CASE(NAME, s_test_##NAME); \
26 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
27
28 #define DEFINE_HEADER(NAME, VALUE) \
29 { \
30 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \
31 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \
32 }
33
34 #define ONE_SEC_IN_NS ((uint64_t)AWS_TIMESTAMP_NANOS)
35 #define MAX_TIMEOUT_NS (600 * ONE_SEC_IN_NS)
36
37 AWS_STATIC_STRING_FROM_LITERAL(s_parallel_stream_test, "SimpleParallelStreamTest");
38
39 static int s_create_read_file(const char *file_path, size_t length) {
40 remove(file_path);
41
42 FILE *file = aws_fopen(file_path, "w");
43 size_t loop = length / s_parallel_stream_test->len;
44 for (size_t i = 0; i < loop; ++i) {
45 fprintf(file, "%s", (char *)s_parallel_stream_test->bytes);
46 }
47 size_t reminder = length % s_parallel_stream_test->len;
48 if (reminder) {
49 fprintf(file, "%.*s", (int)reminder, s_parallel_stream_test->bytes);
50 }
51 fclose(file);
52 return AWS_OP_SUCCESS;
53 }
54
55 struct aws_parallel_read_from_test_args {
56 struct aws_allocator *alloc;
57
58 size_t buffer_start_pos;
59 uint64_t file_offset;
60 size_t read_length;
61 struct aws_future_bool *final_end_future;
62 struct aws_byte_buf *final_dest;
63
64 struct aws_parallel_input_stream *parallel_read_stream;
65 struct aws_atomic_var *completed_count;
66 struct aws_atomic_var *end_of_stream;
67
68 size_t split_num;
69 };
70
71 static void s_s3_parallel_from_file_read_test_task(struct aws_task *task, void *arg, enum aws_task_status task_status) {
72 (void)task_status;
73 struct aws_parallel_read_from_test_args *test_args = arg;
74
75 struct aws_byte_buf read_buf = {
76 .allocator = NULL,
77 .buffer = test_args->final_dest->buffer + test_args->buffer_start_pos,
78 .len = 0,
79 .capacity = test_args->read_length,
80 };
81
82 /* Updated API call with offset and max_length */
83 struct aws_future_bool *read_future = aws_parallel_input_stream_read(
84 test_args->parallel_read_stream, test_args->file_offset, test_args->read_length, &read_buf);
85
86 aws_future_bool_wait(read_future, MAX_TIMEOUT_NS);
87 bool eos_reached = aws_future_bool_get_result(read_future);
88 aws_future_bool_release(read_future);
89
90 struct aws_future_bool *end_future = test_args->final_end_future;
91 size_t read_completed = aws_atomic_fetch_add(test_args->completed_count, 1);
92 if (eos_reached) {
93 aws_atomic_store_int(test_args->end_of_stream, 1);
94 }
95 bool completed = read_completed == test_args->split_num - 1;
96
97 bool reached_eos = aws_atomic_load_int(test_args->end_of_stream) == 1;
98 aws_mem_release(test_args->alloc, task);
99 aws_mem_release(test_args->alloc, test_args);
100 if (completed) {
101 aws_future_bool_set_result(end_future, reached_eos);
102 }
103 aws_future_bool_release(end_future);
104 }
105
106 static int s_parallel_read_test_helper(
107 struct aws_allocator *alloc,
108 struct aws_parallel_input_stream *parallel_read_stream,
109 struct aws_byte_buf *read_buf,
110 struct aws_event_loop_group *elg,
111 size_t start_pos,
112 size_t total_length,
113 size_t split_num,
114 bool *out_eos) {
115
116 struct aws_atomic_var completed_count;
117 aws_atomic_store_int(&completed_count, 0);
118 struct aws_atomic_var end_of_stream;
119 aws_atomic_store_int(&end_of_stream, 0);
120 size_t number_bytes_per_read = total_length / split_num;
121 if (number_bytes_per_read == 0) {
122 /* Updated API call with offset and max_length */
123 struct aws_byte_buf temp_buf = {
124 .allocator = NULL,
125 .buffer = read_buf->buffer,
126 .len = 0,
127 .capacity = total_length,
128 };
129 struct aws_future_bool *read_future =
130 aws_parallel_input_stream_read(parallel_read_stream, start_pos, total_length, &temp_buf);
131
132 ASSERT_TRUE(aws_future_bool_wait(read_future, MAX_TIMEOUT_NS));
133 aws_future_bool_release(read_future);
134 read_buf->len = temp_buf.len;
135 return AWS_OP_SUCCESS;
136 }
137
138 struct aws_future_bool *future = aws_future_bool_new(alloc);
139 for (size_t i = 0; i < split_num; i++) {
140 struct aws_event_loop *loop = aws_event_loop_group_get_next_loop(elg);
141 struct aws_parallel_read_from_test_args *test_args =
142 aws_mem_calloc(alloc, 1, sizeof(struct aws_parallel_read_from_test_args));
143
144 size_t read_length = number_bytes_per_read;
145 if (i == split_num - 1) {
146 /* Last part, adjust the size */
147 read_length += total_length % split_num;
148 }
149
150 test_args->alloc = alloc;
151 test_args->buffer_start_pos = i * number_bytes_per_read;
152 test_args->file_offset = start_pos + test_args->buffer_start_pos;
153 test_args->final_end_future = aws_future_bool_acquire(future);
154 test_args->read_length = read_length;
155 test_args->final_dest = read_buf;
156 test_args->parallel_read_stream = parallel_read_stream;
157 test_args->completed_count = &completed_count;
158 test_args->end_of_stream = &end_of_stream;
159 test_args->split_num = split_num;
160
161 struct aws_task *read_task = aws_mem_calloc(alloc, 1, sizeof(struct aws_task));
162 aws_task_init(read_task, s_s3_parallel_from_file_read_test_task, test_args, "s3_parallel_read_test_task");
163 aws_event_loop_schedule_task_now(loop, read_task);
164 }
165
166 ASSERT_TRUE(aws_future_bool_wait(future, MAX_TIMEOUT_NS));
167 *out_eos = aws_future_bool_get_result(future);
168 aws_future_bool_release(future);
169 read_buf->len = total_length;
170 return AWS_OP_SUCCESS;
171 }
172
173 TEST_CASE(parallel_read_stream_from_file_sanity_test) {
174 (void)ctx;
175 struct aws_s3_tester tester;
176 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
177
178 const char *file_path = "s3_test_parallel_input_stream_read.txt"; /* unique name */
179 ASSERT_SUCCESS(s_create_read_file(file_path, s_parallel_stream_test->len));
180 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_c_str(file_path);
181
182 /* Create an event loop group for the parallel input stream */
183 struct aws_event_loop_group *reading_elg = aws_event_loop_group_new_default(allocator, 1, NULL);
184 ASSERT_NOT_NULL(reading_elg);
185
186 struct aws_parallel_input_stream *parallel_read_stream =
187 aws_parallel_input_stream_new_from_file(allocator, path_cursor, reading_elg, false /*direct_io*/);
188 ASSERT_NOT_NULL(parallel_read_stream);
189
190 aws_parallel_input_stream_acquire(parallel_read_stream);
191 aws_parallel_input_stream_release(parallel_read_stream);
192 struct aws_event_loop_group *el_group = aws_event_loop_group_new_default(allocator, 0, NULL);
193
194 {
195 struct aws_byte_buf read_buf;
196 aws_byte_buf_init(&read_buf, allocator, s_parallel_stream_test->len);
197 bool eos_reached = false;
198 ASSERT_SUCCESS(s_parallel_read_test_helper(
199 allocator, parallel_read_stream, &read_buf, el_group, 0, s_parallel_stream_test->len, 8, &eos_reached));
200
201 /* Read the exact number of bytes will not reach to the EOS */
202 ASSERT_FALSE(eos_reached);
203 ASSERT_TRUE(aws_string_eq_byte_buf(s_parallel_stream_test, &read_buf));
204 aws_byte_buf_clean_up(&read_buf);
205 }
206
207 {
208 size_t extra_byte_len = s_parallel_stream_test->len + 1;
209 struct aws_byte_buf read_buf;
210 aws_byte_buf_init(&read_buf, allocator, extra_byte_len);
211 bool eos_reached = false;
212 ASSERT_SUCCESS(s_parallel_read_test_helper(
213 allocator, parallel_read_stream, &read_buf, el_group, 0, extra_byte_len, 8, &eos_reached));
214
215 /* Read the exact number of bytes will not reach to the EOS */
216 ASSERT_TRUE(eos_reached);
217 aws_byte_buf_clean_up(&read_buf);
218 }
219
220 {
221 /* Failure from short buffer */
222 struct aws_byte_buf read_buf;
223 aws_byte_buf_init(&read_buf, allocator, s_parallel_stream_test->len);
224 /* Set the buffer length to be capacity */
225 read_buf.len = s_parallel_stream_test->len;
226
227 /* Updated API call with offset and max_length */
228 struct aws_future_bool *read_future =
229 aws_parallel_input_stream_read(parallel_read_stream, 0, s_parallel_stream_test->len, &read_buf);
230
231 ASSERT_TRUE(aws_future_bool_is_done(read_future));
232 int error = aws_future_bool_get_error(read_future);
233 ASSERT_UINT_EQUALS(AWS_ERROR_SHORT_BUFFER, error);
234 aws_byte_buf_clean_up(&read_buf);
235 aws_future_bool_release(read_future);
236 }
237
238 {
239 /* offset larger than the length of file, will read nothing and return EOS */
240 struct aws_byte_buf read_buf;
241 aws_byte_buf_init(&read_buf, allocator, s_parallel_stream_test->len);
242
243 /* Updated API call with offset and max_length */
244 struct aws_future_bool *read_future = aws_parallel_input_stream_read(
245 parallel_read_stream, 2 * s_parallel_stream_test->len, s_parallel_stream_test->len, &read_buf);
246
247 /* Wait for the future to finish */
248 ASSERT_TRUE(aws_future_bool_wait(read_future, MAX_TIMEOUT_NS));
249 ASSERT_TRUE(aws_future_bool_is_done(read_future));
250 int error = aws_future_bool_get_error(read_future);
251 bool eos_reached = aws_future_bool_get_result(read_future);
252 /* Seek to offset larger than the length will not fail. */
253 ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, error);
254 ASSERT_TRUE(eos_reached);
255 ASSERT_UINT_EQUALS(0, read_buf.len);
256 aws_byte_buf_clean_up(&read_buf);
257 aws_future_bool_release(read_future);
258 }
259
260 remove(file_path);
261 struct aws_future_void *shutdown_future = aws_parallel_input_stream_get_shutdown_future(parallel_read_stream);
262 aws_parallel_input_stream_release(parallel_read_stream);
263 aws_future_void_wait(shutdown_future, SIZE_MAX);
264 aws_future_void_release(shutdown_future);
265 aws_event_loop_group_release(el_group);
266 aws_event_loop_group_release(reading_elg);
267 aws_s3_tester_clean_up(&tester);
268
269 return AWS_OP_SUCCESS;
270 }
271
272 TEST_CASE(parallel_read_stream_from_large_file_test) {
273 (void)ctx;
274 struct aws_s3_tester tester;
275 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &tester));
276 size_t file_length = MB_TO_BYTES(10);
277
278 const char *file_path = "s3_test_parallel_input_stream_read_large.txt"; /* unique name */
279 ASSERT_SUCCESS(s_create_read_file(file_path, file_length));
280
281 /* Create an event loop group for the parallel input stream */
282 struct aws_event_loop_group *reading_elg = aws_event_loop_group_new_default(allocator, 1, NULL);
283 ASSERT_NOT_NULL(reading_elg);
284
285 struct aws_event_loop_group *el_group = aws_event_loop_group_new_default(allocator, 0, NULL);
286 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_c_str(file_path);
287
288 struct aws_parallel_input_stream *parallel_read_stream =
289 aws_parallel_input_stream_new_from_file(allocator, path_cursor, reading_elg, false /*direct_io*/);
290 ASSERT_NOT_NULL(parallel_read_stream);
291
292 {
293 /* The whole file */
294 struct aws_byte_buf read_buf;
295 aws_byte_buf_init(&read_buf, allocator, file_length);
296 struct aws_byte_buf expected_read_buf;
297 aws_byte_buf_init(&expected_read_buf, allocator, file_length);
298 bool eos_reached = false;
299
300 ASSERT_SUCCESS(s_parallel_read_test_helper(
301 allocator, parallel_read_stream, &read_buf, el_group, 0, file_length, 8, &eos_reached));
302
303 /* Read the exact number of bytes will not reach to the EOS */
304 ASSERT_FALSE(eos_reached);
305 struct aws_input_stream *stream = aws_input_stream_new_from_file(allocator, file_path);
306 ASSERT_SUCCESS(aws_input_stream_read(stream, &expected_read_buf));
307
308 ASSERT_TRUE(aws_byte_buf_eq(&expected_read_buf, &read_buf));
309 aws_byte_buf_clean_up(&read_buf);
310 aws_byte_buf_clean_up(&expected_read_buf);
311 aws_input_stream_release(stream);
312 }
313
314 {
315 /* First string */
316 struct aws_byte_buf read_buf;
317 aws_byte_buf_init(&read_buf, allocator, file_length);
318 bool eos_reached = true;
319
320 ASSERT_SUCCESS(s_parallel_read_test_helper(
321 allocator, parallel_read_stream, &read_buf, el_group, 0, s_parallel_stream_test->len, 8, &eos_reached));
322
323 ASSERT_FALSE(eos_reached);
324 ASSERT_TRUE(aws_string_eq_byte_buf(s_parallel_stream_test, &read_buf));
325 aws_byte_buf_clean_up(&read_buf);
326 }
327
328 {
329 /* Second string */
330 struct aws_byte_buf read_buf;
331 aws_byte_buf_init(&read_buf, allocator, file_length);
332
333 bool eos_reached = true;
334 ASSERT_SUCCESS(s_parallel_read_test_helper(
335 allocator,
336 parallel_read_stream,
337 &read_buf,
338 el_group,
339 s_parallel_stream_test->len,
340 s_parallel_stream_test->len,
341 8,
342 &eos_reached));
343
344 ASSERT_FALSE(eos_reached);
345 ASSERT_TRUE(aws_string_eq_byte_buf(s_parallel_stream_test, &read_buf));
346 aws_byte_buf_clean_up(&read_buf);
347 }
348 remove(file_path);
349 aws_event_loop_group_release(el_group);
350 aws_event_loop_group_release(reading_elg);
351 struct aws_future_void *shutdown_future = aws_parallel_input_stream_get_shutdown_future(parallel_read_stream);
352 aws_parallel_input_stream_release(parallel_read_stream);
353 aws_future_void_wait(shutdown_future, SIZE_MAX);
354 aws_future_void_release(shutdown_future);
355 aws_s3_tester_clean_up(&tester);
356
357 return AWS_OP_SUCCESS;
358 }
359
360 /* Helper structure for part streaming tests */
361 struct part_streaming_test_fixture {
362 struct aws_s3_tester tester;
363 struct aws_event_loop_group *reading_elg;
364 struct aws_parallel_input_stream *parallel_read_stream;
365 struct aws_s3_buffer_pool *buffer_pool;
366 struct aws_s3_buffer_ticket *ticket;
367 const char *file_path;
368 };
369
370 /* Helper function to set up part streaming test fixture */
371 static int s_part_streaming_test_setup(
372 struct aws_allocator *allocator,
373 struct part_streaming_test_fixture *fixture,
374 const char *file_path,
375 size_t file_length,
376 size_t buffer_size) {
377
378 ASSERT_SUCCESS(aws_s3_tester_init(allocator, &fixture->tester));
379
380 fixture->file_path = file_path;
381 ASSERT_SUCCESS(s_create_read_file(file_path, file_length));
382
383 fixture->reading_elg = aws_event_loop_group_new_default(allocator, 1, NULL);
384 ASSERT_NOT_NULL(fixture->reading_elg);
385
386 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_c_str(file_path);
387 fixture->parallel_read_stream =
388 aws_parallel_input_stream_new_from_file(allocator, path_cursor, fixture->reading_elg, false /*direct_io*/);
389 ASSERT_NOT_NULL(fixture->parallel_read_stream);
390
391 fixture->buffer_pool = aws_s3_default_buffer_pool_new(
392 allocator, (struct aws_s3_buffer_pool_config){.part_size = buffer_size, .memory_limit = GB_TO_BYTES(1)});
393
394 struct aws_future_s3_buffer_ticket *future = aws_s3_default_buffer_pool_reserve(
395 fixture->buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = buffer_size});
396 ASSERT_NOT_NULL(future);
397 AWS_FATAL_ASSERT(aws_future_s3_buffer_ticket_is_done(future));
398 AWS_FATAL_ASSERT(aws_future_s3_buffer_ticket_get_error(future) == AWS_OP_SUCCESS);
399 fixture->ticket = aws_future_s3_buffer_ticket_get_result_by_move(future);
400 aws_future_s3_buffer_ticket_release(future);
401
402 return AWS_OP_SUCCESS;
403 }
404
405 /* Helper function to clean up part streaming test fixture */
406 static void s_part_streaming_test_cleanup(struct part_streaming_test_fixture *fixture) {
407 remove(fixture->file_path);
408 aws_event_loop_group_release(fixture->reading_elg);
409 struct aws_future_void *shutdown_future =
410 aws_parallel_input_stream_get_shutdown_future(fixture->parallel_read_stream);
411 aws_parallel_input_stream_release(fixture->parallel_read_stream);
412 aws_future_void_wait(shutdown_future, SIZE_MAX);
413 aws_future_void_release(shutdown_future);
414 aws_s3_buffer_ticket_release(fixture->ticket);
415 aws_s3_default_buffer_pool_destroy(fixture->buffer_pool);
416 aws_s3_tester_clean_up(&fixture->tester);
417 }
418
419 /* Helper function to validate stream content against file */
420 static int s_validate_stream_content(
421 struct aws_allocator *allocator,
422 struct aws_input_stream *stream,
423 const char *file_path,
424 size_t offset,
425 size_t expected_length) {
426
427 struct aws_byte_buf read_buf;
428 aws_byte_buf_init(&read_buf, allocator, expected_length);
429 /* Read entire file and compare with expected content */
430
431 while (read_buf.len < read_buf.capacity) {
432 struct aws_stream_status status;
433 ASSERT_SUCCESS(aws_input_stream_get_status(stream, &status));
434 ASSERT_TRUE(status.is_valid);
435 if (status.is_end_of_stream) {
436 break;
437 }
438 ASSERT_SUCCESS(aws_input_stream_read(stream, &read_buf));
439 }
440 ASSERT_UINT_EQUALS(expected_length, read_buf.len);
441
442 /* Compare with expected content from file */
443 struct aws_byte_buf expected_buf;
444 aws_byte_buf_init(&expected_buf, allocator, expected_length);
445 struct aws_input_stream *file_stream = aws_input_stream_new_from_file(allocator, file_path);
446 ASSERT_SUCCESS(aws_input_stream_seek(file_stream, offset, AWS_SSB_BEGIN));
447
448 while (expected_buf.len < expected_buf.capacity) {
449 struct aws_stream_status status;
450 ASSERT_SUCCESS(aws_input_stream_get_status(file_stream, &status));
451 ASSERT_TRUE(status.is_valid);
452 if (status.is_end_of_stream) {
453 break;
454 }
455 ASSERT_SUCCESS(aws_input_stream_read(file_stream, &expected_buf));
456 }
457 ASSERT_UINT_EQUALS(expected_length, expected_buf.len);
458
459 struct aws_byte_cursor expected_cursor = aws_byte_cursor_from_buf(&expected_buf);
460 struct aws_byte_cursor read_cursor = aws_byte_cursor_from_buf(&read_buf);
461 ASSERT_TRUE(aws_byte_cursor_eq(&expected_cursor, &read_cursor));
462
463 aws_byte_buf_clean_up(&read_buf);
464 aws_byte_buf_clean_up(&expected_buf);
465 aws_input_stream_release(file_stream);
466
467 return AWS_OP_SUCCESS;
468 }
469
470 /* Helper function to validate stream content against file */
471 static int s_validate_stream_content_with_chunks(
472 struct aws_allocator *allocator,
473 struct aws_input_stream *stream,
474 const char *file_path,
475 size_t offset,
476 size_t total_expected_length,
477 size_t chunk_size) {
478
479 struct aws_byte_buf read_buf;
480 aws_byte_buf_init(&read_buf, allocator, total_expected_length);
481 /* Read entire file and compare with expected content */
482 struct aws_byte_buf chunk_buf;
483 aws_byte_buf_init(&chunk_buf, allocator, chunk_size);
484 /* Read in chunks until we reach end of stream */
485 while (true) {
486 struct aws_stream_status status;
487 ASSERT_SUCCESS(aws_input_stream_get_status(stream, &status));
488 ASSERT_TRUE(status.is_valid);
489 if (status.is_end_of_stream) {
490 break;
491 }
492
493 aws_byte_buf_reset(&chunk_buf, 0);
494 ASSERT_SUCCESS(aws_input_stream_read(stream, &chunk_buf));
495
496 /* Append chunk to accumulated buffer */
497 struct aws_byte_cursor chunk_cursor = aws_byte_cursor_from_buf(&chunk_buf);
498 aws_byte_buf_append(&read_buf, &chunk_cursor);
499 }
500
501 aws_byte_buf_clean_up(&chunk_buf);
502 ASSERT_UINT_EQUALS(total_expected_length, read_buf.len);
503
504 /* Compare with expected content from file */
505 struct aws_byte_buf expected_buf;
506 aws_byte_buf_init(&expected_buf, allocator, total_expected_length);
507 struct aws_input_stream *file_stream = aws_input_stream_new_from_file(allocator, file_path);
508 ASSERT_SUCCESS(aws_input_stream_seek(file_stream, offset, AWS_SSB_BEGIN));
509 while (expected_buf.len < expected_buf.capacity) {
510 struct aws_stream_status status;
511 ASSERT_SUCCESS(aws_input_stream_get_status(file_stream, &status));
512 ASSERT_TRUE(status.is_valid);
513 if (status.is_end_of_stream) {
514 break;
515 }
516 ASSERT_SUCCESS(aws_input_stream_read(file_stream, &expected_buf));
517 }
518 ASSERT_UINT_EQUALS(total_expected_length, expected_buf.len);
519
520 struct aws_byte_cursor expected_cursor = aws_byte_cursor_from_buf(&expected_buf);
521 struct aws_byte_cursor read_cursor = aws_byte_cursor_from_buf(&read_buf);
522 ASSERT_TRUE(aws_byte_cursor_eq(&expected_cursor, &read_cursor));
523
524 aws_byte_buf_clean_up(&read_buf);
525 aws_byte_buf_clean_up(&expected_buf);
526 aws_input_stream_release(file_stream);
527
528 return AWS_OP_SUCCESS;
529 }
530
531 TEST_CASE(part_streaming_stream_from_large_file_test) {
532 (void)ctx;
533 struct part_streaming_test_fixture fixture;
534 size_t file_length = MB_TO_BYTES(100);
535
536 ASSERT_SUCCESS(s_part_streaming_test_setup(
537 allocator, &fixture, "s3_part_streaming_stream_read_large.txt", file_length, KB_TO_BYTES(64)));
538 /* Test reading from unaligned offset */
539 struct aws_input_stream *part_streaming_stream = aws_part_streaming_input_stream_new(
540 allocator, fixture.parallel_read_stream, fixture.ticket, 0, file_length, true);
541 ASSERT_NOT_NULL(part_streaming_stream);
542
543 /* Test initial status */
544 struct aws_stream_status status;
545 ASSERT_SUCCESS(aws_input_stream_get_status(part_streaming_stream, &status));
546 ASSERT_TRUE(status.is_valid);
547 ASSERT_FALSE(status.is_end_of_stream);
548
549 /* Validate content */
550 ASSERT_SUCCESS(s_validate_stream_content(allocator, part_streaming_stream, fixture.file_path, 0, file_length));
551
552 aws_input_stream_release(part_streaming_stream);
553 s_part_streaming_test_cleanup(&fixture);
554
555 return AWS_OP_SUCCESS;
556 }
557
558 TEST_CASE(part_streaming_stream_offset_test) {
559 (void)ctx;
560 struct part_streaming_test_fixture fixture;
561 size_t file_length = MB_TO_BYTES(5);
562 size_t offset = KB_TO_BYTES(100);
563 size_t read_size = KB_TO_BYTES(500);
564
565 ASSERT_SUCCESS(s_part_streaming_test_setup(
566 allocator, &fixture, "s3_part_streaming_stream_offset_test.txt", file_length, MB_TO_BYTES(4)));
567
568 /* Test reading from offset */
569 struct aws_input_stream *part_streaming_stream = aws_part_streaming_input_stream_new(
570 allocator, fixture.parallel_read_stream, fixture.ticket, offset, read_size, true);
571 ASSERT_NOT_NULL(part_streaming_stream);
572
573 /* Validate content */
574 ASSERT_SUCCESS(s_validate_stream_content(allocator, part_streaming_stream, fixture.file_path, offset, read_size));
575
576 aws_input_stream_release(part_streaming_stream);
577 s_part_streaming_test_cleanup(&fixture);
578 return AWS_OP_SUCCESS;
579 }
580
581 TEST_CASE(part_streaming_stream_chunked_read_test) {
582 (void)ctx;
583 size_t file_length = MB_TO_BYTES(3);
584 /* small chunks */
585 size_t chunk_size = KB_TO_BYTES(1);
586
587 struct part_streaming_test_fixture fixture;
588 ASSERT_SUCCESS(s_part_streaming_test_setup(
589 allocator, &fixture, "s3_part_streaming_stream_chunked_test.txt", file_length, MB_TO_BYTES(2)));
590
591 /* Test reading in small chunks */
592 struct aws_input_stream *part_streaming_stream = aws_part_streaming_input_stream_new(
593 allocator, fixture.parallel_read_stream, fixture.ticket, 0, file_length, true);
594 ASSERT_NOT_NULL(part_streaming_stream);
595
596 ASSERT_SUCCESS(s_validate_stream_content_with_chunks(
597 allocator, part_streaming_stream, fixture.file_path, 0, file_length, chunk_size));
598 aws_input_stream_release(part_streaming_stream);
599
600 s_part_streaming_test_cleanup(&fixture);
601 return AWS_OP_SUCCESS;
602 }
603
604 TEST_CASE(part_streaming_stream_unaligned_offset_test) {
605 (void)ctx;
606 struct part_streaming_test_fixture fixture;
607 size_t file_length = MB_TO_BYTES(10);
608 /* Use an offset that's not aligned to 4KB page boundary */
609 size_t offset = KB_TO_BYTES(4) + 1234; /* 4KB + 1234 bytes */
610 /* small chunks */
611 size_t chunk_size = KB_TO_BYTES(123);
612
613 ASSERT_SUCCESS(s_part_streaming_test_setup(
614 allocator, &fixture, "s3_part_streaming_stream_unaligned_test.txt", file_length, MB_TO_BYTES(1)));
615
616 /* Test reading from unaligned offset */
617 /* We can create the input stream with more than the file has. And it should still work. */
618 struct aws_input_stream *part_streaming_stream = aws_part_streaming_input_stream_new(
619 allocator, fixture.parallel_read_stream, fixture.ticket, offset, 2 * file_length, true);
620 ASSERT_NOT_NULL(part_streaming_stream);
621
622 ASSERT_SUCCESS(s_validate_stream_content_with_chunks(
623 allocator, part_streaming_stream, fixture.file_path, offset, file_length - offset, chunk_size));
624
625 aws_input_stream_release(part_streaming_stream);
626 s_part_streaming_test_cleanup(&fixture);
627
628 return AWS_OP_SUCCESS;
629 }
630
631 TEST_CASE(part_streaming_stream_small_buffer_test) {
632 (void)ctx;
633 struct part_streaming_test_fixture fixture;
634 size_t file_length = MB_TO_BYTES(10);
635
636 ASSERT_SUCCESS(s_part_streaming_test_setup(
637 allocator, &fixture, "s3_part_streaming_stream_small_buffer_test.txt", file_length, KB_TO_BYTES(1)));
638
639 /* Test that the buffer is smaller than the page size will fail the creating when alignment is required */
640 struct aws_input_stream *part_streaming_stream = aws_part_streaming_input_stream_new(
641 allocator, fixture.parallel_read_stream, fixture.ticket, 0, file_length, true);
642 ASSERT_NULL(part_streaming_stream);
643 ASSERT_UINT_EQUALS(AWS_ERROR_INVALID_ARGUMENT, aws_last_error());
644
645 /* But will still work without alignment */
646 part_streaming_stream = aws_part_streaming_input_stream_new(
647 allocator, fixture.parallel_read_stream, fixture.ticket, 0, file_length, false);
648 ASSERT_NOT_NULL(part_streaming_stream);
649 /* Validate content */
650 ASSERT_SUCCESS(s_validate_stream_content(allocator, part_streaming_stream, fixture.file_path, 0, file_length));
651
652 aws_input_stream_release(part_streaming_stream);
653 s_part_streaming_test_cleanup(&fixture);
654
655 return AWS_OP_SUCCESS;
656 }
657
658 TEST_CASE(part_streaming_stream_seek_unsupported_test) {
659 (void)ctx;
660 struct part_streaming_test_fixture fixture;
661 size_t file_length = KB_TO_BYTES(10);
662
663 ASSERT_SUCCESS(s_part_streaming_test_setup(
664 allocator, &fixture, "s3_part_streaming_stream_seek_test.txt", file_length, KB_TO_BYTES(32)));
665
666 /* Test that seek operation is not supported */
667 struct aws_input_stream *part_streaming_stream = aws_part_streaming_input_stream_new(
668 allocator, fixture.parallel_read_stream, fixture.ticket, 0, file_length, true);
669 ASSERT_NOT_NULL(part_streaming_stream);
670
671 /* Test seek operations - all should fail */
672 ASSERT_FAILS(aws_input_stream_seek(part_streaming_stream, 0, AWS_SSB_BEGIN));
673 ASSERT_UINT_EQUALS(AWS_ERROR_UNSUPPORTED_OPERATION, aws_last_error());
674
675 aws_reset_error();
676 ASSERT_FAILS(aws_input_stream_seek(part_streaming_stream, 100, AWS_SSB_BEGIN));
677 ASSERT_UINT_EQUALS(AWS_ERROR_UNSUPPORTED_OPERATION, aws_last_error());
678
679 aws_reset_error();
680 ASSERT_FAILS(aws_input_stream_seek(part_streaming_stream, 0, AWS_SSB_END));
681 ASSERT_UINT_EQUALS(AWS_ERROR_UNSUPPORTED_OPERATION, aws_last_error());
682
683 aws_input_stream_release(part_streaming_stream);
684 s_part_streaming_test_cleanup(&fixture);
685
686 return AWS_OP_SUCCESS;
687 }
688
689 TEST_CASE(part_streaming_stream_get_length_test) {
690 (void)ctx;
691 struct part_streaming_test_fixture fixture;
692 size_t file_length = KB_TO_BYTES(10);
693
694 ASSERT_SUCCESS(s_part_streaming_test_setup(
695 allocator, &fixture, "s3_part_streaming_stream_get_length_test.txt", file_length, KB_TO_BYTES(32)));
696
697 /* Test that get_length operation is now supported */
698 struct aws_input_stream *part_streaming_stream = aws_part_streaming_input_stream_new(
699 allocator, fixture.parallel_read_stream, fixture.ticket, 0, file_length, true);
700 ASSERT_NOT_NULL(part_streaming_stream);
701
702 int64_t length;
703 ASSERT_SUCCESS(aws_input_stream_get_length(part_streaming_stream, &length));
704 ASSERT_UINT_EQUALS(file_length, (size_t)length);
705
706 /* Test with a different length */
707 size_t custom_length = KB_TO_BYTES(5);
708 struct aws_input_stream *part_streaming_stream2 = aws_part_streaming_input_stream_new(
709 allocator, fixture.parallel_read_stream, fixture.ticket, 0, custom_length, true);
710 ASSERT_NOT_NULL(part_streaming_stream2);
711
712 ASSERT_SUCCESS(aws_input_stream_get_length(part_streaming_stream2, &length));
713 ASSERT_UINT_EQUALS(custom_length, (size_t)length);
714
715 /* Test with an offset */
716 size_t offset = KB_TO_BYTES(2);
717 struct aws_input_stream *part_streaming_stream3 = aws_part_streaming_input_stream_new(
718 allocator, fixture.parallel_read_stream, fixture.ticket, offset, custom_length, true);
719 ASSERT_NOT_NULL(part_streaming_stream3);
720
721 ASSERT_SUCCESS(aws_input_stream_get_length(part_streaming_stream3, &length));
722 ASSERT_UINT_EQUALS(custom_length, (size_t)length);
723
724 /* Test with an offset and passed-in length is larger than available */
725 struct aws_input_stream *part_streaming_stream4 = aws_part_streaming_input_stream_new(
726 allocator, fixture.parallel_read_stream, fixture.ticket, offset, file_length, true);
727 ASSERT_NOT_NULL(part_streaming_stream4);
728
729 ASSERT_SUCCESS(aws_input_stream_get_length(part_streaming_stream4, &length));
730 ASSERT_UINT_EQUALS(file_length - offset, (size_t)length);
731
732 /* Test with an offset is larger than available */
733 struct aws_input_stream *part_streaming_stream5 = aws_part_streaming_input_stream_new(
734 allocator, fixture.parallel_read_stream, fixture.ticket, file_length + offset, file_length, true);
735 ASSERT_NOT_NULL(part_streaming_stream5);
736
737 ASSERT_SUCCESS(aws_input_stream_get_length(part_streaming_stream5, &length));
738 ASSERT_UINT_EQUALS(0, (size_t)length);
739
740 aws_input_stream_release(part_streaming_stream);
741 aws_input_stream_release(part_streaming_stream2);
742 aws_input_stream_release(part_streaming_stream3);
743 aws_input_stream_release(part_streaming_stream4);
744 aws_input_stream_release(part_streaming_stream5);
745 s_part_streaming_test_cleanup(&fixture);
746
747 return AWS_OP_SUCCESS;
748 }
749
750 TEST_CASE(part_streaming_stream_file_deleted_during_read_test) {
751 (void)ctx;
752 struct part_streaming_test_fixture fixture;
753 size_t file_length = MB_TO_BYTES(1);
754
755 ASSERT_SUCCESS(s_part_streaming_test_setup(
756 allocator, &fixture, "s3_part_streaming_stream_delete_test.txt", file_length, KB_TO_BYTES(64)));
757
758 /* Create the part streaming stream */
759 struct aws_input_stream *part_streaming_stream = aws_part_streaming_input_stream_new(
760 allocator, fixture.parallel_read_stream, fixture.ticket, 0, file_length, true);
761 ASSERT_NOT_NULL(part_streaming_stream);
762
763 /* Delete the file while the stream is still active */
764 remove(fixture.file_path);
765 struct aws_byte_buf read_buf;
766 aws_byte_buf_init(&read_buf, allocator, file_length);
767
768 /* Keep reading until we get an error or reach end of stream */
769 int error_code = AWS_OP_SUCCESS;
770 while (read_buf.len < read_buf.capacity) {
771 struct aws_stream_status status;
772 ASSERT_SUCCESS(aws_input_stream_get_status(part_streaming_stream, &status));
773 ASSERT_TRUE(status.is_valid);
774
775 if (status.is_end_of_stream) {
776 break;
777 }
778
779 if (aws_input_stream_read(part_streaming_stream, &read_buf) != AWS_OP_SUCCESS) {
780 /* Error occurred - this is expected when file is deleted during read */
781 error_code = aws_last_error();
782 break;
783 }
784 }
785
786 ASSERT_TRUE(error_code != AWS_OP_SUCCESS);
787
788 aws_byte_buf_clean_up(&read_buf);
789 aws_input_stream_release(part_streaming_stream);
790
791 /* Clean up the rest of the fixture (file is already deleted) */
792 aws_event_loop_group_release(fixture.reading_elg);
793 struct aws_future_void *shutdown_future =
794 aws_parallel_input_stream_get_shutdown_future(fixture.parallel_read_stream);
795 aws_parallel_input_stream_release(fixture.parallel_read_stream);
796 aws_future_void_wait(shutdown_future, SIZE_MAX);
797 aws_future_void_release(shutdown_future);
798 aws_s3_buffer_ticket_release(fixture.ticket);
799 aws_s3_default_buffer_pool_destroy(fixture.buffer_pool);
800 aws_s3_tester_clean_up(&fixture.tester);
801
802 return AWS_OP_SUCCESS;
803 }
804
805 TEST_CASE(part_streaming_stream_zero_length_request_test) {
806 (void)ctx;
807 struct part_streaming_test_fixture fixture;
808 size_t file_length = KB_TO_BYTES(10);
809
810 ASSERT_SUCCESS(s_part_streaming_test_setup(
811 allocator, &fixture, "s3_part_streaming_stream_zero_length_test.txt", file_length, KB_TO_BYTES(32)));
812
813 /* Test with zero length request */
814 struct aws_input_stream *part_streaming_stream =
815 aws_part_streaming_input_stream_new(allocator, fixture.parallel_read_stream, fixture.ticket, 0, 0, true);
816 ASSERT_NOT_NULL(part_streaming_stream);
817
818 /* Check initial status - should be end of stream immediately */
819 struct aws_stream_status status;
820 ASSERT_SUCCESS(aws_input_stream_get_status(part_streaming_stream, &status));
821 ASSERT_TRUE(status.is_valid);
822 ASSERT_TRUE(status.is_end_of_stream);
823
824 /* Try to read - should return immediately with no data */
825 struct aws_byte_buf read_buf;
826 aws_byte_buf_init(&read_buf, allocator, KB_TO_BYTES(1));
827 ASSERT_SUCCESS(aws_input_stream_read(part_streaming_stream, &read_buf));
828 ASSERT_UINT_EQUALS(0, read_buf.len);
829
830 aws_byte_buf_clean_up(&read_buf);
831 aws_input_stream_release(part_streaming_stream);
832 s_part_streaming_test_cleanup(&fixture);
833
834 return AWS_OP_SUCCESS;
835 }
836
837 TEST_CASE(part_streaming_stream_large_offset_test) {
838 (void)ctx;
839 struct part_streaming_test_fixture fixture;
840 size_t file_length = KB_TO_BYTES(10);
841
842 ASSERT_SUCCESS(s_part_streaming_test_setup(
843 allocator, &fixture, "s3_part_streaming_stream_large_offset_test.txt", file_length, KB_TO_BYTES(32)));
844
845 /* Test with offset larger than file size */
846 uint64_t large_offset = file_length * 2;
847 struct aws_input_stream *part_streaming_stream = aws_part_streaming_input_stream_new(
848 allocator, fixture.parallel_read_stream, fixture.ticket, large_offset, file_length, true);
849 ASSERT_NOT_NULL(part_streaming_stream);
850
851 /* Try to read - should reach end of stream immediately */
852 struct aws_byte_buf read_buf;
853 aws_byte_buf_init(&read_buf, allocator, KB_TO_BYTES(1));
854 ASSERT_SUCCESS(aws_input_stream_read(part_streaming_stream, &read_buf));
855 ASSERT_UINT_EQUALS(0, read_buf.len);
856
857 /* Check status - should be end of stream */
858 struct aws_stream_status status;
859 ASSERT_SUCCESS(aws_input_stream_get_status(part_streaming_stream, &status));
860 ASSERT_TRUE(status.is_valid);
861 ASSERT_TRUE(status.is_end_of_stream);
862
863 aws_byte_buf_clean_up(&read_buf);
864 aws_input_stream_release(part_streaming_stream);
865 s_part_streaming_test_cleanup(&fixture);
866
867 return AWS_OP_SUCCESS;
868 }