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/await_context.hpp>
20 : #include <boost/capy/results.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 : /** Captures bytes passed to `write_some`, retrievable afterward through `data`.
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 : @par !example example
48 :
49 :
50 : @see @ref fuse, @ref WriteStream
51 : */
52 : class write_stream
53 : {
54 : fuse f_;
55 : std::string data_;
56 : std::string expect_;
57 : std::size_t max_write_size_;
58 :
59 : std::error_code
60 HIT 356 : consume_match_() noexcept
61 : {
62 356 : if(data_.empty() || expect_.empty())
63 339 : return {};
64 17 : std::size_t const n = (std::min)(data_.size(), expect_.size());
65 17 : if(std::string_view(data_.data(), n) !=
66 34 : std::string_view(expect_.data(), n))
67 4 : return error::test_failure;
68 13 : data_.erase(0, n);
69 13 : expect_.erase(0, n);
70 13 : return {};
71 : }
72 :
73 : public:
74 : /** Construct a write stream.
75 :
76 : @param f The fuse used to inject errors during writes.
77 :
78 : @param max_write_size Maximum bytes transferred per write.
79 : Use to simulate chunked network delivery.
80 : */
81 410 : explicit write_stream(
82 : fuse f = {},
83 : std::size_t max_write_size = std::size_t(-1)) noexcept
84 410 : : f_(std::move(f))
85 410 : , max_write_size_(max_write_size)
86 : {
87 410 : }
88 :
89 : /** Return the written data as a string view.
90 :
91 : @return A view of bytes written but not yet matched by @ref expect.
92 : */
93 : std::string_view
94 312 : data() const noexcept
95 : {
96 312 : return data_;
97 : }
98 :
99 : /** Set the expected data for subsequent writes.
100 :
101 : Stores the expected data and immediately tries to match
102 : against any data already written. Matched data is consumed
103 : from both buffers.
104 :
105 : @param sv The expected data.
106 :
107 : @return `error::test_failure` if existing data does not match;
108 : otherwise an empty error code.
109 : */
110 : std::error_code
111 31 : expect(std::string_view sv)
112 : {
113 31 : expect_.assign(sv);
114 31 : return consume_match_();
115 : }
116 :
117 : /** Return the number of bytes written.
118 :
119 : @return The number of bytes written but not yet matched by @ref expect.
120 : */
121 : std::size_t
122 5 : size() const noexcept
123 : {
124 5 : return data_.size();
125 : }
126 :
127 : /** Asynchronously write data to the stream.
128 :
129 : Transfers up to `buffer_size( buffers )` bytes from the provided
130 : const buffer sequence to the internal buffer. Before transferring
131 : data, the attached @ref fuse is consulted to possibly inject an
132 : error for testing fault scenarios. An empty buffer sequence or a
133 : canceled write completes without consulting the fuse. The
134 : returned `std::size_t` is the number of bytes transferred.
135 :
136 : @par Effects
137 : On success, appends the written bytes to the internal buffer.
138 : If an expectation was set with @ref expect, the matching prefix
139 : is then removed from both the buffer and the expectation. On a
140 : mismatch the write returns `error::test_failure`, transfers no
141 : bytes, and leaves the internal buffer unchanged. If an error is
142 : injected by the fuse, the internal buffer remains unchanged.
143 :
144 : @par Exception Safety
145 : Injected I/O conditions are reported via the `error_code`
146 : component of the result. Throws `std::system_error` when the
147 : attached @ref fuse is in exception mode and reaches its failure
148 : point. Throws `std::bad_alloc` if growing the internal buffer
149 : fails.
150 :
151 : @par Cancellation
152 : If the environment's stop token is requested, the write
153 : completes immediately with `error::canceled` and transfers no
154 : data. An empty buffer sequence is a no-op that completes
155 : successfully regardless of the stop token.
156 :
157 : @throws std::system_error When the attached @ref fuse is in
158 : exception mode and reaches its failure point.
159 :
160 : @throws std::bad_alloc If growing the internal buffer fails.
161 :
162 : @param buffers The const buffer sequence containing data to write.
163 :
164 : @return An awaitable that await-returns `(error_code,std::size_t)`.
165 :
166 : @see @ref fuse
167 : */
168 : template<ConstBufferSequence CB>
169 : auto
170 523 : write_some(CB buffers)
171 : {
172 : struct awaitable
173 : {
174 : write_stream* self_;
175 : CB buffers_;
176 : bool canceled_ = false;
177 :
178 523 : bool await_ready() const noexcept { return false; }
179 :
180 : // The operation completes synchronously, but await_suspend is
181 : // the only place await_context is delivered (the promise's
182 : // context_awaiter forwards it here). Returning false means
183 : // the coroutine does not actually suspend; it resumes
184 : // immediately, having observed the stop token. See await_context,
185 : // ContextAwaitable.
186 : bool
187 523 : await_suspend(
188 : std::coroutine_handle<>,
189 : await_context const* env) noexcept
190 : {
191 523 : canceled_ = env->stop_token.stop_requested();
192 523 : return false;
193 : }
194 :
195 : [[nodiscard]] results<std::size_t>
196 523 : await_resume()
197 : {
198 523 : if(buffers_empty(buffers_))
199 3 : return {std::error_code(), 0};
200 :
201 520 : if(canceled_)
202 1 : return {error::canceled, 0};
203 :
204 519 : auto ec = self_->f_.maybe_fail();
205 422 : if(ec)
206 97 : return {ec, 0};
207 :
208 325 : std::size_t n = buffer_size(buffers_);
209 325 : n = (std::min)(n, self_->max_write_size_);
210 :
211 325 : std::size_t const old_size = self_->data_.size();
212 325 : self_->data_.resize(old_size + n);
213 325 : buffer_copy(make_buffer(
214 325 : self_->data_.data() + old_size, n), buffers_, n);
215 :
216 325 : ec = self_->consume_match_();
217 325 : if(ec)
218 : {
219 2 : self_->data_.resize(old_size);
220 2 : return {ec, 0};
221 : }
222 :
223 323 : return {std::error_code(), n};
224 : }
225 : };
226 523 : return awaitable{this, buffers};
227 : }
228 : };
229 :
230 : } // test
231 : } // capy
232 : } // boost
233 :
234 : #endif
|