Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 : //
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)
6 : //
7 : // Official repository: https://github.com/cppalliance/capy
8 : //
9 :
10 : #ifndef BOOST_CAPY_TEST_WRITE_SINK_HPP
11 : #define BOOST_CAPY_TEST_WRITE_SINK_HPP
12 :
13 : #include <boost/capy/detail/config.hpp>
14 : #include <boost/capy/buffers.hpp>
15 : #include <boost/capy/buffers/buffer_copy.hpp>
16 : #include <boost/capy/buffers/make_buffer.hpp>
17 : #include <boost/capy/coro.hpp>
18 : #include <boost/capy/ex/executor_ref.hpp>
19 : #include <boost/capy/io_result.hpp>
20 : #include <boost/capy/error.hpp>
21 : #include <boost/capy/test/fuse.hpp>
22 :
23 : #include <algorithm>
24 : #include <stop_token>
25 : #include <string>
26 : #include <string_view>
27 :
28 : namespace boost {
29 : namespace capy {
30 : namespace test {
31 :
32 : /** A mock sink for testing write operations.
33 :
34 : Use this to verify code that performs complete writes without needing
35 : real I/O. Call @ref write to write data, then @ref data to retrieve
36 : what was written. The associated @ref fuse enables error injection
37 : at controlled points.
38 :
39 : This class satisfies the @ref WriteSink concept by providing partial
40 : writes via `write_some` (satisfying @ref WriteStream), complete
41 : writes via `write`, and EOF signaling via `write_eof`.
42 :
43 : @par Thread Safety
44 : Not thread-safe.
45 :
46 : @par Example
47 : @code
48 : fuse f;
49 : write_sink ws( f );
50 :
51 : auto r = f.armed( [&]( fuse& ) -> task<void> {
52 : auto [ec, n] = co_await ws.write(
53 : const_buffer( "Hello", 5 ) );
54 : if( ec )
55 : co_return;
56 : auto [ec2] = co_await ws.write_eof();
57 : if( ec2 )
58 : co_return;
59 : // ws.data() returns "Hello"
60 : } );
61 : @endcode
62 :
63 : @see fuse, WriteSink
64 : */
65 : class write_sink
66 : {
67 : fuse f_;
68 : std::string data_;
69 : std::string expect_;
70 : std::size_t max_write_size_;
71 : bool eof_called_ = false;
72 :
73 : std::error_code
74 162 : consume_match_() noexcept
75 : {
76 162 : if(data_.empty() || expect_.empty())
77 162 : return {};
78 0 : std::size_t const n = (std::min)(data_.size(), expect_.size());
79 0 : if(std::string_view(data_.data(), n) !=
80 0 : std::string_view(expect_.data(), n))
81 0 : return error::test_failure;
82 0 : data_.erase(0, n);
83 0 : expect_.erase(0, n);
84 0 : return {};
85 : }
86 :
87 : public:
88 : /** Construct a write sink.
89 :
90 : @param f The fuse used to inject errors during writes.
91 :
92 : @param max_write_size Maximum bytes transferred per write.
93 : Use to simulate chunked delivery.
94 : */
95 262 : explicit write_sink(
96 : fuse f = {},
97 : std::size_t max_write_size = std::size_t(-1)) noexcept
98 262 : : f_(std::move(f))
99 262 : , max_write_size_(max_write_size)
100 : {
101 262 : }
102 :
103 : /// Return the written data as a string view.
104 : std::string_view
105 50 : data() const noexcept
106 : {
107 50 : return data_;
108 : }
109 :
110 : /** Set the expected data for subsequent writes.
111 :
112 : Stores the expected data and immediately tries to match
113 : against any data already written. Matched data is consumed
114 : from both buffers.
115 :
116 : @param sv The expected data.
117 :
118 : @return An error if existing data does not match.
119 : */
120 : std::error_code
121 : expect(std::string_view sv)
122 : {
123 : expect_.assign(sv);
124 : return consume_match_();
125 : }
126 :
127 : /// Return the number of bytes written.
128 : std::size_t
129 2 : size() const noexcept
130 : {
131 2 : return data_.size();
132 : }
133 :
134 : /// Return whether write_eof has been called.
135 : bool
136 38 : eof_called() const noexcept
137 : {
138 38 : return eof_called_;
139 : }
140 :
141 : /// Clear all data and reset state.
142 : void
143 : clear() noexcept
144 : {
145 : data_.clear();
146 : expect_.clear();
147 : eof_called_ = false;
148 : }
149 :
150 : /** Asynchronously write some data to the sink.
151 :
152 : Transfers up to `buffer_size( buffers )` bytes from the provided
153 : const buffer sequence to the internal buffer. Before every write,
154 : the attached @ref fuse is consulted to possibly inject an error.
155 :
156 : @param buffers The const buffer sequence containing data to write.
157 :
158 : @return An awaitable yielding `(error_code,std::size_t)`.
159 :
160 : @see fuse
161 : */
162 : template<ConstBufferSequence CB>
163 : auto
164 38 : write_some(CB buffers)
165 : {
166 : struct awaitable
167 : {
168 : write_sink* self_;
169 : CB buffers_;
170 :
171 38 : bool await_ready() const noexcept { return true; }
172 :
173 0 : void await_suspend(
174 : coro,
175 : executor_ref,
176 : std::stop_token) const noexcept
177 : {
178 0 : }
179 :
180 : io_result<std::size_t>
181 38 : await_resume()
182 : {
183 38 : if(buffer_empty(buffers_))
184 0 : return {{}, 0};
185 :
186 38 : auto ec = self_->f_.maybe_fail();
187 28 : if(ec)
188 10 : return {ec, 0};
189 :
190 18 : std::size_t n = buffer_size(buffers_);
191 18 : n = (std::min)(n, self_->max_write_size_);
192 :
193 18 : std::size_t const old_size = self_->data_.size();
194 18 : self_->data_.resize(old_size + n);
195 18 : buffer_copy(make_buffer(
196 18 : self_->data_.data() + old_size, n), buffers_, n);
197 :
198 18 : ec = self_->consume_match_();
199 18 : if(ec)
200 : {
201 0 : self_->data_.resize(old_size);
202 0 : return {ec, 0};
203 : }
204 :
205 18 : return {{}, n};
206 : }
207 : };
208 38 : return awaitable{this, buffers};
209 : }
210 :
211 : /** Asynchronously write data to the sink.
212 :
213 : Transfers all bytes from the provided const buffer sequence
214 : to the internal buffer. Unlike @ref write_some, this ignores
215 : `max_write_size` and writes all available data, matching the
216 : @ref WriteSink semantic contract.
217 :
218 : @param buffers The const buffer sequence containing data to write.
219 :
220 : @return An awaitable yielding `(error_code,std::size_t)`.
221 :
222 : @see fuse
223 : */
224 : template<ConstBufferSequence CB>
225 : auto
226 206 : write(CB buffers)
227 : {
228 : struct awaitable
229 : {
230 : write_sink* self_;
231 : CB buffers_;
232 :
233 206 : bool await_ready() const noexcept { return true; }
234 :
235 0 : void await_suspend(
236 : coro,
237 : executor_ref,
238 : std::stop_token) const noexcept
239 : {
240 0 : }
241 :
242 : io_result<std::size_t>
243 206 : await_resume()
244 : {
245 206 : auto ec = self_->f_.maybe_fail();
246 172 : if(ec)
247 34 : return {ec, 0};
248 :
249 138 : std::size_t n = buffer_size(buffers_);
250 138 : if(n == 0)
251 0 : return {{}, 0};
252 :
253 138 : std::size_t const old_size = self_->data_.size();
254 138 : self_->data_.resize(old_size + n);
255 138 : buffer_copy(make_buffer(
256 138 : self_->data_.data() + old_size, n), buffers_);
257 :
258 138 : ec = self_->consume_match_();
259 138 : if(ec)
260 0 : return {ec, n};
261 :
262 138 : return {{}, n};
263 : }
264 : };
265 206 : return awaitable{this, buffers};
266 : }
267 :
268 : /** Atomically write data and signal end-of-stream.
269 :
270 : Transfers all bytes from the provided const buffer sequence to
271 : the internal buffer and signals end-of-stream. Before the write,
272 : the attached @ref fuse is consulted to possibly inject an error
273 : for testing fault scenarios.
274 :
275 : @par Effects
276 : On success, appends the written bytes to the internal buffer
277 : and marks the sink as finalized.
278 : If an error is injected by the fuse, the internal buffer remains
279 : unchanged.
280 :
281 : @par Exception Safety
282 : No-throw guarantee.
283 :
284 : @param buffers The const buffer sequence containing data to write.
285 :
286 : @return An awaitable yielding `(error_code,std::size_t)`.
287 :
288 : @see fuse
289 : */
290 : template<ConstBufferSequence CB>
291 : auto
292 16 : write_eof(CB buffers)
293 : {
294 : struct awaitable
295 : {
296 : write_sink* self_;
297 : CB buffers_;
298 :
299 16 : bool await_ready() const noexcept { return true; }
300 :
301 0 : void await_suspend(
302 : coro,
303 : executor_ref,
304 : std::stop_token) const noexcept
305 : {
306 0 : }
307 :
308 : io_result<std::size_t>
309 16 : await_resume()
310 : {
311 16 : auto ec = self_->f_.maybe_fail();
312 11 : if(ec)
313 5 : return {ec, 0};
314 :
315 6 : std::size_t n = buffer_size(buffers_);
316 6 : if(n > 0)
317 : {
318 6 : std::size_t const old_size = self_->data_.size();
319 6 : self_->data_.resize(old_size + n);
320 6 : buffer_copy(make_buffer(
321 6 : self_->data_.data() + old_size, n), buffers_);
322 :
323 6 : ec = self_->consume_match_();
324 6 : if(ec)
325 0 : return {ec, n};
326 : }
327 :
328 6 : self_->eof_called_ = true;
329 :
330 6 : return {{}, n};
331 : }
332 : };
333 16 : return awaitable{this, buffers};
334 : }
335 :
336 : /** Signal end-of-stream.
337 :
338 : Marks the sink as finalized, indicating no more data will be
339 : written. Before signaling, the attached @ref fuse is consulted
340 : to possibly inject an error for testing fault scenarios.
341 :
342 : @par Effects
343 : On success, marks the sink as finalized.
344 : If an error is injected by the fuse, the state remains unchanged.
345 :
346 : @par Exception Safety
347 : No-throw guarantee.
348 :
349 : @return An awaitable yielding `(error_code)`.
350 :
351 : @see fuse
352 : */
353 : auto
354 60 : write_eof()
355 : {
356 : struct awaitable
357 : {
358 : write_sink* self_;
359 :
360 60 : bool await_ready() const noexcept { return true; }
361 :
362 : // This method is required to satisfy Capy's IoAwaitable concept,
363 : // but is never called because await_ready() returns true.
364 : // See the comment on write(CB buffers) for a detailed explanation.
365 0 : void await_suspend(
366 : coro,
367 : executor_ref,
368 : std::stop_token) const noexcept
369 : {
370 0 : }
371 :
372 : io_result<>
373 60 : await_resume()
374 : {
375 60 : auto ec = self_->f_.maybe_fail();
376 44 : if(ec)
377 16 : return {ec};
378 :
379 28 : self_->eof_called_ = true;
380 28 : return {};
381 : }
382 : };
383 60 : return awaitable{this};
384 : }
385 : };
386 :
387 : } // test
388 : } // capy
389 : } // boost
390 :
391 : #endif
|