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 236 : consume_match_() noexcept
75 : {
76 236 : if(data_.empty() || expect_.empty())
77 228 : return {};
78 8 : std::size_t const n = (std::min)(data_.size(), expect_.size());
79 8 : if(std::string_view(data_.data(), n) !=
80 16 : std::string_view(expect_.data(), n))
81 4 : return error::test_failure;
82 4 : data_.erase(0, n);
83 4 : expect_.erase(0, n);
84 4 : 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 410 : explicit write_sink(
96 : fuse f = {},
97 : std::size_t max_write_size = std::size_t(-1)) noexcept
98 410 : : f_(std::move(f))
99 410 : , max_write_size_(max_write_size)
100 : {
101 410 : }
102 :
103 : /// Return the written data as a string view.
104 : std::string_view
105 100 : data() const noexcept
106 : {
107 100 : 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 16 : expect(std::string_view sv)
122 : {
123 16 : expect_.assign(sv);
124 16 : return consume_match_();
125 : }
126 :
127 : /// Return the number of bytes written.
128 : std::size_t
129 6 : size() const noexcept
130 : {
131 6 : return data_.size();
132 : }
133 :
134 : /// Return whether write_eof has been called.
135 : bool
136 64 : eof_called() const noexcept
137 : {
138 64 : return eof_called_;
139 : }
140 :
141 : /// Clear all data and reset state.
142 : void
143 4 : clear() noexcept
144 : {
145 4 : data_.clear();
146 4 : expect_.clear();
147 4 : eof_called_ = false;
148 4 : }
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 76 : write_some(CB buffers)
165 : {
166 : struct awaitable
167 : {
168 : write_sink* self_;
169 : CB buffers_;
170 :
171 76 : 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 76 : await_resume()
181 : {
182 76 : if(buffer_empty(buffers_))
183 2 : return {{}, 0};
184 :
185 74 : auto ec = self_->f_.maybe_fail();
186 53 : if(ec)
187 21 : return {ec, 0};
188 :
189 32 : std::size_t n = buffer_size(buffers_);
190 32 : n = (std::min)(n, self_->max_write_size_);
191 :
192 32 : std::size_t const old_size = self_->data_.size();
193 32 : self_->data_.resize(old_size + n);
194 32 : buffer_copy(make_buffer(
195 32 : self_->data_.data() + old_size, n), buffers_, n);
196 :
197 32 : ec = self_->consume_match_();
198 32 : if(ec)
199 : {
200 0 : self_->data_.resize(old_size);
201 0 : return {ec, 0};
202 : }
203 :
204 32 : return {{}, n};
205 : }
206 : };
207 76 : 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 302 : write(CB buffers)
226 : {
227 : struct awaitable
228 : {
229 : write_sink* self_;
230 : CB buffers_;
231 :
232 302 : 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 302 : await_resume()
242 : {
243 302 : auto ec = self_->f_.maybe_fail();
244 241 : if(ec)
245 61 : return {ec, 0};
246 :
247 180 : std::size_t n = buffer_size(buffers_);
248 180 : if(n == 0)
249 2 : return {{}, 0};
250 :
251 178 : std::size_t const old_size = self_->data_.size();
252 178 : self_->data_.resize(old_size + n);
253 178 : buffer_copy(make_buffer(
254 178 : self_->data_.data() + old_size, n), buffers_);
255 :
256 178 : ec = self_->consume_match_();
257 178 : if(ec)
258 2 : return {ec, n};
259 :
260 176 : return {{}, n};
261 : }
262 : };
263 302 : 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 34 : write_eof(CB buffers)
291 : {
292 : struct awaitable
293 : {
294 : write_sink* self_;
295 : CB buffers_;
296 :
297 34 : 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 34 : await_resume()
307 : {
308 34 : auto ec = self_->f_.maybe_fail();
309 23 : if(ec)
310 11 : return {ec, 0};
311 :
312 12 : std::size_t n = buffer_size(buffers_);
313 12 : if(n > 0)
314 : {
315 10 : std::size_t const old_size = self_->data_.size();
316 10 : self_->data_.resize(old_size + n);
317 10 : buffer_copy(make_buffer(
318 10 : self_->data_.data() + old_size, n), buffers_);
319 :
320 10 : ec = self_->consume_match_();
321 10 : if(ec)
322 0 : return {ec, n};
323 : }
324 :
325 12 : self_->eof_called_ = true;
326 :
327 12 : return {{}, n};
328 : }
329 : };
330 34 : 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 82 : write_eof()
352 : {
353 : struct awaitable
354 : {
355 : write_sink* self_;
356 :
357 82 : 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 82 : await_resume()
370 : {
371 82 : auto ec = self_->f_.maybe_fail();
372 60 : if(ec)
373 22 : return {ec};
374 :
375 38 : self_->eof_called_ = true;
376 38 : return {};
377 : }
378 : };
379 82 : return awaitable{this};
380 : }
381 : };
382 :
383 : } // test
384 : } // capy
385 : } // boost
386 :
387 : #endif
|