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