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_STREAM_HPP
11 : #define BOOST_CAPY_TEST_WRITE_STREAM_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 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 879 : consume_match_() noexcept
71 : {
72 879 : if(data_.empty() || expect_.empty())
73 879 : return {};
74 0 : std::size_t const n = (std::min)(data_.size(), expect_.size());
75 0 : if(std::string_view(data_.data(), n) !=
76 0 : std::string_view(expect_.data(), n))
77 0 : return error::test_failure;
78 0 : data_.erase(0, n);
79 0 : expect_.erase(0, n);
80 0 : 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 1097 : explicit write_stream(
92 : fuse f = {},
93 : std::size_t max_write_size = std::size_t(-1)) noexcept
94 1097 : : f_(std::move(f))
95 1097 : , max_write_size_(max_write_size)
96 : {
97 1097 : }
98 :
99 : /// Return the written data as a string view.
100 : std::string_view
101 922 : data() const noexcept
102 : {
103 922 : 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 : expect(std::string_view sv)
118 : {
119 : expect_.assign(sv);
120 : return consume_match_();
121 : }
122 :
123 : /// Return the number of bytes written.
124 : std::size_t
125 2 : size() const noexcept
126 : {
127 2 : 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 : @param buffers The const buffer sequence containing data to write.
147 :
148 : @return An awaitable yielding `(error_code,std::size_t)`.
149 :
150 : @see fuse
151 : */
152 : template<ConstBufferSequence CB>
153 : auto
154 1079 : write_some(CB buffers)
155 : {
156 : struct awaitable
157 : {
158 : write_stream* self_;
159 : CB buffers_;
160 :
161 1079 : bool await_ready() const noexcept { return true; }
162 :
163 : // This method is required to satisfy Capy's IoAwaitable concept,
164 : // but is never called because await_ready() returns true.
165 : //
166 : // Capy uses a two-layer awaitable system: the promise's
167 : // await_transform wraps awaitables in a transform_awaiter whose
168 : // standard await_suspend(coroutine_handle) calls this custom
169 : // 3-argument overload, passing the executor and stop_token from
170 : // the coroutine's context. For synchronous test awaitables like
171 : // this one, the coroutine never suspends, so this is not invoked.
172 : // The signature exists to allow the same awaitable type to work
173 : // with both synchronous (test) and asynchronous (real I/O) code.
174 0 : void await_suspend(
175 : coro,
176 : executor_ref,
177 : std::stop_token) const noexcept
178 : {
179 0 : }
180 :
181 : io_result<std::size_t>
182 1079 : await_resume()
183 : {
184 1079 : if(buffer_empty(buffers_))
185 0 : return {{}, 0};
186 :
187 1079 : auto ec = self_->f_.maybe_fail();
188 979 : if(ec)
189 100 : return {ec, 0};
190 :
191 879 : std::size_t n = buffer_size(buffers_);
192 879 : n = (std::min)(n, self_->max_write_size_);
193 :
194 879 : std::size_t const old_size = self_->data_.size();
195 879 : self_->data_.resize(old_size + n);
196 879 : buffer_copy(make_buffer(
197 879 : self_->data_.data() + old_size, n), buffers_, n);
198 :
199 879 : ec = self_->consume_match_();
200 879 : if(ec)
201 : {
202 0 : self_->data_.resize(old_size);
203 0 : return {ec, 0};
204 : }
205 :
206 879 : return {{}, n};
207 : }
208 : };
209 1079 : return awaitable{this, buffers};
210 : }
211 : };
212 :
213 : } // test
214 : } // capy
215 : } // boost
216 :
217 : #endif
|