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_BUFFER_SINK_HPP
12 : #define BOOST_HTTP_TEST_BUFFER_SINK_HPP
13 :
14 : #include <boost/capy/buffers.hpp>
15 : #include <boost/capy/buffers/make_buffer.hpp>
16 : #include <coroutine>
17 : #include <boost/capy/ex/await_context.hpp>
18 : #include <boost/capy/results.hpp>
19 : #include <boost/capy/test/fuse.hpp>
20 :
21 : #include <algorithm>
22 : #include <span>
23 : #include <string>
24 : #include <string_view>
25 :
26 : namespace boost {
27 : namespace http {
28 : namespace test {
29 :
30 : /** A mock buffer sink for testing callee-owns-buffers write operations.
31 :
32 : Use this to verify code that writes data using the callee-owns-buffers
33 : pattern without needing real I/O. Call @ref prepare to get writable
34 : buffers, write into them, then call @ref commit to finalize. The
35 : associated @ref capy::test::fuse enables error injection at controlled points.
36 :
37 : This class satisfies the @ref BufferSink concept by providing
38 : internal storage that callers write into directly.
39 :
40 : @par Thread Safety
41 : Not thread-safe.
42 :
43 : @par Example
44 : @code
45 : capy::test::fuse f;
46 : buffer_sink bs( f );
47 :
48 : auto r = f.armed( [&]( capy::test::fuse& ) -> task<void> {
49 : capy::mutable_buffer arr[16];
50 : auto bufs = bs.prepare( arr );
51 : if( bufs.empty() )
52 : co_return;
53 :
54 : // Write data into the first prepared buffer
55 : std::memcpy( bufs[0].data(), "Hello", 5 );
56 :
57 : auto [ec] = co_await bs.commit( 5 );
58 : if( ec )
59 : co_return;
60 :
61 : auto [ec2] = co_await bs.commit_eof( 0 );
62 : // bs.data() returns "Hello"
63 : } );
64 : @endcode
65 :
66 : @see capy::test::fuse, BufferSink
67 : */
68 : class buffer_sink
69 : {
70 : capy::test::fuse f_;
71 : std::string data_;
72 : std::string prepare_buf_;
73 : std::size_t prepare_size_ = 0;
74 : std::size_t max_prepare_size_;
75 : bool eof_called_ = false;
76 :
77 : public:
78 : /** Construct a buffer sink.
79 :
80 : @param f The capy::test::fuse used to inject errors during commits.
81 :
82 : @param max_prepare_size Maximum bytes available per prepare.
83 : Use to simulate limited buffer space.
84 : */
85 HIT 494 : explicit buffer_sink(
86 : capy::test::fuse f = {},
87 : std::size_t max_prepare_size = 4096) noexcept
88 494 : : f_(std::move(f))
89 494 : , max_prepare_size_(max_prepare_size)
90 : {
91 494 : prepare_buf_.resize(max_prepare_size_);
92 494 : }
93 :
94 : /// Return the written data as a string view.
95 : std::string_view
96 64 : data() const noexcept
97 : {
98 64 : return data_;
99 : }
100 :
101 : /// Return the number of bytes written.
102 : std::size_t
103 4 : size() const noexcept
104 : {
105 4 : return data_.size();
106 : }
107 :
108 : /// Return whether commit_eof has been called.
109 : bool
110 62 : eof_called() const noexcept
111 : {
112 62 : return eof_called_;
113 : }
114 :
115 : /// Clear all data and reset state.
116 : void
117 : clear() noexcept
118 : {
119 : data_.clear();
120 : prepare_size_ = 0;
121 : eof_called_ = false;
122 : }
123 :
124 : /** Prepare writable buffers.
125 :
126 : Fills the provided span with mutable buffer descriptors pointing
127 : to internal storage. The caller writes data into these buffers,
128 : then calls @ref commit to finalize.
129 :
130 : @param dest Span of capy::mutable_buffer to fill.
131 :
132 : @return A span of filled buffers (empty or 1 buffer in this implementation).
133 : */
134 : std::span<capy::mutable_buffer>
135 786 : prepare(std::span<capy::mutable_buffer> dest)
136 : {
137 786 : if(dest.empty())
138 MIS 0 : return {};
139 :
140 HIT 786 : prepare_size_ = max_prepare_size_;
141 786 : dest[0] = capy::make_buffer(prepare_buf_.data(), prepare_size_);
142 786 : return dest.first(1);
143 : }
144 :
145 : /** Commit bytes written to the prepared buffers.
146 :
147 : Transfers `n` bytes from the prepared buffer to the internal
148 : data buffer. Before committing, the attached @ref capy::test::fuse is
149 : consulted to possibly inject an error for testing fault scenarios.
150 :
151 : @param n The number of bytes to commit.
152 :
153 : @return An awaitable that await-returns `(error_code)`.
154 :
155 : @par Cancellation
156 : If the environment's stop token has been requested, the commit
157 : completes immediately with `capy::error::canceled` and commits no data.
158 :
159 : @see capy::test::fuse
160 : */
161 : auto
162 692 : commit(std::size_t n)
163 : {
164 : struct awaitable
165 : {
166 : buffer_sink* self_;
167 : std::size_t n_;
168 : bool canceled_ = false;
169 :
170 692 : bool await_ready() const noexcept { return false; }
171 :
172 : // The operation completes synchronously, but await_suspend is
173 : // the only place capy::await_context is delivered (the promise's
174 : // awaiter forwards it here). Returning false means
175 : // the coroutine does not actually suspend; it resumes
176 : // immediately, having observed the stop token. See capy::await_context,
177 : // ContextAwaitable.
178 : bool
179 692 : await_suspend(
180 : std::coroutine_handle<>,
181 : capy::await_context const* env) noexcept
182 : {
183 692 : canceled_ = env->stop_token.stop_requested();
184 692 : return false;
185 : }
186 :
187 : capy::results<>
188 692 : await_resume()
189 : {
190 692 : if(canceled_)
191 MIS 0 : return {capy::error::canceled};
192 :
193 HIT 692 : auto ec = self_->f_.maybe_fail();
194 617 : if(ec)
195 153 : return {ec};
196 :
197 464 : std::size_t to_commit = (std::min)(n_, self_->prepare_size_);
198 464 : self_->data_.append(self_->prepare_buf_.data(), to_commit);
199 464 : self_->prepare_size_ = 0;
200 :
201 464 : return {};
202 : }
203 : };
204 692 : return awaitable{this, n};
205 : }
206 :
207 : /** Commit final bytes and signal end-of-stream.
208 :
209 : Transfers `n` bytes from the prepared buffer to the internal
210 : data buffer and marks the sink as finalized. Before committing,
211 : the attached @ref capy::test::fuse is consulted to possibly inject an error
212 : for testing fault scenarios.
213 :
214 : @param n The number of bytes to commit.
215 :
216 : @return An awaitable that await-returns `(error_code)`.
217 :
218 : @par Cancellation
219 : If the environment's stop token has been requested, the operation
220 : completes immediately with `capy::error::canceled`, commits no data, and
221 : does not signal end-of-stream.
222 :
223 : @see capy::test::fuse
224 : */
225 : auto
226 162 : commit_eof(std::size_t n)
227 : {
228 : struct awaitable
229 : {
230 : buffer_sink* self_;
231 : std::size_t n_;
232 : bool canceled_ = false;
233 :
234 162 : bool await_ready() const noexcept { return false; }
235 :
236 : // Reads the stop token without suspending; see the comment
237 : // on commit() for details.
238 : bool
239 162 : await_suspend(
240 : std::coroutine_handle<>,
241 : capy::await_context const* env) noexcept
242 : {
243 162 : canceled_ = env->stop_token.stop_requested();
244 162 : return false;
245 : }
246 :
247 : capy::results<>
248 162 : await_resume()
249 : {
250 162 : if(canceled_)
251 MIS 0 : return {capy::error::canceled};
252 :
253 HIT 162 : auto ec = self_->f_.maybe_fail();
254 118 : if(ec)
255 44 : return {ec};
256 :
257 74 : std::size_t to_commit = (std::min)(n_, self_->prepare_size_);
258 74 : self_->data_.append(self_->prepare_buf_.data(), to_commit);
259 74 : self_->prepare_size_ = 0;
260 :
261 74 : self_->eof_called_ = true;
262 74 : return {};
263 : }
264 : };
265 162 : return awaitable{this, n};
266 : }
267 : };
268 :
269 : } // test
270 : } // capy
271 : } // boost
272 :
273 : #endif
|