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 <coroutine>
18 : #include <boost/capy/ex/io_env.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 : std::coroutine_handle<>,
175 : io_env const*) const noexcept
176 : {
177 0 : }
178 :
179 : io_result<std::size_t>
180 38 : await_resume()
181 : {
182 38 : if(buffer_empty(buffers_))
183 0 : return {{}, 0};
184 :
185 38 : auto ec = self_->f_.maybe_fail();
186 28 : if(ec)
187 10 : return {ec, 0};
188 :
189 18 : std::size_t n = buffer_size(buffers_);
190 18 : n = (std::min)(n, self_->max_write_size_);
191 :
192 18 : std::size_t const old_size = self_->data_.size();
193 18 : self_->data_.resize(old_size + n);
194 18 : buffer_copy(make_buffer(
195 18 : self_->data_.data() + old_size, n), buffers_, n);
196 :
197 18 : ec = self_->consume_match_();
198 18 : if(ec)
199 : {
200 0 : self_->data_.resize(old_size);
201 0 : return {ec, 0};
202 : }
203 :
204 18 : return {{}, n};
205 : }
206 : };
207 38 : return awaitable{this, buffers};
208 : }
209 :
210 : /** Asynchronously write data to the sink.
211 :
212 : Transfers all bytes from the provided const buffer sequence
213 : to the internal buffer. Unlike @ref write_some, this ignores
214 : `max_write_size` and writes all available data, matching the
215 : @ref WriteSink semantic contract.
216 :
217 : @param buffers The const buffer sequence containing data to write.
218 :
219 : @return An awaitable yielding `(error_code,std::size_t)`.
220 :
221 : @see fuse
222 : */
223 : template<ConstBufferSequence CB>
224 : auto
225 206 : write(CB buffers)
226 : {
227 : struct awaitable
228 : {
229 : write_sink* self_;
230 : CB buffers_;
231 :
232 206 : bool await_ready() const noexcept { return true; }
233 :
234 0 : void await_suspend(
235 : std::coroutine_handle<>,
236 : io_env const*) const noexcept
237 : {
238 0 : }
239 :
240 : io_result<std::size_t>
241 206 : await_resume()
242 : {
243 206 : auto ec = self_->f_.maybe_fail();
244 172 : if(ec)
245 34 : return {ec, 0};
246 :
247 138 : std::size_t n = buffer_size(buffers_);
248 138 : if(n == 0)
249 0 : return {{}, 0};
250 :
251 138 : std::size_t const old_size = self_->data_.size();
252 138 : self_->data_.resize(old_size + n);
253 138 : buffer_copy(make_buffer(
254 138 : self_->data_.data() + old_size, n), buffers_);
255 :
256 138 : ec = self_->consume_match_();
257 138 : if(ec)
258 0 : return {ec, n};
259 :
260 138 : return {{}, n};
261 : }
262 : };
263 206 : return awaitable{this, buffers};
264 : }
265 :
266 : /** Atomically write data and signal end-of-stream.
267 :
268 : Transfers all bytes from the provided const buffer sequence to
269 : the internal buffer and signals end-of-stream. Before the write,
270 : the attached @ref fuse is consulted to possibly inject an error
271 : for testing fault scenarios.
272 :
273 : @par Effects
274 : On success, appends the written bytes to the internal buffer
275 : and marks the sink as finalized.
276 : If an error is injected by the fuse, the internal buffer remains
277 : unchanged.
278 :
279 : @par Exception Safety
280 : No-throw guarantee.
281 :
282 : @param buffers The const buffer sequence containing data to write.
283 :
284 : @return An awaitable yielding `(error_code,std::size_t)`.
285 :
286 : @see fuse
287 : */
288 : template<ConstBufferSequence CB>
289 : auto
290 16 : write_eof(CB buffers)
291 : {
292 : struct awaitable
293 : {
294 : write_sink* self_;
295 : CB buffers_;
296 :
297 16 : bool await_ready() const noexcept { return true; }
298 :
299 0 : void await_suspend(
300 : std::coroutine_handle<>,
301 : io_env const*) const noexcept
302 : {
303 0 : }
304 :
305 : io_result<std::size_t>
306 16 : await_resume()
307 : {
308 16 : auto ec = self_->f_.maybe_fail();
309 11 : if(ec)
310 5 : return {ec, 0};
311 :
312 6 : std::size_t n = buffer_size(buffers_);
313 6 : if(n > 0)
314 : {
315 6 : std::size_t const old_size = self_->data_.size();
316 6 : self_->data_.resize(old_size + n);
317 6 : buffer_copy(make_buffer(
318 6 : self_->data_.data() + old_size, n), buffers_);
319 :
320 6 : ec = self_->consume_match_();
321 6 : if(ec)
322 0 : return {ec, n};
323 : }
324 :
325 6 : self_->eof_called_ = true;
326 :
327 6 : return {{}, n};
328 : }
329 : };
330 16 : return awaitable{this, buffers};
331 : }
332 :
333 : /** Signal end-of-stream.
334 :
335 : Marks the sink as finalized, indicating no more data will be
336 : written. Before signaling, the attached @ref fuse is consulted
337 : to possibly inject an error for testing fault scenarios.
338 :
339 : @par Effects
340 : On success, marks the sink as finalized.
341 : If an error is injected by the fuse, the state remains unchanged.
342 :
343 : @par Exception Safety
344 : No-throw guarantee.
345 :
346 : @return An awaitable yielding `(error_code)`.
347 :
348 : @see fuse
349 : */
350 : auto
351 60 : write_eof()
352 : {
353 : struct awaitable
354 : {
355 : write_sink* self_;
356 :
357 60 : bool await_ready() const noexcept { return true; }
358 :
359 : // This method is required to satisfy Capy's IoAwaitable concept,
360 : // but is never called because await_ready() returns true.
361 : // See the comment on write(CB buffers) for a detailed explanation.
362 0 : void await_suspend(
363 : std::coroutine_handle<>,
364 : io_env const*) const noexcept
365 : {
366 0 : }
367 :
368 : io_result<>
369 60 : await_resume()
370 : {
371 60 : auto ec = self_->f_.maybe_fail();
372 44 : if(ec)
373 16 : return {ec};
374 :
375 28 : self_->eof_called_ = true;
376 28 : return {};
377 : }
378 : };
379 60 : return awaitable{this};
380 : }
381 : };
382 :
383 : } // test
384 : } // capy
385 : } // boost
386 :
387 : #endif
|