LCOV - code coverage report
Current view: top level - boost/capy/test - write_sink.hpp (source / functions) Coverage Total Hit
Test: coverage_filtered.info Lines: 52.7 % 74 39
Test Date: 2026-01-30 23:43:15 Functions: 64.7 % 17 11

            Line data    Source code
       1              : //
       2              : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
       3              : //
       4              : // Distributed under the Boost Software License, Version 1.0. (See accompanying
       5              : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
       6              : //
       7              : // Official repository: https://github.com/cppalliance/capy
       8              : //
       9              : 
      10              : #ifndef BOOST_CAPY_TEST_WRITE_SINK_HPP
      11              : #define BOOST_CAPY_TEST_WRITE_SINK_HPP
      12              : 
      13              : #include <boost/capy/detail/config.hpp>
      14              : #include <boost/capy/buffers.hpp>
      15              : #include <boost/capy/buffers/buffer_copy.hpp>
      16              : #include <boost/capy/buffers/make_buffer.hpp>
      17              : #include <boost/capy/coro.hpp>
      18              : #include <boost/capy/ex/executor_ref.hpp>
      19              : #include <boost/capy/io_result.hpp>
      20              : #include <boost/capy/error.hpp>
      21              : #include <boost/capy/test/fuse.hpp>
      22              : 
      23              : #include <algorithm>
      24              : #include <stop_token>
      25              : #include <string>
      26              : #include <string_view>
      27              : 
      28              : namespace boost {
      29              : namespace capy {
      30              : namespace test {
      31              : 
      32              : /** A mock sink for testing write operations.
      33              : 
      34              :     Use this to verify code that performs complete writes without needing
      35              :     real I/O. Call @ref write to write data, then @ref data to retrieve
      36              :     what was written. The associated @ref fuse enables error injection
      37              :     at controlled points.
      38              : 
      39              :     Unlike @ref write_stream which provides partial writes via `write_some`,
      40              :     this class satisfies the @ref WriteSink concept by providing complete
      41              :     writes and EOF signaling.
      42              : 
      43              :     @par Thread Safety
      44              :     Not thread-safe.
      45              : 
      46              :     @par Example
      47              :     @code
      48              :     fuse f;
      49              :     write_sink ws( f );
      50              : 
      51              :     auto r = f.armed( [&]( fuse& ) -> task<void> {
      52              :         auto [ec, n] = co_await ws.write(
      53              :             const_buffer( "Hello", 5 ) );
      54              :         if( ec )
      55              :             co_return;
      56              :         auto [ec2] = co_await ws.write_eof();
      57              :         if( ec2 )
      58              :             co_return;
      59              :         // ws.data() returns "Hello"
      60              :     } );
      61              :     @endcode
      62              : 
      63              :     @see fuse, WriteSink
      64              : */
      65              : class write_sink
      66              : {
      67              :     fuse* f_;
      68              :     std::string data_;
      69              :     std::string expect_;
      70              :     std::size_t max_write_size_;
      71              :     bool eof_called_ = false;
      72              : 
      73              :     std::error_code
      74          172 :     consume_match_() noexcept
      75              :     {
      76          172 :         if(data_.empty() || expect_.empty())
      77          172 :             return {};
      78            0 :         std::size_t const n = (std::min)(data_.size(), expect_.size());
      79            0 :         if(std::string_view(data_.data(), n) !=
      80            0 :             std::string_view(expect_.data(), n))
      81            0 :             return error::test_failure;
      82            0 :         data_.erase(0, n);
      83            0 :         expect_.erase(0, n);
      84            0 :         return {};
      85              :     }
      86              : 
      87              : public:
      88              :     /** Construct a write sink.
      89              : 
      90              :         @param f The fuse used to inject errors during writes.
      91              : 
      92              :         @param max_write_size Maximum bytes transferred per write.
      93              :         Use to simulate chunked delivery.
      94              :     */
      95          236 :     explicit write_sink(
      96              :         fuse& f,
      97              :         std::size_t max_write_size = std::size_t(-1)) noexcept
      98          236 :         : f_(&f)
      99          236 :         , max_write_size_(max_write_size)
     100              :     {
     101          236 :     }
     102              : 
     103              :     /// Return the written data as a string view.
     104              :     std::string_view
     105           38 :     data() const noexcept
     106              :     {
     107           38 :         return data_;
     108              :     }
     109              : 
     110              :     /** Set the expected data for subsequent writes.
     111              : 
     112              :         Stores the expected data and immediately tries to match
     113              :         against any data already written. Matched data is consumed
     114              :         from both buffers.
     115              : 
     116              :         @param sv The expected data.
     117              : 
     118              :         @return An error if existing data does not match.
     119              :     */
     120              :     std::error_code
     121              :     expect(std::string_view sv)
     122              :     {
     123              :         expect_.assign(sv);
     124              :         return consume_match_();
     125              :     }
     126              : 
     127              :     /// Return the number of bytes written.
     128              :     std::size_t
     129            2 :     size() const noexcept
     130              :     {
     131            2 :         return data_.size();
     132              :     }
     133              : 
     134              :     /// Return whether write_eof has been called.
     135              :     bool
     136           36 :     eof_called() const noexcept
     137              :     {
     138           36 :         return eof_called_;
     139              :     }
     140              : 
     141              :     /// Clear all data and reset state.
     142              :     void
     143              :     clear() noexcept
     144              :     {
     145              :         data_.clear();
     146              :         expect_.clear();
     147              :         eof_called_ = false;
     148              :     }
     149              : 
     150              :     /** Asynchronously write data to the sink.
     151              : 
     152              :         Transfers all bytes from the provided const buffer sequence to
     153              :         the internal buffer. Before every write, the attached @ref fuse
     154              :         is consulted to possibly inject an error for testing fault
     155              :         scenarios. The returned `std::size_t` is the number of bytes
     156              :         transferred.
     157              : 
     158              :         @par Effects
     159              :         On success, appends the written bytes to the internal buffer.
     160              :         If an error is injected by the fuse, the internal buffer remains
     161              :         unchanged.
     162              : 
     163              :         @par Exception Safety
     164              :         No-throw guarantee.
     165              : 
     166              :         @param buffers The const buffer sequence containing data to write.
     167              : 
     168              :         @return An awaitable yielding `(error_code,std::size_t)`.
     169              : 
     170              :         @see fuse
     171              :     */
     172              :     template<ConstBufferSequence CB>
     173              :     auto
     174          254 :     write(CB buffers)
     175              :     {
     176              :         struct awaitable
     177              :         {
     178              :             write_sink* self_;
     179              :             CB buffers_;
     180              : 
     181          254 :             bool await_ready() const noexcept { return true; }
     182              : 
     183            0 :             void await_suspend(
     184              :                 coro,
     185              :                 executor_ref,
     186              :                 std::stop_token) const noexcept
     187              :             {
     188            0 :             }
     189              : 
     190              :             io_result<std::size_t>
     191          254 :             await_resume()
     192              :             {
     193          254 :                 auto ec = self_->f_->maybe_fail();
     194          213 :                 if(ec)
     195           41 :                     return {ec, 0};
     196              : 
     197          172 :                 std::size_t n = buffer_size(buffers_);
     198          172 :                 n = (std::min)(n, self_->max_write_size_);
     199          172 :                 if(n == 0)
     200            0 :                     return {{}, 0};
     201              : 
     202          172 :                 std::size_t const old_size = self_->data_.size();
     203          172 :                 self_->data_.resize(old_size + n);
     204          172 :                 buffer_copy(make_buffer(
     205          172 :                     self_->data_.data() + old_size, n), buffers_, n);
     206              : 
     207          172 :                 ec = self_->consume_match_();
     208          172 :                 if(ec)
     209            0 :                     return {ec, n};
     210              : 
     211          172 :                 return {{}, n};
     212              :             }
     213              :         };
     214          254 :         return awaitable{this, buffers};
     215              :     }
     216              : 
     217              :     /** Asynchronously write data to the sink with optional EOF.
     218              : 
     219              :         Transfers all bytes from the provided const buffer sequence to
     220              :         the internal buffer, optionally signaling end-of-stream. Before
     221              :         every write, the attached @ref fuse is consulted to possibly
     222              :         inject an error for testing fault scenarios. The returned
     223              :         `std::size_t` is the number of bytes transferred.
     224              : 
     225              :         @par Effects
     226              :         On success, appends the written bytes to the internal buffer.
     227              :         If `eof` is `true`, marks the sink as finalized.
     228              :         If an error is injected by the fuse, the internal buffer remains
     229              :         unchanged.
     230              : 
     231              :         @par Exception Safety
     232              :         No-throw guarantee.
     233              : 
     234              :         @param buffers The const buffer sequence containing data to write.
     235              :         @param eof If true, signals end-of-stream after writing.
     236              : 
     237              :         @return An awaitable yielding `(error_code,std::size_t)`.
     238              : 
     239              :         @see fuse
     240              :     */
     241              :     template<ConstBufferSequence CB>
     242              :     auto
     243            0 :     write(CB buffers, bool eof)
     244              :     {
     245              :         struct awaitable
     246              :         {
     247              :             write_sink* self_;
     248              :             CB buffers_;
     249              :             bool eof_;
     250              : 
     251            0 :             bool await_ready() const noexcept { return true; }
     252              : 
     253            0 :             void await_suspend(
     254              :                 coro,
     255              :                 executor_ref,
     256              :                 std::stop_token) const noexcept
     257              :             {
     258            0 :             }
     259              : 
     260              :             io_result<std::size_t>
     261            0 :             await_resume()
     262              :             {
     263            0 :                 auto ec = self_->f_->maybe_fail();
     264            0 :                 if(ec)
     265            0 :                     return {ec, 0};
     266              : 
     267            0 :                 std::size_t n = buffer_size(buffers_);
     268            0 :                 n = (std::min)(n, self_->max_write_size_);
     269            0 :                 if(n > 0)
     270              :                 {
     271            0 :                     std::size_t const old_size = self_->data_.size();
     272            0 :                     self_->data_.resize(old_size + n);
     273            0 :                     buffer_copy(make_buffer(
     274            0 :                         self_->data_.data() + old_size, n), buffers_, n);
     275              : 
     276            0 :                     ec = self_->consume_match_();
     277            0 :                     if(ec)
     278            0 :                         return {ec, n};
     279              :                 }
     280              : 
     281            0 :                 if(eof_)
     282            0 :                     self_->eof_called_ = true;
     283              : 
     284            0 :                 return {{}, n};
     285              :             }
     286              :         };
     287            0 :         return awaitable{this, buffers, eof};
     288              :     }
     289              : 
     290              :     /** Signal end-of-stream.
     291              : 
     292              :         Marks the sink as finalized, indicating no more data will be
     293              :         written. Before signaling, the attached @ref fuse is consulted
     294              :         to possibly inject an error for testing fault scenarios.
     295              : 
     296              :         @par Effects
     297              :         On success, marks the sink as finalized.
     298              :         If an error is injected by the fuse, the state remains unchanged.
     299              : 
     300              :         @par Exception Safety
     301              :         No-throw guarantee.
     302              : 
     303              :         @return An awaitable yielding `(error_code)`.
     304              : 
     305              :         @see fuse
     306              :     */
     307              :     auto
     308           68 :     write_eof()
     309              :     {
     310              :         struct awaitable
     311              :         {
     312              :             write_sink* self_;
     313              : 
     314           68 :             bool await_ready() const noexcept { return true; }
     315              : 
     316            0 :             void await_suspend(
     317              :                 coro,
     318              :                 executor_ref,
     319              :                 std::stop_token) const noexcept
     320              :             {
     321            0 :             }
     322              : 
     323              :             io_result<>
     324           68 :             await_resume()
     325              :             {
     326           68 :                 auto ec = self_->f_->maybe_fail();
     327           50 :                 if(ec)
     328           18 :                     return {ec};
     329              : 
     330           32 :                 self_->eof_called_ = true;
     331           32 :                 return {};
     332              :             }
     333              :         };
     334           68 :         return awaitable{this};
     335              :     }
     336              : };
     337              : 
     338              : } // test
     339              : } // capy
     340              : } // boost
     341              : 
     342              : #endif
        

Generated by: LCOV version 2.3