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_SOURCE_HPP
12 : #define BOOST_HTTP_TEST_BUFFER_SOURCE_HPP
13 :
14 : #include <boost/capy/buffers.hpp>
15 : #include <boost/capy/buffers/make_buffer.hpp>
16 : #include <coroutine>
17 : #include <boost/capy/error.hpp>
18 : #include <boost/capy/ex/await_context.hpp>
19 : #include <boost/capy/results.hpp>
20 : #include <boost/capy/test/fuse.hpp>
21 :
22 : #include <algorithm>
23 : #include <span>
24 : #include <string>
25 : #include <string_view>
26 :
27 : namespace boost {
28 : namespace http {
29 : namespace test {
30 :
31 : /** A mock buffer source for testing pull (BufferSource) operations.
32 :
33 : Use this to verify code that transfers data from a buffer source to
34 : a sink without needing real I/O. Call @ref provide to supply data,
35 : then @ref pull to retrieve buffer descriptors. The associated
36 : @ref capy::test::fuse enables error injection at controlled points.
37 :
38 : This class satisfies the @ref BufferSource concept by providing
39 : a pull interface that fills an array of buffer descriptors and
40 : a consume interface to indicate bytes used.
41 :
42 : @par Thread Safety
43 : Not thread-safe.
44 :
45 : @par Example
46 : @code
47 : capy::test::fuse f;
48 : buffer_source bs( f );
49 : bs.provide( "Hello, " );
50 : bs.provide( "World!" );
51 :
52 : auto r = f.armed( [&]( capy::test::fuse& ) -> task<void> {
53 : capy::const_buffer arr[16];
54 : auto [ec, bufs] = co_await bs.pull( arr );
55 : if( ec )
56 : co_return;
57 : // bufs contains buffer descriptors
58 : std::size_t n = capy::buffer_size( bufs );
59 : bs.consume( n );
60 : } );
61 : @endcode
62 :
63 : @see capy::test::fuse, BufferSource
64 : */
65 : class buffer_source
66 : {
67 : capy::test::fuse f_;
68 : std::string data_;
69 : std::size_t pos_ = 0;
70 : std::size_t max_pull_size_;
71 :
72 : public:
73 : /** Construct a buffer source.
74 :
75 : @param f The capy::test::fuse used to inject errors during pulls.
76 :
77 : @param max_pull_size Maximum bytes returned per pull.
78 : Use to simulate chunked delivery.
79 : */
80 HIT 302 : explicit buffer_source(
81 : capy::test::fuse f = {},
82 : std::size_t max_pull_size = std::size_t(-1)) noexcept
83 302 : : f_(std::move(f))
84 302 : , max_pull_size_(max_pull_size)
85 : {
86 302 : }
87 :
88 : /** Append data to be returned by subsequent pulls.
89 :
90 : Multiple calls accumulate data that @ref pull returns.
91 :
92 : @param sv The data to append.
93 : */
94 : void
95 316 : provide(std::string_view sv)
96 : {
97 316 : data_.append(sv);
98 316 : }
99 :
100 : /// Clear all data and reset the read position.
101 : void
102 : clear() noexcept
103 : {
104 : data_.clear();
105 : pos_ = 0;
106 : }
107 :
108 : /// Return the number of bytes available for pulling.
109 : std::size_t
110 : available() const noexcept
111 : {
112 : return data_.size() - pos_;
113 : }
114 :
115 : /** Consume bytes from the source.
116 :
117 : Advances the internal read position by the specified number
118 : of bytes. The next call to @ref pull returns data starting
119 : after the consumed bytes.
120 :
121 : @param n The number of bytes to consume. Must not exceed the
122 : total size of buffers returned by the previous @ref pull.
123 : */
124 : void
125 285 : consume(std::size_t n) noexcept
126 : {
127 285 : pos_ += n;
128 285 : }
129 :
130 : /** Pull buffer data from the source.
131 :
132 : Fills the provided span with buffer descriptors pointing to
133 : internal data starting from the current unconsumed position.
134 : Returns a span of filled buffers. When no data remains,
135 : returns an empty span to signal completion.
136 :
137 : Calling pull multiple times without intervening @ref consume
138 : returns the same data. Use consume to advance past processed
139 : bytes.
140 :
141 : @param dest Span of capy::const_buffer to fill.
142 :
143 : @return An awaitable that await-returns `(error_code,std::span<capy::const_buffer>)`.
144 :
145 : @par Cancellation
146 : If the environment's stop token has been requested, the pull
147 : completes immediately with `capy::error::canceled` and an empty span.
148 :
149 : @see consume, capy::test::fuse
150 : */
151 : auto
152 552 : pull(std::span<capy::const_buffer> dest)
153 : {
154 : struct awaitable
155 : {
156 : buffer_source* self_;
157 : std::span<capy::const_buffer> dest_;
158 : bool canceled_ = false;
159 :
160 552 : bool await_ready() const noexcept { return false; }
161 :
162 : // The operation completes synchronously, but await_suspend is
163 : // the only place capy::await_context is delivered (the promise's
164 : // awaiter forwards it here). Returning false means
165 : // the coroutine does not actually suspend; it resumes
166 : // immediately, having observed the stop token. See capy::await_context,
167 : // ContextAwaitable.
168 : bool
169 552 : await_suspend(
170 : std::coroutine_handle<>,
171 : capy::await_context const* env) noexcept
172 : {
173 552 : canceled_ = env->stop_token.stop_requested();
174 552 : return false;
175 : }
176 :
177 : capy::results<std::span<capy::const_buffer>>
178 552 : await_resume()
179 : {
180 552 : if(canceled_)
181 MIS 0 : return {capy::error::canceled, {}};
182 :
183 HIT 552 : auto ec = self_->f_.maybe_fail();
184 467 : if(ec)
185 85 : return {ec, {}};
186 :
187 382 : if(self_->pos_ >= self_->data_.size())
188 66 : return {capy::error::eof, {}};
189 :
190 316 : std::size_t avail = self_->data_.size() - self_->pos_;
191 316 : std::size_t to_return = (std::min)(avail, self_->max_pull_size_);
192 :
193 316 : if(dest_.empty())
194 MIS 0 : return {std::error_code(), {}};
195 :
196 : // Fill a single buffer descriptor
197 HIT 316 : dest_[0] = capy::make_buffer(
198 316 : self_->data_.data() + self_->pos_,
199 : to_return);
200 :
201 316 : return {std::error_code(), dest_.first(1)};
202 : }
203 : };
204 552 : return awaitable{this, dest};
205 : }
206 : };
207 :
208 : } // test
209 : } // capy
210 : } // boost
211 :
212 : #endif
|