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