100.00% Lines (36/36) 100.00% Functions (8/8)
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/capy 8   // Official repository: https://github.com/cppalliance/capy
9   // 9   //
10   10  
11   #ifndef BOOST_CAPY_TEST_READ_STREAM_HPP 11   #ifndef BOOST_CAPY_TEST_READ_STREAM_HPP
12   #define BOOST_CAPY_TEST_READ_STREAM_HPP 12   #define BOOST_CAPY_TEST_READ_STREAM_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/buffers.hpp> 15   #include <boost/capy/buffers.hpp>
16   #include <boost/capy/buffers/buffer_copy.hpp> 16   #include <boost/capy/buffers/buffer_copy.hpp>
17   #include <boost/capy/buffers/make_buffer.hpp> 17   #include <boost/capy/buffers/make_buffer.hpp>
18   #include <boost/capy/cond.hpp> 18   #include <boost/capy/cond.hpp>
19   #include <coroutine> 19   #include <coroutine>
20   #include <boost/capy/ex/await_context.hpp> 20   #include <boost/capy/ex/await_context.hpp>
21   #include <boost/capy/results.hpp> 21   #include <boost/capy/results.hpp>
22   #include <boost/capy/test/fuse.hpp> 22   #include <boost/capy/test/fuse.hpp>
23   23  
24   #include <string> 24   #include <string>
25   #include <string_view> 25   #include <string_view>
26   26  
27   namespace boost { 27   namespace boost {
28   namespace capy { 28   namespace capy {
29   namespace test { 29   namespace test {
30   30  
31   /** Buffers data supplied via `provide`, then hands it out through `read_some`. 31   /** Buffers data supplied via `provide`, then hands it out through `read_some`.
32   32  
33   Use this to verify code that performs reads without needing 33   Use this to verify code that performs reads without needing
34   real I/O. Call @ref provide to supply data, then @ref read_some 34   real I/O. Call @ref provide to supply data, then @ref read_some
35   to consume it. The associated @ref fuse enables error injection 35   to consume it. The associated @ref fuse enables error injection
36   at controlled points. An optional `max_read_size` constructor 36   at controlled points. An optional `max_read_size` constructor
37   parameter limits bytes per read to simulate chunked delivery. 37   parameter limits bytes per read to simulate chunked delivery.
38   38  
39   This class satisfies the @ref ReadStream concept. 39   This class satisfies the @ref ReadStream concept.
40   40  
41   @par Thread Safety 41   @par Thread Safety
42   Not thread-safe. 42   Not thread-safe.
43   43  
44   @par Example 44   @par Example
45   @par !example example 45   @par !example example
46   46  
47   47  
48   @see @ref fuse, @ref ReadStream 48   @see @ref fuse, @ref ReadStream
49   */ 49   */
50   class read_stream 50   class read_stream
51   { 51   {
52   fuse f_; 52   fuse f_;
53   std::string data_; 53   std::string data_;
54   std::size_t pos_ = 0; 54   std::size_t pos_ = 0;
55   std::size_t max_read_size_; 55   std::size_t max_read_size_;
56   56  
57   public: 57   public:
58   /** Construct a read stream. 58   /** Construct a read stream.
59   59  
60   @param f The fuse used to inject errors during reads. 60   @param f The fuse used to inject errors during reads.
61   61  
62   @param max_read_size Maximum bytes returned per read. 62   @param max_read_size Maximum bytes returned per read.
63   Use to simulate chunked network delivery. 63   Use to simulate chunked network delivery.
64   */ 64   */
HITCBC 65   308 explicit read_stream( 65   301 explicit read_stream(
66   fuse f = {}, 66   fuse f = {},
67   std::size_t max_read_size = std::size_t(-1)) noexcept 67   std::size_t max_read_size = std::size_t(-1)) noexcept
HITCBC 68   308 : f_(std::move(f)) 68   301 : f_(std::move(f))
HITCBC 69   308 , max_read_size_(max_read_size) 69   301 , max_read_size_(max_read_size)
70   { 70   {
HITCBC 71   308 } 71   301 }
72   72  
73   /** Append data to be returned by subsequent reads. 73   /** Append data to be returned by subsequent reads.
74   74  
75   Multiple calls accumulate data that @ref read_some returns. 75   Multiple calls accumulate data that @ref read_some returns.
76   76  
77   @param sv The data to append. 77   @param sv The data to append.
78   */ 78   */
79   void 79   void
HITCBC 80   310 provide(std::string_view sv) 80   291 provide(std::string_view sv)
81   { 81   {
HITCBC 82   310 data_.append(sv); 82   291 data_.append(sv);
HITCBC 83   310 } 83   291 }
84   84  
85   /// Clear all data and reset the read position. 85   /// Clear all data and reset the read position.
86   void 86   void
HITCBC 87   6 clear() noexcept 87   6 clear() noexcept
88   { 88   {
HITCBC 89   6 data_.clear(); 89   6 data_.clear();
HITCBC 90   6 pos_ = 0; 90   6 pos_ = 0;
HITCBC 91   6 } 91   6 }
92   92  
93   /** Return the number of bytes available for reading. 93   /** Return the number of bytes available for reading.
94   94  
95   @return The number of provided bytes not yet consumed. 95   @return The number of provided bytes not yet consumed.
96   */ 96   */
97   std::size_t 97   std::size_t
HITCBC 98   24 available() const noexcept 98   24 available() const noexcept
99   { 99   {
HITCBC 100   24 return data_.size() - pos_; 100   24 return data_.size() - pos_;
101   } 101   }
102   102  
103   /** Asynchronously read data from the stream. 103   /** Asynchronously read data from the stream.
104   104  
105   Transfers up to `buffer_size( buffers )` bytes from the internal 105   Transfers up to `buffer_size( buffers )` bytes from the internal
106   buffer to the provided mutable buffer sequence. If no data remains, 106   buffer to the provided mutable buffer sequence. If no data remains,
107 - returns `error::eof`. Before every read, the attached @ref fuse is 107 + returns `error::eof`. Before transferring data, the attached
108 - consulted to possibly inject an error for testing fault scenarios. 108 + @ref fuse is consulted to possibly inject an error for testing
109 - The returned `std::size_t` is the number of bytes transferred. 109 + fault scenarios. An empty buffer sequence or a canceled read
  110 + completes without consulting the fuse. The returned
  111 + `std::size_t` is the number of bytes transferred.
110   112  
111   @par Effects 113   @par Effects
112   On success, advances the internal read position by the number of 114   On success, advances the internal read position by the number of
113   bytes copied. If an error is injected by the fuse, the read position 115   bytes copied. If an error is injected by the fuse, the read position
114   remains unchanged. 116   remains unchanged.
115   117  
116   @par Exception Safety 118   @par Exception Safety
117   Injected I/O conditions are reported via the `error_code` 119   Injected I/O conditions are reported via the `error_code`
118   component of the result. Throws `std::system_error` only when 120   component of the result. Throws `std::system_error` only when
119   the attached @ref fuse is in exception mode and reaches its 121   the attached @ref fuse is in exception mode and reaches its
120   failure point; no-throw otherwise. 122   failure point; no-throw otherwise.
121   123  
122   @par Cancellation 124   @par Cancellation
123   If the environment's stop token is requested, the read 125   If the environment's stop token is requested, the read
124   completes immediately with `error::canceled` and transfers no 126   completes immediately with `error::canceled` and transfers no
125   data. This lets code under test exercise its cancellation paths. 127   data. This lets code under test exercise its cancellation paths.
126   An empty buffer sequence is a no-op that completes successfully 128   An empty buffer sequence is a no-op that completes successfully
127   regardless of the stop token. 129   regardless of the stop token.
128   130  
  131 + @throws std::system_error When the attached @ref fuse is in
  132 + exception mode and reaches its failure point.
  133 +
129   @param buffers The mutable buffer sequence to receive data. 134   @param buffers The mutable buffer sequence to receive data.
130   135  
131 -  
132 - @throws std::system_error When the attached @ref fuse is in  
133 - exception mode and reaches its failure point.  
134   @return An awaitable that await-returns `(error_code,std::size_t)`. 136   @return An awaitable that await-returns `(error_code,std::size_t)`.
135   137  
136   @see @ref fuse 138   @see @ref fuse
137   */ 139   */
138   template<MutableBufferSequence MB> 140   template<MutableBufferSequence MB>
139   auto 141   auto
HITCBC 140   439 read_some(MB buffers) 142   405 read_some(MB buffers)
141   { 143   {
142   struct awaitable 144   struct awaitable
143   { 145   {
144   read_stream* self_; 146   read_stream* self_;
145   MB buffers_; 147   MB buffers_;
146   bool canceled_ = false; 148   bool canceled_ = false;
147   149  
HITCBC 148   439 bool await_ready() const noexcept { return false; } 150   405 bool await_ready() const noexcept { return false; }
149   151  
150   // The operation completes synchronously, but await_suspend 152   // The operation completes synchronously, but await_suspend
151   // is the only place await_context is delivered (the promise's 153   // is the only place await_context is delivered (the promise's
152   // context_awaiter forwards it here). Returning false means 154   // context_awaiter forwards it here). Returning false means
153   // the coroutine does not actually suspend — it resumes 155   // the coroutine does not actually suspend — it resumes
154   // immediately — so the read still completes synchronously 156   // immediately — so the read still completes synchronously
155   // while having observed the stop token. See await_context, ContextAwaitable. 157   // while having observed the stop token. See await_context, ContextAwaitable.
156   bool 158   bool
HITCBC 157   439 await_suspend( 159   405 await_suspend(
158   std::coroutine_handle<>, 160   std::coroutine_handle<>,
159   await_context const* env) noexcept 161   await_context const* env) noexcept
160   { 162   {
HITCBC 161   439 canceled_ = env->stop_token.stop_requested(); 163   405 canceled_ = env->stop_token.stop_requested();
HITCBC 162   439 return false; 164   405 return false;
163   } 165   }
164   166  
165   [[nodiscard]] results<std::size_t> 167   [[nodiscard]] results<std::size_t>
HITCBC 166   439 await_resume() 168   405 await_resume()
167   { 169   {
168   // Empty buffer is a no-op regardless of 170   // Empty buffer is a no-op regardless of
169   // stream state, stop token, or fuse. 171   // stream state, stop token, or fuse.
HITCBC 170   439 if(buffers_empty(buffers_)) 172   405 if(buffers_empty(buffers_))
HITCBC 171   7 return {std::error_code(), 0}; 173   7 return {std::error_code(), 0};
172   174  
HITCBC 173   432 if(canceled_) 175   398 if(canceled_)
HITCBC 174   2 return {error::canceled, 0}; 176   2 return {error::canceled, 0};
175   177  
HITCBC 176   430 auto ec = self_->f_.maybe_fail(); 178   396 auto ec = self_->f_.maybe_fail();
HITCBC 177   339 if(ec) 179   305 if(ec)
HITCBC 178   91 return {ec, 0}; 180   91 return {ec, 0};
179   181  
HITCBC 180   248 if(self_->pos_ >= self_->data_.size()) 182   214 if(self_->pos_ >= self_->data_.size())
HITCBC 181   41 return {error::eof, 0}; 183   23 return {error::eof, 0};
182   184  
HITCBC 183   207 std::size_t avail = self_->data_.size() - self_->pos_; 185   191 std::size_t avail = self_->data_.size() - self_->pos_;
HITCBC 184   207 if(avail > self_->max_read_size_) 186   191 if(avail > self_->max_read_size_)
HITCBC 185   26 avail = self_->max_read_size_; 187   26 avail = self_->max_read_size_;
HITCBC 186   207 auto src = make_buffer(self_->data_.data() + self_->pos_, avail); 188   191 auto src = make_buffer(self_->data_.data() + self_->pos_, avail);
HITCBC 187   207 std::size_t const n = buffer_copy(buffers_, src); 189   191 std::size_t const n = buffer_copy(buffers_, src);
HITCBC 188   207 self_->pos_ += n; 190   191 self_->pos_ += n;
HITCBC 189   207 return {std::error_code(), n}; 191   191 return {std::error_code(), n};
190   } 192   }
191   }; 193   };
HITCBC 192   439 return awaitable{this, buffers}; 194   405 return awaitable{this, buffers};
193   } 195   }
194   }; 196   };
195   197  
196   } // test 198   } // test
197   } // capy 199   } // capy
198   } // boost 200   } // boost
199   201  
200   #endif 202   #endif