Debian Codegraph @

crt/aws-c-io/include/aws/testing/async_stream_tester.h

raw symbols SCIP
1 #ifndef AWS_TESTING_ASYNC_STREAM_TESTER_H
2 #define AWS_TESTING_ASYNC_STREAM_TESTER_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
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 * Use aws_async_input_stream_tester to test edge cases in systems that take async streams.
25 * You can customize its behavior (e.g. fail on 3rd read, always complete async, always complete synchronously, etc)
26 */
27
28 enum aws_async_read_completion_strategy {
29 /* the tester has its own thread, and reads always complete from there */
30 AWS_ASYNC_READ_COMPLETES_ON_ANOTHER_THREAD,
31 /* reads complete before read() even returns */
32 AWS_ASYNC_READ_COMPLETES_IMMEDIATELY,
33 /* sometimes reads complete immediately, sometimes they complete on another thread */
34 AWS_ASYNC_READ_COMPLETES_ON_RANDOM_THREAD,
35 };
36
37 struct aws_async_input_stream_tester_options {
38 /* the async tester uses the synchronous tester under the hood,
39 * so here are those options */
40 struct aws_input_stream_tester_options base;
41
42 enum aws_async_read_completion_strategy completion_strategy;
43
44 /* if non-zero, a read will take at least this long to complete */
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 /* when thread should perform a read, these are set */
60 struct aws_byte_buf *read_dest;
61 struct aws_future_bool *read_future;
62
63 /* if true, thread should shut down */
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 /* delay, if that's how we're configured */
78 if (impl->options.read_duration_ns != 0) {
79 aws_thread_current_sleep(impl->options.read_duration_ns);
80 }
81
82 /* Keep calling read() until we get some data, or hit EOF.
83 * We do this because the synchronous aws_input_stream API allows
84 * 0 byte reads, but the aws_async_input_stream API does not. */
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 /* read from stream */
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 /* check if stream is done */
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 /* BEGIN CRITICAL SECTION */
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 /* END CRITICAL SECTION */
145 } else {
146 /* acquire additional refcount on future, since we call release once it's complete */
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 /* refcount has reached zero */
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 /* signal thread to finish cleaning things up */
172
173 /* BEGIN CRITICAL SECTION */
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 /* END CRITICAL SECTION */
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 /* BEGIN CRITICAL SECTION */
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 /* acquire work */
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 /* END CRITICAL SECTION */
209
210 if (read_dest != NULL) {
211 s_async_input_stream_tester_do_actual_read(impl, read_dest, read_future);
212 }
213 }
214
215 /* thread has shut down, finish destruction */
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 /* AWS_TESTING_ASYNC_STREAM_TESTER_H */