| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/io/async_stream.h> |
| 6 |
|
| 7 |
#include <aws/common/clock.h> |
| 8 |
#include <aws/io/future.h> |
| 9 |
#include <aws/testing/async_stream_tester.h> |
| 10 |
#include <aws/testing/aws_test_harness.h> |
| 11 |
#include <aws/testing/stream_tester.h> |
| 12 |
|
| 13 |
#define ONE_SEC_IN_NS ((uint64_t)AWS_TIMESTAMP_NANOS) |
| 14 |
#define MAX_TIMEOUT_NS (10 * ONE_SEC_IN_NS) |
| 15 |
|
| 16 |
|
| 17 |
static int s_test_async_input_stream_read_to_fill( |
| 18 |
struct aws_allocator *alloc, |
| 19 |
struct aws_async_input_stream_tester_options *options) { |
| 20 |
|
| 21 |
aws_io_library_init(alloc); |
| 22 |
|
| 23 |
options->base.source_bytes = aws_byte_cursor_from_c_str("123456789"); |
| 24 |
struct aws_async_input_stream *async_stream = aws_async_input_stream_new_tester(alloc, options); |
| 25 |
|
| 26 |
|
| 27 |
struct aws_byte_buf buf; |
| 28 |
aws_byte_buf_init(&buf, alloc, 5); |
| 29 |
|
| 30 |
struct aws_future_bool *read_future = aws_async_input_stream_read_to_fill(async_stream, &buf); |
| 31 |
ASSERT_TRUE(aws_future_bool_wait(read_future, MAX_TIMEOUT_NS)); |
| 32 |
ASSERT_INT_EQUALS(0, aws_future_bool_get_error(read_future)); |
| 33 |
ASSERT_BIN_ARRAYS_EQUALS("12345", 5, buf.buffer, buf.len); |
| 34 |
bool eof = aws_future_bool_get_result(read_future); |
| 35 |
ASSERT_FALSE(eof); |
| 36 |
aws_future_bool_release(read_future); |
| 37 |
|
| 38 |
|
| 39 |
buf.len = 0; |
| 40 |
read_future = aws_async_input_stream_read_to_fill(async_stream, &buf); |
| 41 |
ASSERT_TRUE(aws_future_bool_wait(read_future, MAX_TIMEOUT_NS)); |
| 42 |
ASSERT_INT_EQUALS(0, aws_future_bool_get_error(read_future)); |
| 43 |
ASSERT_BIN_ARRAYS_EQUALS("6789", 4, buf.buffer, buf.len); |
| 44 |
eof = aws_future_bool_get_result(read_future); |
| 45 |
ASSERT_TRUE(eof); |
| 46 |
aws_future_bool_release(read_future); |
| 47 |
|
| 48 |
|
| 49 |
aws_byte_buf_clean_up(&buf); |
| 50 |
aws_async_input_stream_release(async_stream); |
| 51 |
aws_io_library_clean_up(); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
AWS_TEST_CASE(async_input_stream_fill_completes_on_thread, s_test_async_input_stream_fill_completes_on_thread) |
| 58 |
static int s_test_async_input_stream_fill_completes_on_thread(struct aws_allocator *alloc, void *ctx) { |
| 59 |
(void)ctx; |
| 60 |
struct aws_async_input_stream_tester_options options = { |
| 61 |
.completion_strategy = AWS_ASYNC_READ_COMPLETES_ON_ANOTHER_THREAD, |
| 62 |
.base = {.max_bytes_per_read = 1}, |
| 63 |
}; |
| 64 |
return s_test_async_input_stream_read_to_fill(alloc, &options); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
AWS_TEST_CASE(async_input_stream_fill_completes_immediately, s_test_async_input_stream_fill_completes_immediately) |
| 70 |
static int s_test_async_input_stream_fill_completes_immediately(struct aws_allocator *alloc, void *ctx) { |
| 71 |
(void)ctx; |
| 72 |
struct aws_async_input_stream_tester_options options = { |
| 73 |
.completion_strategy = AWS_ASYNC_READ_COMPLETES_IMMEDIATELY, |
| 74 |
.base = {.max_bytes_per_read = 1}, |
| 75 |
}; |
| 76 |
return s_test_async_input_stream_read_to_fill(alloc, &options); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
AWS_TEST_CASE(async_input_stream_fill_completes_randomly, s_test_async_input_stream_fill_completes_randomly) |
| 82 |
static int s_test_async_input_stream_fill_completes_randomly(struct aws_allocator *alloc, void *ctx) { |
| 83 |
(void)ctx; |
| 84 |
struct aws_async_input_stream_tester_options options = { |
| 85 |
.completion_strategy = AWS_ASYNC_READ_COMPLETES_ON_RANDOM_THREAD, |
| 86 |
.base = {.max_bytes_per_read = 1}, |
| 87 |
}; |
| 88 |
return s_test_async_input_stream_read_to_fill(alloc, &options); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
AWS_TEST_CASE(async_input_stream_fill_eof_requires_extra_read, s_test_async_input_stream_fill_eof_requires_extra_read) |
| 94 |
static int s_test_async_input_stream_fill_eof_requires_extra_read(struct aws_allocator *alloc, void *ctx) { |
| 95 |
(void)ctx; |
| 96 |
aws_io_library_init(alloc); |
| 97 |
|
| 98 |
struct aws_async_input_stream_tester_options options = { |
| 99 |
.base = |
| 100 |
{ |
| 101 |
.source_bytes = aws_byte_cursor_from_c_str("123456789"), |
| 102 |
.eof_requires_extra_read = true, |
| 103 |
}, |
| 104 |
}; |
| 105 |
struct aws_async_input_stream *async_stream = aws_async_input_stream_new_tester(alloc, &options); |
| 106 |
|
| 107 |
|
| 108 |
struct aws_byte_buf buf; |
| 109 |
aws_byte_buf_init(&buf, alloc, 9); |
| 110 |
|
| 111 |
struct aws_future_bool *read_future = aws_async_input_stream_read_to_fill(async_stream, &buf); |
| 112 |
ASSERT_TRUE(aws_future_bool_wait(read_future, MAX_TIMEOUT_NS)); |
| 113 |
ASSERT_INT_EQUALS(0, aws_future_bool_get_error(read_future)); |
| 114 |
ASSERT_BIN_ARRAYS_EQUALS("123456789", 9, buf.buffer, buf.len); |
| 115 |
bool eof = aws_future_bool_get_result(read_future); |
| 116 |
ASSERT_FALSE(eof); |
| 117 |
aws_future_bool_release(read_future); |
| 118 |
|
| 119 |
|
| 120 |
buf.len = 0; |
| 121 |
read_future = aws_async_input_stream_read_to_fill(async_stream, &buf); |
| 122 |
ASSERT_TRUE(aws_future_bool_wait(read_future, MAX_TIMEOUT_NS)); |
| 123 |
ASSERT_INT_EQUALS(0, aws_future_bool_get_error(read_future)); |
| 124 |
ASSERT_UINT_EQUALS(0, buf.len); |
| 125 |
eof = aws_future_bool_get_result(read_future); |
| 126 |
ASSERT_TRUE(eof); |
| 127 |
aws_future_bool_release(read_future); |
| 128 |
|
| 129 |
|
| 130 |
aws_byte_buf_clean_up(&buf); |
| 131 |
aws_async_input_stream_release(async_stream); |
| 132 |
aws_io_library_clean_up(); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
AWS_TEST_CASE(async_input_stream_fill_reports_error, s_test_async_input_stream_fill_reports_error) |
| 139 |
static int s_test_async_input_stream_fill_reports_error(struct aws_allocator *alloc, void *ctx) { |
| 140 |
(void)ctx; |
| 141 |
aws_io_library_init(alloc); |
| 142 |
|
| 143 |
struct aws_async_input_stream_tester_options options = { |
| 144 |
.base = |
| 145 |
{ |
| 146 |
.source_bytes = aws_byte_cursor_from_c_str("123456789"), |
| 147 |
.max_bytes_per_read = 1, |
| 148 |
.fail_on_nth_read = 2, |
| 149 |
.fail_with_error_code = 999, |
| 150 |
}, |
| 151 |
}; |
| 152 |
struct aws_async_input_stream *async_stream = aws_async_input_stream_new_tester(alloc, &options); |
| 153 |
|
| 154 |
|
| 155 |
struct aws_byte_buf buf; |
| 156 |
aws_byte_buf_init(&buf, alloc, 512); |
| 157 |
|
| 158 |
struct aws_future_bool *read_future = aws_async_input_stream_read_to_fill(async_stream, &buf); |
| 159 |
ASSERT_TRUE(aws_future_bool_wait(read_future, MAX_TIMEOUT_NS)); |
| 160 |
ASSERT_INT_EQUALS(999, aws_future_bool_get_error(read_future)); |
| 161 |
aws_future_bool_release(read_future); |
| 162 |
|
| 163 |
|
| 164 |
aws_byte_buf_clean_up(&buf); |
| 165 |
aws_async_input_stream_release(async_stream); |
| 166 |
aws_io_library_clean_up(); |
| 167 |
return 0; |
| 168 |
} |