TLA Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2026 Michael Vandeberg
4 : //
5 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 : //
8 : // Official repository: https://github.com/cppalliance/http
9 : //
10 :
11 : #ifndef BOOST_HTTP_TEST_WRITE_SINK_HPP
12 : #define BOOST_HTTP_TEST_WRITE_SINK_HPP
13 :
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/await_context.hpp>
19 : #include <boost/capy/results.hpp>
20 : #include <boost/capy/error.hpp>
21 : #include <boost/capy/test/fuse.hpp>
22 :
23 : #include <algorithm>
24 : #include <string>
25 : #include <string_view>
26 :
27 : namespace boost {
28 : namespace http {
29 : namespace test {
30 :
31 : /** A mock sink for testing write operations.
32 :
33 : Use this to verify code that performs complete writes without needing
34 : real I/O. Call @ref write to write data, then @ref data to retrieve
35 : what was written. The associated @ref capy::test::fuse enables error injection
36 : at controlled points.
37 :
38 : This class satisfies the @ref WriteSink concept by providing partial
39 : writes via `write_some` (satisfying @ref WriteStream), complete
40 : writes via `write`, and EOF signaling via `write_eof`.
41 :
42 : @par Thread Safety
43 : Not thread-safe.
44 :
45 : @par Example
46 : @code
47 : capy::test::fuse f;
48 : write_sink ws( f );
49 :
50 : auto r = f.armed( [&]( capy::test::fuse& ) -> task<void> {
51 : auto [ec, n] = co_await ws.write(
52 : capy::const_buffer( "Hello", 5 ) );
53 : if( ec )
54 : co_return;
55 : auto [ec2] = co_await ws.write_eof();
56 : if( ec2 )
57 : co_return;
58 : // ws.data() returns "Hello"
59 : } );
60 : @endcode
61 :
62 : @see capy::test::fuse, WriteSink
63 : */
64 : class write_sink
65 : {
66 : capy::test::fuse f_;
67 : std::string data_;
68 : std::string expect_;
69 : std::size_t max_write_size_;
70 : bool eof_called_ = false;
71 :
72 : std::error_code
73 HIT 1056 : consume_match_() noexcept
74 : {
75 1056 : if(data_.empty() || expect_.empty())
76 1056 : return {};
77 MIS 0 : std::size_t const n = (std::min)(data_.size(), expect_.size());
78 0 : if(std::string_view(data_.data(), n) !=
79 0 : std::string_view(expect_.data(), n))
80 0 : return capy::error::test_failure;
81 0 : data_.erase(0, n);
82 0 : expect_.erase(0, n);
83 0 : return {};
84 : }
85 :
86 : public:
87 : /** Construct a write sink.
88 :
89 : @param f The capy::test::fuse used to inject errors during writes.
90 :
91 : @param max_write_size Maximum bytes transferred per write.
92 : Use to simulate chunked delivery.
93 : */
94 HIT 402 : explicit write_sink(
95 : capy::test::fuse f = {},
96 : std::size_t max_write_size = std::size_t(-1)) noexcept
97 402 : : f_(std::move(f))
98 402 : , max_write_size_(max_write_size)
99 : {
100 402 : }
101 :
102 : /// Return the written data as a string view.
103 : std::string_view
104 54 : data() const noexcept
105 : {
106 54 : return data_;
107 : }
108 :
109 : /** Set the expected data for subsequent writes.
110 :
111 : Stores the expected data and immediately tries to match
112 : against any data already written. Matched data is consumed
113 : from both buffers.
114 :
115 : @param sv The expected data.
116 :
117 : @return An error if existing data does not match.
118 : */
119 : std::error_code
120 : expect(std::string_view sv)
121 : {
122 : expect_.assign(sv);
123 : return consume_match_();
124 : }
125 :
126 : /// Return the number of bytes written.
127 : std::size_t
128 2 : size() const noexcept
129 : {
130 2 : return data_.size();
131 : }
132 :
133 : /// Return whether write_eof has been called.
134 : bool
135 42 : eof_called() const noexcept
136 : {
137 42 : return eof_called_;
138 : }
139 :
140 : /// Clear all data and reset state.
141 : void
142 : clear() noexcept
143 : {
144 : data_.clear();
145 : expect_.clear();
146 : eof_called_ = false;
147 : }
148 :
149 : /** Asynchronously write some data to the sink.
150 :
151 : Transfers up to `capy::buffer_size( buffers )` bytes from the provided
152 : const buffer sequence to the internal buffer. Before every write,
153 : the attached @ref capy::test::fuse is consulted to possibly inject an error.
154 :
155 : @param buffers The const buffer sequence containing data to write.
156 :
157 : @return An awaitable that await-returns `(error_code,std::size_t)`.
158 :
159 : @par Cancellation
160 : If the environment's stop token has been requested, the write
161 : completes immediately with `capy::error::canceled` and transfers no
162 : data. An empty buffer sequence is a no-op that completes
163 : successfully regardless of the stop token.
164 :
165 : @see capy::test::fuse
166 : */
167 : template<capy::ConstBufferSequence CB>
168 : auto
169 38 : write_some(CB buffers)
170 : {
171 : struct awaitable
172 : {
173 : write_sink* self_;
174 : CB buffers_;
175 : bool canceled_ = false;
176 :
177 38 : bool await_ready() const noexcept { return false; }
178 :
179 : // The operation completes synchronously, but await_suspend is
180 : // the only place capy::await_context is delivered (the promise's
181 : // awaiter forwards it here). Returning false means
182 : // the coroutine does not actually suspend; it resumes
183 : // immediately, having observed the stop token. See capy::await_context,
184 : // ContextAwaitable.
185 : bool
186 38 : await_suspend(
187 : std::coroutine_handle<>,
188 : capy::await_context const* env) noexcept
189 : {
190 38 : canceled_ = env->stop_token.stop_requested();
191 38 : return false;
192 : }
193 :
194 : capy::results<std::size_t>
195 38 : await_resume()
196 : {
197 38 : if(capy::buffers_empty(buffers_))
198 MIS 0 : return {std::error_code(), 0};
199 :
200 HIT 38 : if(canceled_)
201 MIS 0 : return {capy::error::canceled, 0};
202 :
203 HIT 38 : auto ec = self_->f_.maybe_fail();
204 28 : if(ec)
205 10 : return {ec, 0};
206 :
207 18 : std::size_t n = capy::buffer_size(buffers_);
208 18 : n = (std::min)(n, self_->max_write_size_);
209 :
210 18 : std::size_t const old_size = self_->data_.size();
211 18 : self_->data_.resize(old_size + n);
212 18 : capy::buffer_copy(capy::make_buffer(
213 18 : self_->data_.data() + old_size, n), buffers_, n);
214 :
215 18 : ec = self_->consume_match_();
216 18 : if(ec)
217 : {
218 MIS 0 : self_->data_.resize(old_size);
219 0 : return {ec, 0};
220 : }
221 :
222 HIT 18 : return {std::error_code(), n};
223 : }
224 : };
225 38 : return awaitable{this, buffers};
226 : }
227 :
228 : /** Asynchronously write data to the sink.
229 :
230 : Transfers all bytes from the provided const buffer sequence
231 : to the internal buffer. Unlike @ref write_some, this ignores
232 : `max_write_size` and writes all available data, matching the
233 : @ref WriteSink semantic contract.
234 :
235 : @par Exception Safety
236 : Injected I/O conditions are reported via the `error_code`
237 : component of the result. Throws `std::system_error` only when
238 : the attached @ref capy::test::fuse is in exception mode and reaches its
239 : failure point; no-throw otherwise.
240 :
241 : @param buffers The const buffer sequence containing data to write.
242 :
243 : @return An awaitable that await-returns `(error_code,std::size_t)`.
244 :
245 : @par Cancellation
246 : If the environment's stop token has been requested, the write
247 : completes immediately with `capy::error::canceled` and transfers no
248 : data.
249 :
250 : @throws std::system_error When the attached @ref capy::test::fuse is in
251 : exception mode and reaches its failure point.
252 :
253 : @see capy::test::fuse
254 : */
255 : template<capy::ConstBufferSequence CB>
256 : auto
257 1150 : write(CB buffers)
258 : {
259 : struct awaitable
260 : {
261 : write_sink* self_;
262 : CB buffers_;
263 : bool canceled_ = false;
264 :
265 1150 : bool await_ready() const noexcept { return false; }
266 :
267 : // Reads the stop token without suspending; see the comment
268 : // on write_some() for details.
269 : bool
270 1150 : await_suspend(
271 : std::coroutine_handle<>,
272 : capy::await_context const* env) noexcept
273 : {
274 1150 : canceled_ = env->stop_token.stop_requested();
275 1150 : return false;
276 : }
277 :
278 : capy::results<std::size_t>
279 1150 : await_resume()
280 : {
281 1150 : if(canceled_)
282 MIS 0 : return {capy::error::canceled, 0};
283 :
284 HIT 1150 : auto ec = self_->f_.maybe_fail();
285 1091 : if(ec)
286 59 : return {ec, 0};
287 :
288 1032 : std::size_t n = capy::buffer_size(buffers_);
289 1032 : if(n == 0)
290 MIS 0 : return {std::error_code(), 0};
291 :
292 HIT 1032 : std::size_t const old_size = self_->data_.size();
293 1032 : self_->data_.resize(old_size + n);
294 1032 : capy::buffer_copy(capy::make_buffer(
295 1032 : self_->data_.data() + old_size, n), buffers_);
296 :
297 1032 : ec = self_->consume_match_();
298 1032 : if(ec)
299 MIS 0 : return {ec, n};
300 :
301 HIT 1032 : return {std::error_code(), n};
302 : }
303 : };
304 1150 : return awaitable{this, buffers};
305 : }
306 :
307 : /** Atomically write data and signal end-of-stream.
308 :
309 : Transfers all bytes from the provided const buffer sequence to
310 : the internal buffer and signals end-of-stream. Before the write,
311 : the attached @ref capy::test::fuse is consulted to possibly inject an error
312 : for testing fault scenarios.
313 :
314 : @par Effects
315 : On success, appends the written bytes to the internal buffer
316 : and marks the sink as finalized.
317 : If an error is injected by the capy::test::fuse, the internal buffer remains
318 : unchanged.
319 :
320 : @par Exception Safety
321 : Injected I/O conditions are reported via the `error_code`
322 : component of the result. Throws `std::system_error` only when
323 : the attached @ref capy::test::fuse is in exception mode and reaches its
324 : failure point; no-throw otherwise.
325 :
326 : @par Cancellation
327 : If the environment's stop token has been requested, the operation
328 : completes immediately with `capy::error::canceled`, transfers no data,
329 : and does not signal end-of-stream.
330 :
331 : @param buffers The const buffer sequence containing data to write.
332 :
333 : @return An awaitable that await-returns `(error_code,std::size_t)`.
334 :
335 : @throws std::system_error When the attached @ref capy::test::fuse is in
336 : exception mode and reaches its failure point.
337 :
338 : @see capy::test::fuse
339 : */
340 : template<capy::ConstBufferSequence CB>
341 : auto
342 16 : write_eof(CB buffers)
343 : {
344 : struct awaitable
345 : {
346 : write_sink* self_;
347 : CB buffers_;
348 : bool canceled_ = false;
349 :
350 16 : bool await_ready() const noexcept { return false; }
351 :
352 : // Reads the stop token without suspending; see the comment
353 : // on write_some() for details.
354 : bool
355 16 : await_suspend(
356 : std::coroutine_handle<>,
357 : capy::await_context const* env) noexcept
358 : {
359 16 : canceled_ = env->stop_token.stop_requested();
360 16 : return false;
361 : }
362 :
363 : capy::results<std::size_t>
364 16 : await_resume()
365 : {
366 16 : if(canceled_)
367 MIS 0 : return {capy::error::canceled, 0};
368 :
369 HIT 16 : auto ec = self_->f_.maybe_fail();
370 11 : if(ec)
371 5 : return {ec, 0};
372 :
373 6 : std::size_t n = capy::buffer_size(buffers_);
374 6 : if(n > 0)
375 : {
376 6 : std::size_t const old_size = self_->data_.size();
377 6 : self_->data_.resize(old_size + n);
378 6 : capy::buffer_copy(capy::make_buffer(
379 6 : self_->data_.data() + old_size, n), buffers_);
380 :
381 6 : ec = self_->consume_match_();
382 6 : if(ec)
383 MIS 0 : return {ec, n};
384 : }
385 :
386 HIT 6 : self_->eof_called_ = true;
387 :
388 6 : return {std::error_code(), n};
389 : }
390 : };
391 16 : return awaitable{this, buffers};
392 : }
393 :
394 : /** Signal end-of-stream.
395 :
396 : Marks the sink as finalized, indicating no more data will be
397 : written. Before signaling, the attached @ref capy::test::fuse is consulted
398 : to possibly inject an error for testing fault scenarios.
399 :
400 : @par Effects
401 : On success, marks the sink as finalized.
402 : If an error is injected by the capy::test::fuse, the state remains unchanged.
403 :
404 : @par Exception Safety
405 : Injected I/O conditions are reported via the `error_code`
406 : component of the result. Throws `std::system_error` only when
407 : the attached @ref capy::test::fuse is in exception mode and reaches its
408 : failure point; no-throw otherwise.
409 :
410 : @par Cancellation
411 : If the environment's stop token has been requested, the operation
412 : completes immediately with `capy::error::canceled` and does not signal
413 : end-of-stream.
414 :
415 : @return An awaitable that await-returns `(error_code)`.
416 :
417 : @throws std::system_error When the attached @ref capy::test::fuse is in
418 : exception mode and reaches its failure point.
419 :
420 : @see capy::test::fuse
421 : */
422 : auto
423 68 : write_eof()
424 : {
425 : struct awaitable
426 : {
427 : write_sink* self_;
428 : bool canceled_ = false;
429 :
430 68 : bool await_ready() const noexcept { return false; }
431 :
432 : // Reads the stop token without suspending; see the comment
433 : // on write_some() for details.
434 : bool
435 68 : await_suspend(
436 : std::coroutine_handle<>,
437 : capy::await_context const* env) noexcept
438 : {
439 68 : canceled_ = env->stop_token.stop_requested();
440 68 : return false;
441 : }
442 :
443 : capy::results<>
444 68 : await_resume()
445 : {
446 68 : if(canceled_)
447 MIS 0 : return {capy::error::canceled};
448 :
449 HIT 68 : auto ec = self_->f_.maybe_fail();
450 50 : if(ec)
451 18 : return {ec};
452 :
453 32 : self_->eof_called_ = true;
454 32 : return {};
455 : }
456 : };
457 68 : return awaitable{this};
458 : }
459 : };
460 :
461 : } // test
462 : } // capy
463 : } // boost
464 :
465 : #endif
|