LCOV - code coverage report
Current view: top level - include/boost/http/test - read_source.hpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 90.9 % 55 50 5
Test Date: 2026-11-24 19:37:16 Functions: 100.0 % 11 11

           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_READ_SOURCE_HPP
      12                 : #define BOOST_HTTP_TEST_READ_SOURCE_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 <string>
      24                 : #include <string_view>
      25                 : 
      26                 : namespace boost {
      27                 : namespace http {
      28                 : namespace test {
      29                 : 
      30                 : /** A mock source for testing read operations.
      31                 : 
      32                 :     Use this to verify code that performs complete reads without needing
      33                 :     real I/O. Call @ref provide to supply data, then @ref read
      34                 :     to consume it. The associated @ref capy::test::fuse enables error injection
      35                 :     at controlled points.
      36                 : 
      37                 :     This class satisfies the @ref ReadSource concept by providing both
      38                 :     partial reads via `read_some` (satisfying @ref ReadStream) and
      39                 :     complete reads via `read` that fill the entire buffer sequence
      40                 :     before returning.
      41                 : 
      42                 :     @par Thread Safety
      43                 :     Not thread-safe.
      44                 : 
      45                 :     @par Example
      46                 :     @code
      47                 :     capy::test::fuse f;
      48                 :     read_source rs( f );
      49                 :     rs.provide( "Hello, " );
      50                 :     rs.provide( "World!" );
      51                 : 
      52                 :     auto r = f.armed( [&]( capy::test::fuse& ) -> task<void> {
      53                 :         char buf[32];
      54                 :         auto [ec, n] = co_await rs.read(
      55                 :             capy::mutable_buffer( buf, sizeof( buf ) ) );
      56                 :         if( ec )
      57                 :             co_return;
      58                 :         // buf contains "Hello, World!"
      59                 :     } );
      60                 :     @endcode
      61                 : 
      62                 :     @see capy::test::fuse, ReadSource
      63                 : */
      64                 : class read_source
      65                 : {
      66                 :     capy::test::fuse f_;
      67                 :     std::string data_;
      68                 :     std::size_t pos_ = 0;
      69                 :     std::size_t max_read_size_;
      70                 : 
      71                 : public:
      72                 :     /** Construct a read source.
      73                 : 
      74                 :         @param f The capy::test::fuse used to inject errors during reads.
      75                 : 
      76                 :         @param max_read_size Maximum bytes returned per read.
      77                 :         Use to simulate chunked delivery.
      78                 :     */
      79 HIT         277 :     explicit read_source(
      80                 :         capy::test::fuse f = {},
      81                 :         std::size_t max_read_size = std::size_t(-1)) noexcept
      82             277 :         : f_(std::move(f))
      83             277 :         , max_read_size_(max_read_size)
      84                 :     {
      85             277 :     }
      86                 : 
      87                 :     /** Append data to be returned by subsequent reads.
      88                 : 
      89                 :         Multiple calls accumulate data that @ref read returns.
      90                 : 
      91                 :         @param sv The data to append.
      92                 :     */
      93                 :     void
      94             258 :     provide(std::string_view sv)
      95                 :     {
      96             258 :         data_.append(sv);
      97             258 :     }
      98                 : 
      99                 :     /// Clear all data and reset the read position.
     100                 :     void
     101                 :     clear() noexcept
     102                 :     {
     103                 :         data_.clear();
     104                 :         pos_ = 0;
     105                 :     }
     106                 : 
     107                 :     /// Return the number of bytes available for reading.
     108                 :     std::size_t
     109               8 :     available() const noexcept
     110                 :     {
     111               8 :         return data_.size() - pos_;
     112                 :     }
     113                 : 
     114                 :     /** Asynchronously read some data from the source.
     115                 : 
     116                 :         Transfers up to `capy::buffer_size( buffers )` bytes from the internal
     117                 :         buffer to the provided mutable buffer sequence. If no data
     118                 :         remains, returns `capy::error::eof`. Before every read, the attached
     119                 :         @ref capy::test::fuse is consulted to possibly inject an error for testing
     120                 :         fault scenarios.
     121                 : 
     122                 :         @param buffers The mutable buffer sequence to receive data.
     123                 : 
     124                 :         @return An awaitable that await-returns `(error_code,std::size_t)`.
     125                 : 
     126                 :         @par Cancellation
     127                 :         If the environment's stop token has been requested, the read
     128                 :         completes immediately with `capy::error::canceled` and transfers no
     129                 :         data. An empty buffer sequence is a no-op that completes
     130                 :         successfully regardless of the stop token.
     131                 : 
     132                 :         @see capy::test::fuse
     133                 :     */
     134                 :     template<capy::MutableBufferSequence MB>
     135                 :     auto
     136              50 :     read_some(MB buffers)
     137                 :     {
     138                 :         struct awaitable
     139                 :         {
     140                 :             read_source* self_;
     141                 :             MB buffers_;
     142                 :             bool canceled_ = false;
     143                 : 
     144              50 :             bool await_ready() const noexcept { return false; }
     145                 : 
     146                 :             // The operation completes synchronously, but await_suspend is
     147                 :             // the only place capy::await_context is delivered (the promise's
     148                 :             // awaiter forwards it here). Returning false means
     149                 :             // the coroutine does not actually suspend; it resumes
     150                 :             // immediately, having observed the stop token. See capy::await_context,
     151                 :             // ContextAwaitable.
     152                 :             bool
     153              50 :             await_suspend(
     154                 :                 std::coroutine_handle<>,
     155                 :                 capy::await_context const* env) noexcept
     156                 :             {
     157              50 :                 canceled_ = env->stop_token.stop_requested();
     158              50 :                 return false;
     159                 :             }
     160                 : 
     161                 :             capy::results<std::size_t>
     162              50 :             await_resume()
     163                 :             {
     164              50 :                 if(capy::buffers_empty(buffers_))
     165 MIS           0 :                     return {std::error_code(), 0};
     166                 : 
     167 HIT          50 :                 if(canceled_)
     168 MIS           0 :                     return {capy::error::canceled, 0};
     169                 : 
     170 HIT          50 :                 auto ec = self_->f_.maybe_fail();
     171              36 :                 if(ec)
     172              14 :                     return {ec, 0};
     173                 : 
     174              22 :                 if(self_->pos_ >= self_->data_.size())
     175               2 :                     return {capy::error::eof, 0};
     176                 : 
     177              20 :                 std::size_t avail = self_->data_.size() - self_->pos_;
     178              20 :                 if(avail > self_->max_read_size_)
     179 MIS           0 :                     avail = self_->max_read_size_;
     180 HIT          20 :                 auto src = capy::make_buffer(self_->data_.data() + self_->pos_, avail);
     181              20 :                 std::size_t const n = capy::buffer_copy(buffers_, src);
     182              20 :                 self_->pos_ += n;
     183              20 :                 return {std::error_code(), n};
     184                 :             }
     185                 :         };
     186              50 :         return awaitable{this, buffers};
     187                 :     }
     188                 : 
     189                 :     /** Asynchronously read data from the source.
     190                 : 
     191                 :         Fills the entire buffer sequence from the internal data.
     192                 :         If the available data is less than the buffer size, returns
     193                 :         `capy::error::eof` with the number of bytes transferred. Before
     194                 :         every read, the attached @ref capy::test::fuse is consulted to possibly
     195                 :         inject an error for testing fault scenarios.
     196                 : 
     197                 :         Unlike @ref read_some, this ignores `max_read_size` and
     198                 :         transfers all available data in a single operation, matching
     199                 :         the @ref ReadSource semantic contract.
     200                 : 
     201                 :         @param buffers The mutable buffer sequence to receive data.
     202                 : 
     203                 :         @return An awaitable that await-returns `(error_code,std::size_t)`.
     204                 : 
     205                 :         @par Cancellation
     206                 :         If the environment's stop token has been requested, the read
     207                 :         completes immediately with `capy::error::canceled` and transfers no
     208                 :         data. An empty buffer sequence is a no-op that completes
     209                 :         successfully regardless of the stop token.
     210                 : 
     211                 :         @see capy::test::fuse
     212                 :     */
     213                 :     template<capy::MutableBufferSequence MB>
     214                 :     auto
     215             292 :     read(MB buffers)
     216                 :     {
     217                 :         struct awaitable
     218                 :         {
     219                 :             read_source* self_;
     220                 :             MB buffers_;
     221                 :             bool canceled_ = false;
     222                 : 
     223             292 :             bool await_ready() const noexcept { return false; }
     224                 : 
     225                 :             // Reads the stop token without suspending; see the comment
     226                 :             // on read_some() for details.
     227                 :             bool
     228             292 :             await_suspend(
     229                 :                 std::coroutine_handle<>,
     230                 :                 capy::await_context const* env) noexcept
     231                 :             {
     232             292 :                 canceled_ = env->stop_token.stop_requested();
     233             292 :                 return false;
     234                 :             }
     235                 : 
     236                 :             capy::results<std::size_t>
     237             292 :             await_resume()
     238                 :             {
     239             292 :                 if(capy::buffers_empty(buffers_))
     240 MIS           0 :                     return {std::error_code(), 0};
     241                 : 
     242 HIT         292 :                 if(canceled_)
     243 MIS           0 :                     return {capy::error::canceled, 0};
     244                 : 
     245 HIT         292 :                 auto ec = self_->f_.maybe_fail();
     246             232 :                 if(ec)
     247              60 :                     return {ec, 0};
     248                 : 
     249             172 :                 if(self_->pos_ >= self_->data_.size())
     250              16 :                     return {capy::error::eof, 0};
     251                 : 
     252             156 :                 std::size_t avail = self_->data_.size() - self_->pos_;
     253             156 :                 auto src = capy::make_buffer(self_->data_.data() + self_->pos_, avail);
     254             156 :                 std::size_t const n = capy::buffer_copy(buffers_, src);
     255             156 :                 self_->pos_ += n;
     256                 : 
     257             156 :                 if(n < capy::buffer_size(buffers_))
     258              64 :                     return {capy::error::eof, n};
     259              92 :                 return {std::error_code(), n};
     260                 :             }
     261                 :         };
     262             292 :         return awaitable{this, buffers};
     263                 :     }
     264                 : };
     265                 : 
     266                 : } // test
     267                 : } // capy
     268                 : } // boost
     269                 : 
     270                 : #endif
        

Generated by: LCOV version 2.3