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