| 1 |
#ifndef AWS_TESTING_ASYNC_STREAM_TESTER_H |
| 2 |
#define AWS_TESTING_ASYNC_STREAM_TESTER_H |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <aws/io/async_stream.h> |
| 9 |
|
| 10 |
#include <aws/common/byte_buf.h> |
| 11 |
#include <aws/common/condition_variable.h> |
| 12 |
#include <aws/common/mutex.h> |
| 13 |
#include <aws/common/thread.h> |
| 14 |
#include <aws/io/future.h> |
| 15 |
#include <aws/testing/stream_tester.h> |
| 16 |
|
| 17 |
#ifndef AWS_UNSTABLE_TESTING_API |
| 18 |
# error This code is designed for use by AWS owned libraries for the AWS C99 SDK. \ |
| 19 |
You are welcome to use it, but we make no promises on the stability of this API. \ |
| 20 |
To enable use of this code, set the AWS_UNSTABLE_TESTING_API compiler flag. |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
enum aws_async_read_completion_strategy { |
| 29 |
|
| 30 |
AWS_ASYNC_READ_COMPLETES_ON_ANOTHER_THREAD, |
| 31 |
|
| 32 |
AWS_ASYNC_READ_COMPLETES_IMMEDIATELY, |
| 33 |
|
| 34 |
AWS_ASYNC_READ_COMPLETES_ON_RANDOM_THREAD, |
| 35 |
}; |
| 36 |
|
| 37 |
struct aws_async_input_stream_tester_options { |
| 38 |
|
| 39 |
|
| 40 |
struct aws_input_stream_tester_options base; |
| 41 |
|
| 42 |
enum aws_async_read_completion_strategy completion_strategy; |
| 43 |
|
| 44 |
|
| 45 |
uint64_t read_duration_ns; |
| 46 |
}; |
| 47 |
|
| 48 |
struct aws_async_input_stream_tester { |
| 49 |
struct aws_async_input_stream base; |
| 50 |
struct aws_allocator *alloc; |
| 51 |
struct aws_async_input_stream_tester_options options; |
| 52 |
struct aws_input_stream *source_stream; |
| 53 |
|
| 54 |
struct aws_thread thread; |
| 55 |
struct { |
| 56 |
struct aws_mutex lock; |
| 57 |
struct aws_condition_variable cvar; |
| 58 |
|
| 59 |
|
| 60 |
struct aws_byte_buf *read_dest; |
| 61 |
struct aws_future_bool *read_future; |
| 62 |
|
| 63 |
|
| 64 |
bool do_shutdown; |
| 65 |
} synced_data; |
| 66 |
|
| 67 |
struct aws_atomic_var num_outstanding_reads; |
| 68 |
}; |
| 69 |
|
| 70 |
static inline void s_async_input_stream_tester_do_actual_read( |
| 71 |
struct aws_async_input_stream_tester *impl, |
| 72 |
struct aws_byte_buf *dest, |
| 73 |
struct aws_future_bool *read_future) { |
| 74 |
|
| 75 |
int error_code = 0; |
| 76 |
|
| 77 |
|
| 78 |
if (impl->options.read_duration_ns != 0) { |
| 79 |
aws_thread_current_sleep(impl->options.read_duration_ns); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
size_t prev_len = dest->len; |
| 86 |
struct aws_stream_status status = {.is_end_of_stream = false, .is_valid = true}; |
| 87 |
while ((dest->len == prev_len) && !status.is_end_of_stream) { |
| 88 |
|
| 89 |
if (aws_input_stream_read(impl->source_stream, dest) != AWS_OP_SUCCESS) { |
| 90 |
error_code = aws_last_error(); |
| 91 |
goto done; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
if (aws_input_stream_get_status(impl->source_stream, &status) != AWS_OP_SUCCESS) { |
| 96 |
error_code = aws_last_error(); |
| 97 |
goto done; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
done: |
| 102 |
aws_atomic_fetch_sub(&impl->num_outstanding_reads, 1); |
| 103 |
|
| 104 |
if (error_code != 0) { |
| 105 |
aws_future_bool_set_error(read_future, error_code); |
| 106 |
} else { |
| 107 |
aws_future_bool_set_result(read_future, status.is_end_of_stream); |
| 108 |
} |
| 109 |
|
| 110 |
aws_future_bool_release(read_future); |
| 111 |
} |
| 112 |
|
| 113 |
static inline struct aws_future_bool *s_async_input_stream_tester_read( |
| 114 |
struct aws_async_input_stream *stream, |
| 115 |
struct aws_byte_buf *dest) { |
| 116 |
|
| 117 |
struct aws_async_input_stream_tester *impl = (struct aws_async_input_stream_tester *)stream->impl; |
| 118 |
|
| 119 |
size_t prev_outstanding_reads = aws_atomic_fetch_add(&impl->num_outstanding_reads, 1); |
| 120 |
AWS_FATAL_ASSERT(prev_outstanding_reads == 0 && "Overlapping read() calls are forbidden"); |
| 121 |
|
| 122 |
struct aws_future_bool *read_future = aws_future_bool_new(stream->alloc); |
| 123 |
|
| 124 |
bool do_on_thread = false; |
| 125 |
switch (impl->options.completion_strategy) { |
| 126 |
case AWS_ASYNC_READ_COMPLETES_ON_ANOTHER_THREAD: |
| 127 |
do_on_thread = true; |
| 128 |
break; |
| 129 |
case AWS_ASYNC_READ_COMPLETES_IMMEDIATELY: |
| 130 |
do_on_thread = false; |
| 131 |
break; |
| 132 |
case AWS_ASYNC_READ_COMPLETES_ON_RANDOM_THREAD: |
| 133 |
do_on_thread = (rand() % 2 == 0); |
| 134 |
break; |
| 135 |
} |
| 136 |
|
| 137 |
if (do_on_thread) { |
| 138 |
|
| 139 |
aws_mutex_lock(&impl->synced_data.lock); |
| 140 |
impl->synced_data.read_dest = dest; |
| 141 |
impl->synced_data.read_future = aws_future_bool_acquire(read_future); |
| 142 |
AWS_FATAL_ASSERT(aws_condition_variable_notify_all(&impl->synced_data.cvar) == AWS_OP_SUCCESS); |
| 143 |
aws_mutex_unlock(&impl->synced_data.lock); |
| 144 |
|
| 145 |
} else { |
| 146 |
|
| 147 |
aws_future_bool_acquire(read_future); |
| 148 |
s_async_input_stream_tester_do_actual_read(impl, dest, read_future); |
| 149 |
} |
| 150 |
|
| 151 |
return read_future; |
| 152 |
} |
| 153 |
|
| 154 |
static inline void s_async_input_stream_tester_do_actual_destroy(struct aws_async_input_stream_tester *impl) { |
| 155 |
if (impl->options.completion_strategy != AWS_ASYNC_READ_COMPLETES_IMMEDIATELY) { |
| 156 |
aws_condition_variable_clean_up(&impl->synced_data.cvar); |
| 157 |
aws_mutex_clean_up(&impl->synced_data.lock); |
| 158 |
} |
| 159 |
|
| 160 |
aws_input_stream_release(impl->source_stream); |
| 161 |
aws_mem_release(impl->base.alloc, impl); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
static inline void s_async_input_stream_tester_destroy(struct aws_async_input_stream *async_stream) { |
| 166 |
struct aws_async_input_stream_tester *impl = (struct aws_async_input_stream_tester *)async_stream->impl; |
| 167 |
|
| 168 |
if (impl->options.completion_strategy == AWS_ASYNC_READ_COMPLETES_IMMEDIATELY) { |
| 169 |
s_async_input_stream_tester_do_actual_destroy(impl); |
| 170 |
} else { |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
aws_mutex_lock(&impl->synced_data.lock); |
| 175 |
impl->synced_data.do_shutdown = true; |
| 176 |
AWS_FATAL_ASSERT(aws_condition_variable_notify_all(&impl->synced_data.cvar) == AWS_OP_SUCCESS); |
| 177 |
aws_mutex_unlock(&impl->synced_data.lock); |
| 178 |
|
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
static inline bool s_async_input_stream_tester_thread_pred(void *arg) { |
| 183 |
struct aws_async_input_stream_tester *impl = (struct aws_async_input_stream_tester *)arg; |
| 184 |
return impl->synced_data.do_shutdown || (impl->synced_data.read_dest != NULL); |
| 185 |
} |
| 186 |
|
| 187 |
static inline void s_async_input_stream_tester_thread(void *arg) { |
| 188 |
struct aws_async_input_stream_tester *impl = (struct aws_async_input_stream_tester *)arg; |
| 189 |
bool do_shutdown = false; |
| 190 |
struct aws_byte_buf *read_dest = NULL; |
| 191 |
struct aws_future_bool *read_future = NULL; |
| 192 |
while (!do_shutdown) { |
| 193 |
|
| 194 |
aws_mutex_lock(&impl->synced_data.lock); |
| 195 |
AWS_FATAL_ASSERT( |
| 196 |
aws_condition_variable_wait_pred( |
| 197 |
&impl->synced_data.cvar, &impl->synced_data.lock, s_async_input_stream_tester_thread_pred, impl) == |
| 198 |
AWS_OP_SUCCESS); |
| 199 |
|
| 200 |
|
| 201 |
do_shutdown = impl->synced_data.do_shutdown; |
| 202 |
read_dest = impl->synced_data.read_dest; |
| 203 |
impl->synced_data.read_dest = NULL; |
| 204 |
read_future = impl->synced_data.read_future; |
| 205 |
impl->synced_data.read_future = NULL; |
| 206 |
|
| 207 |
aws_mutex_unlock(&impl->synced_data.lock); |
| 208 |
|
| 209 |
|
| 210 |
if (read_dest != NULL) { |
| 211 |
s_async_input_stream_tester_do_actual_read(impl, read_dest, read_future); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
s_async_input_stream_tester_do_actual_destroy(impl); |
| 217 |
} |
| 218 |
|
| 219 |
static inline uint64_t aws_async_input_stream_tester_total_bytes_read( |
| 220 |
const struct aws_async_input_stream *async_stream) { |
| 221 |
|
| 222 |
const struct aws_async_input_stream_tester *async_impl = |
| 223 |
(const struct aws_async_input_stream_tester *)async_stream->impl; |
| 224 |
const struct aws_input_stream_tester *synchronous_impl = |
| 225 |
(const struct aws_input_stream_tester *)async_impl->source_stream->impl; |
| 226 |
return synchronous_impl->total_bytes_read; |
| 227 |
} |
| 228 |
|
| 229 |
static struct aws_async_input_stream_vtable s_async_input_stream_tester_vtable = { |
| 230 |
.destroy = s_async_input_stream_tester_destroy, |
| 231 |
.read = s_async_input_stream_tester_read, |
| 232 |
}; |
| 233 |
|
| 234 |
static inline struct aws_async_input_stream *aws_async_input_stream_new_tester( |
| 235 |
struct aws_allocator *alloc, |
| 236 |
const struct aws_async_input_stream_tester_options *options) { |
| 237 |
|
| 238 |
struct aws_async_input_stream_tester *impl = |
| 239 |
(struct aws_async_input_stream_tester *)aws_mem_calloc(alloc, 1, sizeof(struct aws_async_input_stream_tester)); |
| 240 |
aws_async_input_stream_init_base(&impl->base, alloc, &s_async_input_stream_tester_vtable, impl); |
| 241 |
impl->options = *options; |
| 242 |
aws_atomic_init_int(&impl->num_outstanding_reads, 0); |
| 243 |
|
| 244 |
impl->source_stream = aws_input_stream_new_tester(alloc, &options->base); |
| 245 |
AWS_FATAL_ASSERT(impl->source_stream); |
| 246 |
|
| 247 |
if (options->completion_strategy != AWS_ASYNC_READ_COMPLETES_IMMEDIATELY) { |
| 248 |
aws_mutex_init(&impl->synced_data.lock); |
| 249 |
aws_condition_variable_init(&impl->synced_data.cvar); |
| 250 |
|
| 251 |
AWS_FATAL_ASSERT(aws_thread_init(&impl->thread, alloc) == AWS_OP_SUCCESS); |
| 252 |
struct aws_thread_options thread_options = *aws_default_thread_options(); |
| 253 |
thread_options.name = aws_byte_cursor_from_c_str("AsyncStream"); |
| 254 |
thread_options.join_strategy = AWS_TJS_MANAGED; |
| 255 |
|
| 256 |
AWS_FATAL_ASSERT( |
| 257 |
aws_thread_launch(&impl->thread, s_async_input_stream_tester_thread, impl, &thread_options) == |
| 258 |
AWS_OP_SUCCESS); |
| 259 |
} |
| 260 |
|
| 261 |
return &impl->base; |
| 262 |
} |
| 263 |
|
| 264 |
#endif |