LCOV - code coverage report
Current view: top level - capy/test - write_stream.hpp (source / functions) Coverage Total Hit
Test: coverage_remapped.info Lines: 100.0 % 46 46
Test Date: 2026-06-24 15:55:48 Functions: 100.0 % 33 33

           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/capy
       9                 : //
      10                 : 
      11                 : #ifndef BOOST_CAPY_TEST_WRITE_STREAM_HPP
      12                 : #define BOOST_CAPY_TEST_WRITE_STREAM_HPP
      13                 : 
      14                 : #include <boost/capy/detail/config.hpp>
      15                 : #include <boost/capy/buffers.hpp>
      16                 : #include <boost/capy/buffers/buffer_copy.hpp>
      17                 : #include <boost/capy/buffers/make_buffer.hpp>
      18                 : #include <coroutine>
      19                 : #include <boost/capy/ex/io_env.hpp>
      20                 : #include <boost/capy/io_result.hpp>
      21                 : #include <boost/capy/error.hpp>
      22                 : #include <boost/capy/test/fuse.hpp>
      23                 : 
      24                 : #include <algorithm>
      25                 : #include <string>
      26                 : #include <string_view>
      27                 : 
      28                 : namespace boost {
      29                 : namespace capy {
      30                 : namespace test {
      31                 : 
      32                 : /** A mock stream for testing write operations.
      33                 : 
      34                 :     Use this to verify code that performs writes without needing
      35                 :     real I/O. Call @ref write_some to write data, then @ref data
      36                 :     to retrieve what was written. The associated @ref fuse enables
      37                 :     error injection at controlled points. An optional
      38                 :     `max_write_size` constructor parameter limits bytes per write
      39                 :     to simulate chunked delivery.
      40                 : 
      41                 :     This class satisfies the @ref WriteStream concept.
      42                 : 
      43                 :     @par Thread Safety
      44                 :     Not thread-safe.
      45                 : 
      46                 :     @par Example
      47                 :     @code
      48                 :     fuse f;
      49                 :     write_stream ws( f );
      50                 : 
      51                 :     auto r = f.armed( [&]( fuse& ) -> task<void> {
      52                 :         auto [ec, n] = co_await ws.write_some(
      53                 :             const_buffer( "Hello", 5 ) );
      54                 :         if( ec )
      55                 :             co_return;
      56                 :         // ws.data() returns "Hello"
      57                 :     } );
      58                 :     @endcode
      59                 : 
      60                 :     @see fuse, WriteStream
      61                 : */
      62                 : class write_stream
      63                 : {
      64                 :     fuse f_;
      65                 :     std::string data_;
      66                 :     std::string expect_;
      67                 :     std::size_t max_write_size_;
      68                 : 
      69                 :     std::error_code
      70 HIT         959 :     consume_match_() noexcept
      71                 :     {
      72             959 :         if(data_.empty() || expect_.empty())
      73             943 :             return {};
      74              16 :         std::size_t const n = (std::min)(data_.size(), expect_.size());
      75              16 :         if(std::string_view(data_.data(), n) !=
      76              32 :             std::string_view(expect_.data(), n))
      77               4 :             return error::test_failure;
      78              12 :         data_.erase(0, n);
      79              12 :         expect_.erase(0, n);
      80              12 :         return {};
      81                 :     }
      82                 : 
      83                 : public:
      84                 :     /** Construct a write stream.
      85                 : 
      86                 :         @param f The fuse used to inject errors during writes.
      87                 : 
      88                 :         @param max_write_size Maximum bytes transferred per write.
      89                 :         Use to simulate chunked network delivery.
      90                 :     */
      91            1192 :     explicit write_stream(
      92                 :         fuse f = {},
      93                 :         std::size_t max_write_size = std::size_t(-1)) noexcept
      94            1192 :         : f_(std::move(f))
      95            1192 :         , max_write_size_(max_write_size)
      96                 :     {
      97            1192 :     }
      98                 : 
      99                 :     /// Return the written data as a string view.
     100                 :     std::string_view
     101             962 :     data() const noexcept
     102                 :     {
     103             962 :         return data_;
     104                 :     }
     105                 : 
     106                 :     /** Set the expected data for subsequent writes.
     107                 : 
     108                 :         Stores the expected data and immediately tries to match
     109                 :         against any data already written. Matched data is consumed
     110                 :         from both buffers.
     111                 : 
     112                 :         @param sv The expected data.
     113                 : 
     114                 :         @return An error if existing data does not match.
     115                 :     */
     116                 :     std::error_code
     117              30 :     expect(std::string_view sv)
     118                 :     {
     119              30 :         expect_.assign(sv);
     120              30 :         return consume_match_();
     121                 :     }
     122                 : 
     123                 :     /// Return the number of bytes written.
     124                 :     std::size_t
     125               7 :     size() const noexcept
     126                 :     {
     127               7 :         return data_.size();
     128                 :     }
     129                 : 
     130                 :     /** Asynchronously write data to the stream.
     131                 : 
     132                 :         Transfers up to `buffer_size( buffers )` bytes from the provided
     133                 :         const buffer sequence to the internal buffer. Before every write,
     134                 :         the attached @ref fuse is consulted to possibly inject an error
     135                 :         for testing fault scenarios. The returned `std::size_t` is the
     136                 :         number of bytes transferred.
     137                 : 
     138                 :         @par Effects
     139                 :         On success, appends the written bytes to the internal buffer.
     140                 :         If an error is injected by the fuse, the internal buffer remains
     141                 :         unchanged.
     142                 : 
     143                 :         @par Exception Safety
     144                 :         No-throw guarantee.
     145                 : 
     146                 :         @par Cancellation
     147                 :         If the environment's stop token has been requested, the write
     148                 :         completes immediately with `error::canceled` and transfers no
     149                 :         data. An empty buffer sequence is a no-op that completes
     150                 :         successfully regardless of the stop token.
     151                 : 
     152                 :         @param buffers The const buffer sequence containing data to write.
     153                 : 
     154                 :         @return An awaitable that await-returns `(error_code,std::size_t)`.
     155                 : 
     156                 :         @see fuse
     157                 :     */
     158                 :     template<ConstBufferSequence CB>
     159                 :     auto
     160            1194 :     write_some(CB buffers)
     161                 :     {
     162                 :         struct awaitable
     163                 :         {
     164                 :             write_stream* self_;
     165                 :             CB buffers_;
     166                 :             bool canceled_ = false;
     167                 : 
     168            1194 :             bool await_ready() const noexcept { return false; }
     169                 : 
     170                 :             // The operation completes synchronously, but await_suspend is
     171                 :             // the only place io_env is delivered (the promise's
     172                 :             // transform_awaiter forwards it here). Returning false means
     173                 :             // the coroutine does not actually suspend; it resumes
     174                 :             // immediately, having observed the stop token. See io_env,
     175                 :             // IoAwaitable.
     176                 :             bool
     177            1194 :             await_suspend(
     178                 :                 std::coroutine_handle<>,
     179                 :                 io_env const* env) noexcept
     180                 :             {
     181            1194 :                 canceled_ = env->stop_token.stop_requested();
     182            1194 :                 return false;
     183                 :             }
     184                 : 
     185                 :             io_result<std::size_t>
     186            1194 :             await_resume()
     187                 :             {
     188            1194 :                 if(buffer_empty(buffers_))
     189               2 :                     return {{}, 0};
     190                 : 
     191            1192 :                 if(canceled_)
     192               1 :                     return {error::canceled, 0};
     193                 : 
     194            1191 :                 auto ec = self_->f_.maybe_fail();
     195            1060 :                 if(ec)
     196             131 :                     return {ec, 0};
     197                 : 
     198             929 :                 std::size_t n = buffer_size(buffers_);
     199             929 :                 n = (std::min)(n, self_->max_write_size_);
     200                 : 
     201             929 :                 std::size_t const old_size = self_->data_.size();
     202             929 :                 self_->data_.resize(old_size + n);
     203             929 :                 buffer_copy(make_buffer(
     204             929 :                     self_->data_.data() + old_size, n), buffers_, n);
     205                 : 
     206             929 :                 ec = self_->consume_match_();
     207             929 :                 if(ec)
     208                 :                 {
     209               2 :                     self_->data_.resize(old_size);
     210               2 :                     return {ec, 0};
     211                 :                 }
     212                 : 
     213             927 :                 return {{}, n};
     214                 :             }
     215                 :         };
     216            1194 :         return awaitable{this, buffers};
     217                 :     }
     218                 : };
     219                 : 
     220                 : } // test
     221                 : } // capy
     222                 : } // boost
     223                 : 
     224                 : #endif
        

Generated by: LCOV version 2.3