| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
| 5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/cppalliance/capy
|
7 |
|
// Official repository: https://github.com/cppalliance/capy
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_CAPY_TEST_BUFFER_SINK_HPP
|
10 |
|
#ifndef BOOST_CAPY_TEST_BUFFER_SINK_HPP
|
| 11 |
|
#define BOOST_CAPY_TEST_BUFFER_SINK_HPP
|
11 |
|
#define BOOST_CAPY_TEST_BUFFER_SINK_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/capy/detail/config.hpp>
|
13 |
|
#include <boost/capy/detail/config.hpp>
|
| 14 |
|
#include <boost/capy/buffers.hpp>
|
14 |
|
#include <boost/capy/buffers.hpp>
|
| 15 |
|
#include <boost/capy/buffers/make_buffer.hpp>
|
15 |
|
#include <boost/capy/buffers/make_buffer.hpp>
|
| 16 |
|
#include <boost/capy/coro.hpp>
|
16 |
|
#include <boost/capy/coro.hpp>
|
| 17 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
17 |
|
#include <boost/capy/ex/executor_ref.hpp>
|
| 18 |
|
#include <boost/capy/io_result.hpp>
|
18 |
|
#include <boost/capy/io_result.hpp>
|
| 19 |
|
#include <boost/capy/test/fuse.hpp>
|
19 |
|
#include <boost/capy/test/fuse.hpp>
|
| 20 |
|
|
20 |
|
|
| 21 |
|
|
21 |
|
|
| 22 |
|
|
22 |
|
|
| 23 |
|
|
23 |
|
|
| 24 |
|
|
24 |
|
|
| 25 |
|
|
25 |
|
|
| 26 |
|
|
26 |
|
|
| 27 |
|
|
27 |
|
|
| 28 |
|
|
28 |
|
|
| 29 |
|
|
29 |
|
|
| 30 |
|
|
30 |
|
|
| 31 |
|
/** A mock buffer sink for testing callee-owns-buffers write operations.
|
31 |
|
/** A mock buffer sink for testing callee-owns-buffers write operations.
|
| 32 |
|
|
32 |
|
|
| 33 |
|
Use this to verify code that writes data using the callee-owns-buffers
|
33 |
|
Use this to verify code that writes data using the callee-owns-buffers
|
| 34 |
|
pattern without needing real I/O. Call @ref prepare to get writable
|
34 |
|
pattern without needing real I/O. Call @ref prepare to get writable
|
| 35 |
|
buffers, write into them, then call @ref commit to finalize. The
|
35 |
|
buffers, write into them, then call @ref commit to finalize. The
|
| 36 |
|
associated @ref fuse enables error injection at controlled points.
|
36 |
|
associated @ref fuse enables error injection at controlled points.
|
| 37 |
|
|
37 |
|
|
| 38 |
|
This class satisfies the @ref BufferSink concept by providing
|
38 |
|
This class satisfies the @ref BufferSink concept by providing
|
| 39 |
|
internal storage that callers write into directly.
|
39 |
|
internal storage that callers write into directly.
|
| 40 |
|
|
40 |
|
|
| 41 |
|
|
41 |
|
|
| 42 |
|
|
42 |
|
|
| 43 |
|
|
43 |
|
|
| 44 |
|
|
44 |
|
|
| 45 |
|
|
45 |
|
|
| 46 |
|
|
46 |
|
|
| 47 |
|
|
47 |
|
|
| 48 |
|
|
48 |
|
|
| 49 |
|
auto r = f.armed( [&]( fuse& ) -> task<void> {
|
49 |
|
auto r = f.armed( [&]( fuse& ) -> task<void> {
|
| 50 |
|
|
50 |
|
|
| 51 |
|
std::size_t count = bs.prepare( arr, 16 );
|
51 |
|
std::size_t count = bs.prepare( arr, 16 );
|
| 52 |
|
|
52 |
|
|
| 53 |
|
|
53 |
|
|
| 54 |
|
|
54 |
|
|
| 55 |
|
// Write data into arr[0]
|
55 |
|
// Write data into arr[0]
|
| 56 |
|
std::memcpy( arr[0].data(), "Hello", 5 );
|
56 |
|
std::memcpy( arr[0].data(), "Hello", 5 );
|
| 57 |
|
|
57 |
|
|
| 58 |
|
auto [ec] = co_await bs.commit( 5 );
|
58 |
|
auto [ec] = co_await bs.commit( 5 );
|
| 59 |
|
|
59 |
|
|
| 60 |
|
|
60 |
|
|
| 61 |
|
|
61 |
|
|
| 62 |
|
auto [ec2] = co_await bs.commit_eof();
|
62 |
|
auto [ec2] = co_await bs.commit_eof();
|
| 63 |
|
// bs.data() returns "Hello"
|
63 |
|
// bs.data() returns "Hello"
|
| 64 |
|
|
64 |
|
|
| 65 |
|
|
65 |
|
|
| 66 |
|
|
66 |
|
|
| 67 |
|
|
67 |
|
|
| 68 |
|
|
68 |
|
|
| 69 |
|
|
69 |
|
|
| 70 |
|
|
70 |
|
|
| 71 |
|
|
71 |
|
|
| 72 |
|
|
72 |
|
|
| 73 |
|
std::string prepare_buf_;
|
73 |
|
std::string prepare_buf_;
|
| 74 |
|
std::size_t prepare_size_ = 0;
|
74 |
|
std::size_t prepare_size_ = 0;
|
| 75 |
|
std::size_t max_prepare_size_;
|
75 |
|
std::size_t max_prepare_size_;
|
| 76 |
|
bool eof_called_ = false;
|
76 |
|
bool eof_called_ = false;
|
| 77 |
|
|
77 |
|
|
| 78 |
|
|
78 |
|
|
| 79 |
|
/** Construct a buffer sink.
|
79 |
|
/** Construct a buffer sink.
|
| 80 |
|
|
80 |
|
|
| 81 |
|
@param f The fuse used to inject errors during commits.
|
81 |
|
@param f The fuse used to inject errors during commits.
|
| 82 |
|
|
82 |
|
|
| 83 |
|
@param max_prepare_size Maximum bytes available per prepare.
|
83 |
|
@param max_prepare_size Maximum bytes available per prepare.
|
| 84 |
|
Use to simulate limited buffer space.
|
84 |
|
Use to simulate limited buffer space.
|
| 85 |
|
|
85 |
|
|
| 86 |
|
|
86 |
|
|
| 87 |
|
|
87 |
|
|
| 88 |
|
std::size_t max_prepare_size = 4096) noexcept
|
88 |
|
std::size_t max_prepare_size = 4096) noexcept
|
| 89 |
|
|
89 |
|
|
| 90 |
|
, max_prepare_size_(max_prepare_size)
|
90 |
|
, max_prepare_size_(max_prepare_size)
|
| 91 |
|
|
91 |
|
|
| 92 |
|
prepare_buf_.resize(max_prepare_size_);
|
92 |
|
prepare_buf_.resize(max_prepare_size_);
|
| 93 |
|
|
93 |
|
|
| 94 |
|
|
94 |
|
|
| 95 |
|
/// Return the written data as a string view.
|
95 |
|
/// Return the written data as a string view.
|
| 96 |
|
|
96 |
|
|
| 97 |
|
|
97 |
|
|
| 98 |
|
|
98 |
|
|
| 99 |
|
|
99 |
|
|
| 100 |
|
|
100 |
|
|
| 101 |
|
|
101 |
|
|
| 102 |
|
/// Return the number of bytes written.
|
102 |
|
/// Return the number of bytes written.
|
| 103 |
|
|
103 |
|
|
| 104 |
|
|
104 |
|
|
| 105 |
|
|
105 |
|
|
| 106 |
|
|
106 |
|
|
| 107 |
|
|
107 |
|
|
| 108 |
|
|
108 |
|
|
| 109 |
|
/// Return whether commit_eof has been called.
|
109 |
|
/// Return whether commit_eof has been called.
|
| 110 |
|
|
110 |
|
|
| 111 |
|
eof_called() const noexcept
|
111 |
|
eof_called() const noexcept
|
| 112 |
|
|
112 |
|
|
| 113 |
|
|
113 |
|
|
| 114 |
|
|
114 |
|
|
| 115 |
|
|
115 |
|
|
| 116 |
|
/// Clear all data and reset state.
|
116 |
|
/// Clear all data and reset state.
|
| 117 |
|
|
117 |
|
|
| 118 |
|
|
118 |
|
|
| 119 |
|
|
119 |
|
|
| 120 |
|
|
120 |
|
|
| 121 |
|
|
121 |
|
|
| 122 |
|
|
122 |
|
|
| 123 |
|
|
123 |
|
|
| 124 |
|
|
124 |
|
|
| 125 |
|
/** Prepare writable buffers.
|
125 |
|
/** Prepare writable buffers.
|
| 126 |
|
|
126 |
|
|
| 127 |
|
Fills the provided span with mutable buffer descriptors pointing
|
127 |
|
Fills the provided span with mutable buffer descriptors pointing
|
| 128 |
|
to internal storage. The caller writes data into these buffers,
|
128 |
|
to internal storage. The caller writes data into these buffers,
|
| 129 |
|
then calls @ref commit to finalize.
|
129 |
|
then calls @ref commit to finalize.
|
| 130 |
|
|
130 |
|
|
| 131 |
|
@param dest Span of mutable_buffer to fill.
|
131 |
|
@param dest Span of mutable_buffer to fill.
|
| 132 |
|
|
132 |
|
|
| 133 |
|
@return A span of filled buffers (empty or 1 buffer in this implementation).
|
133 |
|
@return A span of filled buffers (empty or 1 buffer in this implementation).
|
| 134 |
|
|
134 |
|
|
| 135 |
|
std::span<mutable_buffer>
|
135 |
|
std::span<mutable_buffer>
|
| 136 |
|
prepare(std::span<mutable_buffer> dest)
|
136 |
|
prepare(std::span<mutable_buffer> dest)
|
| 137 |
|
|
137 |
|
|
| 138 |
|
|
138 |
|
|
| 139 |
|
|
139 |
|
|
| 140 |
|
|
140 |
|
|
| 141 |
|
prepare_size_ = max_prepare_size_;
|
141 |
|
prepare_size_ = max_prepare_size_;
|
| 142 |
|
dest[0] = make_buffer(prepare_buf_.data(), prepare_size_);
|
142 |
|
dest[0] = make_buffer(prepare_buf_.data(), prepare_size_);
|
| 143 |
|
|
143 |
|
|
| 144 |
|
|
144 |
|
|
| 145 |
|
|
145 |
|
|
| 146 |
|
/** Commit bytes written to the prepared buffers.
|
146 |
|
/** Commit bytes written to the prepared buffers.
|
| 147 |
|
|
147 |
|
|
| 148 |
|
Transfers `n` bytes from the prepared buffer to the internal
|
148 |
|
Transfers `n` bytes from the prepared buffer to the internal
|
| 149 |
|
data buffer. Before committing, the attached @ref fuse is
|
149 |
|
data buffer. Before committing, the attached @ref fuse is
|
| 150 |
|
consulted to possibly inject an error for testing fault scenarios.
|
150 |
|
consulted to possibly inject an error for testing fault scenarios.
|
| 151 |
|
|
151 |
|
|
| 152 |
|
@param n The number of bytes to commit.
|
152 |
|
@param n The number of bytes to commit.
|
| 153 |
|
|
153 |
|
|
| 154 |
|
@return An awaitable yielding `(error_code)`.
|
154 |
|
@return An awaitable yielding `(error_code)`.
|
| 155 |
|
|
155 |
|
|
| 156 |
|
|
156 |
|
|
| 157 |
|
|
157 |
|
|
| 158 |
|
|
158 |
|
|
| 159 |
|
|
159 |
|
|
| 160 |
|
|
160 |
|
|
| 161 |
|
|
161 |
|
|
| 162 |
|
|
162 |
|
|
| 163 |
|
|
163 |
|
|
| 164 |
|
|
164 |
|
|
| 165 |
|
|
165 |
|
|
| 166 |
|
bool await_ready() const noexcept { return true; }
|
166 |
|
bool await_ready() const noexcept { return true; }
|
| 167 |
|
|
167 |
|
|
|
|
|
168 |
+ |
// This method is required to satisfy Capy's IoAwaitable concept,
|
|
|
|
169 |
+ |
// but is never called because await_ready() returns true.
|
|
|
|
170 |
+ |
|
|
|
|
171 |
+ |
// Capy uses a two-layer awaitable system: the promise's
|
|
|
|
172 |
+ |
// await_transform wraps awaitables in a transform_awaiter whose
|
|
|
|
173 |
+ |
// standard await_suspend(coroutine_handle) calls this custom
|
|
|
|
174 |
+ |
// 3-argument overload, passing the executor and stop_token from
|
|
|
|
175 |
+ |
// the coroutine's context. For synchronous test awaitables like
|
|
|
|
176 |
+ |
// this one, the coroutine never suspends, so this is not invoked.
|
|
|
|
177 |
+ |
// The signature exists to allow the same awaitable type to work
|
|
|
|
178 |
+ |
// with both synchronous (test) and asynchronous (real I/O) code.
|
| 168 |
|
|
179 |
|
|
| 169 |
|
|
180 |
|
|
| 170 |
|
|
181 |
|
|
| 171 |
|
std::stop_token) const noexcept
|
182 |
|
std::stop_token) const noexcept
|
| 172 |
|
|
183 |
|
|
| 173 |
|
|
184 |
|
|
| 174 |
|
|
185 |
|
|
| 175 |
|
|
186 |
|
|
| 176 |
|
|
187 |
|
|
| 177 |
|
|
188 |
|
|
| 178 |
|
auto ec = self_->f_->maybe_fail();
|
189 |
|
auto ec = self_->f_->maybe_fail();
|
| 179 |
|
|
190 |
|
|
| 180 |
|
|
191 |
|
|
| 181 |
|
|
192 |
|
|
| 182 |
|
std::size_t to_commit = (std::min)(n_, self_->prepare_size_);
|
193 |
|
std::size_t to_commit = (std::min)(n_, self_->prepare_size_);
|
| 183 |
|
self_->data_.append(self_->prepare_buf_.data(), to_commit);
|
194 |
|
self_->data_.append(self_->prepare_buf_.data(), to_commit);
|
| 184 |
|
self_->prepare_size_ = 0;
|
195 |
|
self_->prepare_size_ = 0;
|
| 185 |
|
|
196 |
|
|
| 186 |
|
|
197 |
|
|
| 187 |
|
|
198 |
|
|
| 188 |
|
|
199 |
|
|
| 189 |
|
return awaitable{this, n};
|
200 |
|
return awaitable{this, n};
|
| 190 |
|
|
201 |
|
|
| 191 |
|
|
202 |
|
|
| 192 |
|
/** Commit bytes written with optional end-of-stream.
|
203 |
|
/** Commit bytes written with optional end-of-stream.
|
| 193 |
|
|
204 |
|
|
| 194 |
|
Transfers `n` bytes from the prepared buffer to the internal
|
205 |
|
Transfers `n` bytes from the prepared buffer to the internal
|
| 195 |
|
data buffer. If `eof` is true, marks the sink as finalized.
|
206 |
|
data buffer. If `eof` is true, marks the sink as finalized.
|
| 196 |
|
|
207 |
|
|
| 197 |
|
@param n The number of bytes to commit.
|
208 |
|
@param n The number of bytes to commit.
|
| 198 |
|
@param eof If true, signals end-of-stream after committing.
|
209 |
|
@param eof If true, signals end-of-stream after committing.
|
| 199 |
|
|
210 |
|
|
| 200 |
|
@return An awaitable yielding `(error_code)`.
|
211 |
|
@return An awaitable yielding `(error_code)`.
|
| 201 |
|
|
212 |
|
|
| 202 |
|
|
213 |
|
|
| 203 |
|
|
214 |
|
|
| 204 |
|
|
215 |
|
|
| 205 |
|
commit(std::size_t n, bool eof)
|
216 |
|
commit(std::size_t n, bool eof)
|
| 206 |
|
|
217 |
|
|
| 207 |
|
|
218 |
|
|
| 208 |
|
|
219 |
|
|
| 209 |
|
|
220 |
|
|
| 210 |
|
|
221 |
|
|
| 211 |
|
|
222 |
|
|
| 212 |
|
|
223 |
|
|
| 213 |
|
bool await_ready() const noexcept { return true; }
|
224 |
|
bool await_ready() const noexcept { return true; }
|
| 214 |
|
|
225 |
|
|
|
|
|
226 |
+ |
// This method is required to satisfy Capy's IoAwaitable concept,
|
|
|
|
227 |
+ |
// but is never called because await_ready() returns true.
|
|
|
|
228 |
+ |
// See the comment on commit(std::size_t) for a detailed explanation.
|
| 215 |
|
|
229 |
|
|
| 216 |
|
|
230 |
|
|
| 217 |
|
|
231 |
|
|
| 218 |
|
std::stop_token) const noexcept
|
232 |
|
std::stop_token) const noexcept
|
| 219 |
|
|
233 |
|
|
| 220 |
|
|
234 |
|
|
| 221 |
|
|
235 |
|
|
| 222 |
|
|
236 |
|
|
| 223 |
|
|
237 |
|
|
| 224 |
|
|
238 |
|
|
| 225 |
|
auto ec = self_->f_->maybe_fail();
|
239 |
|
auto ec = self_->f_->maybe_fail();
|
| 226 |
|
|
240 |
|
|
| 227 |
|
|
241 |
|
|
| 228 |
|
|
242 |
|
|
| 229 |
|
std::size_t to_commit = (std::min)(n_, self_->prepare_size_);
|
243 |
|
std::size_t to_commit = (std::min)(n_, self_->prepare_size_);
|
| 230 |
|
self_->data_.append(self_->prepare_buf_.data(), to_commit);
|
244 |
|
self_->data_.append(self_->prepare_buf_.data(), to_commit);
|
| 231 |
|
self_->prepare_size_ = 0;
|
245 |
|
self_->prepare_size_ = 0;
|
| 232 |
|
|
246 |
|
|
| 233 |
|
|
247 |
|
|
| 234 |
|
self_->eof_called_ = true;
|
248 |
|
self_->eof_called_ = true;
|
| 235 |
|
|
249 |
|
|
| 236 |
|
|
250 |
|
|
| 237 |
|
|
251 |
|
|
| 238 |
|
|
252 |
|
|
| 239 |
|
return awaitable{this, n, eof};
|
253 |
|
return awaitable{this, n, eof};
|
| 240 |
|
|
254 |
|
|
| 241 |
|
|
255 |
|
|
| 242 |
|
/** Signal end-of-stream.
|
256 |
|
/** Signal end-of-stream.
|
| 243 |
|
|
257 |
|
|
| 244 |
|
Marks the sink as finalized, indicating no more data will be
|
258 |
|
Marks the sink as finalized, indicating no more data will be
|
| 245 |
|
written. Before signaling, the attached @ref fuse is consulted
|
259 |
|
written. Before signaling, the attached @ref fuse is consulted
|
| 246 |
|
to possibly inject an error for testing fault scenarios.
|
260 |
|
to possibly inject an error for testing fault scenarios.
|
| 247 |
|
|
261 |
|
|
| 248 |
|
@return An awaitable yielding `(error_code)`.
|
262 |
|
@return An awaitable yielding `(error_code)`.
|
| 249 |
|
|
263 |
|
|
| 250 |
|
|
264 |
|
|
| 251 |
|
|
265 |
|
|
| 252 |
|
|
266 |
|
|
| 253 |
|
|
267 |
|
|
| 254 |
|
|
268 |
|
|
| 255 |
|
|
269 |
|
|
| 256 |
|
|
270 |
|
|
| 257 |
|
|
271 |
|
|
| 258 |
|
|
272 |
|
|
| 259 |
|
bool await_ready() const noexcept { return true; }
|
273 |
|
bool await_ready() const noexcept { return true; }
|
| 260 |
|
|
274 |
|
|
|
|
|
275 |
+ |
// This method is required to satisfy Capy's IoAwaitable concept,
|
|
|
|
276 |
+ |
// but is never called because await_ready() returns true.
|
|
|
|
277 |
+ |
// See the comment on commit(std::size_t) for a detailed explanation.
|
| 261 |
|
|
278 |
|
|
| 262 |
|
|
279 |
|
|
| 263 |
|
|
280 |
|
|
| 264 |
|
std::stop_token) const noexcept
|
281 |
|
std::stop_token) const noexcept
|
| 265 |
|
|
282 |
|
|
| 266 |
|
|
283 |
|
|
| 267 |
|
|
284 |
|
|
| 268 |
|
|
285 |
|
|
| 269 |
|
|
286 |
|
|
| 270 |
|
|
287 |
|
|
| 271 |
|
auto ec = self_->f_->maybe_fail();
|
288 |
|
auto ec = self_->f_->maybe_fail();
|
| 272 |
|
|
289 |
|
|
| 273 |
|
|
290 |
|
|
| 274 |
|
|
291 |
|
|
| 275 |
|
self_->eof_called_ = true;
|
292 |
|
self_->eof_called_ = true;
|
| 276 |
|
|
293 |
|
|
| 277 |
|
|
294 |
|
|
| 278 |
|
|
295 |
|
|
| 279 |
|
|
296 |
|
|
| 280 |
|
|
297 |
|
|
| 281 |
|
|
298 |
|
|
| 282 |
|
|
299 |
|
|
| 283 |
|
|
300 |
|
|
| 284 |
|
|
301 |
|
|
| 285 |
|
|
302 |
|
|
| 286 |
|
|
303 |
|
|
| 287 |
|
|
304 |
|
|