72.00% Lines (126/175) 100.00% Functions (16/16)
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_FUSE_HPP 11   #ifndef BOOST_CAPY_TEST_FUSE_HPP
12   #define BOOST_CAPY_TEST_FUSE_HPP 12   #define BOOST_CAPY_TEST_FUSE_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/concept/context_runnable.hpp> 15   #include <boost/capy/concept/context_runnable.hpp>
16   #include <boost/capy/error.hpp> 16   #include <boost/capy/error.hpp>
17   #include <boost/capy/test/run_blocking.hpp> 17   #include <boost/capy/test/run_blocking.hpp>
18   #include <system_error> 18   #include <system_error>
19   #include <concepts> 19   #include <concepts>
20   #include <cstddef> 20   #include <cstddef>
21   #include <exception> 21   #include <exception>
22   #include <limits> 22   #include <limits>
23   #include <memory> 23   #include <memory>
24   #include <source_location> 24   #include <source_location>
25   #include <type_traits> 25   #include <type_traits>
26   26  
27   /* 27   /*
28   LLM/AI Instructions for fuse-based test patterns: 28   LLM/AI Instructions for fuse-based test patterns:
29   29  
30   When f.armed() runs a test, it injects errors at successive points 30   When f.armed() runs a test, it injects errors at successive points
31   via maybe_fail(). Operations like read_stream::read_some() and 31   via maybe_fail(). Operations like read_stream::read_some() and
32   write_stream::write_some() call maybe_fail() internally. 32   write_stream::write_some() call maybe_fail() internally.
33   33  
34   CORRECT pattern - early return on injected error: 34   CORRECT pattern - early return on injected error:
35   35  
36   auto [ec, n] = co_await rs.read_some(buf); 36   auto [ec, n] = co_await rs.read_some(buf);
37   if(ec) 37   if(ec)
38   co_return; // fuse injected error, exit gracefully 38   co_return; // fuse injected error, exit gracefully
39   // ... continue with success path 39   // ... continue with success path
40   40  
41   WRONG pattern - asserting success unconditionally: 41   WRONG pattern - asserting success unconditionally:
42   42  
43   auto [ec, n] = co_await rs.read_some(buf); 43   auto [ec, n] = co_await rs.read_some(buf);
44   BOOST_TEST(! ec); // FAILS when fuse injects error! 44   BOOST_TEST(! ec); // FAILS when fuse injects error!
45   45  
46   The fuse mechanism tests error handling by failing at each point 46   The fuse mechanism tests error handling by failing at each point
47   in sequence. Tests must handle injected errors by returning early, 47   in sequence. Tests must handle injected errors by returning early,
48   not by asserting that operations always succeed. 48   not by asserting that operations always succeed.
49   */ 49   */
50   50  
51   namespace boost { 51   namespace boost {
52   namespace capy { 52   namespace capy {
53   namespace test { 53   namespace test {
54   54  
55   /** Reruns a code path, injecting a failure at one later point on each pass. 55   /** Reruns a code path, injecting a failure at one later point on each pass.
56   56  
57   This class enables exhaustive testing of error handling 57   This class enables exhaustive testing of error handling
58   paths by injecting failures at successive points in code. 58   paths by injecting failures at successive points in code.
59   Each iteration fails at a later point until the code path 59   Each iteration fails at a later point until the code path
60   completes without encountering a failure. The @ref armed 60   completes without encountering a failure. The @ref armed
61   method runs in two phases: first with error codes, then 61   method runs in two phases: first with error codes, then
62   with exceptions. The @ref inert method runs once without 62   with exceptions. The @ref inert method runs once without
63   automatic failure injection. 63   automatic failure injection.
64   64  
65   @par Thread Safety 65   @par Thread Safety
66   66  
67   @b Not @b thread @b safe. Instances must not be accessed 67   @b Not @b thread @b safe. Instances must not be accessed
68   from different logical threads of operation concurrently. 68   from different logical threads of operation concurrently.
69   This includes coroutines - accessing the same fuse from 69   This includes coroutines - accessing the same fuse from
70   multiple concurrent coroutines causes non-deterministic 70   multiple concurrent coroutines causes non-deterministic
71   test behavior. 71   test behavior.
72   72  
  73 + @note A fuse supports a single @ref armed or @ref inert run.
  74 + Copies share the same state. A second run on the same state
  75 + may skip error-code injection. After any @ref fail call, every
  76 + later run on that state reports failure. Construct a new fuse
  77 + for each run.
  78 +
73   @par Basic Inline Usage 79   @par Basic Inline Usage
74   80  
75   @par !example example_1 81   @par !example example_1
76   82  
77   83  
78   @par Named Fuse with armed() 84   @par Named Fuse with armed()
79   85  
80   @par !example example_2 86   @par !example example_2
81   87  
82   88  
83   @par Using inert() for Single-Run Tests 89   @par Using inert() for Single-Run Tests
84   90  
85   @par !example example_3 91   @par !example example_3
86   92  
87   93  
88   @par Dependency Injection (Standalone Usage) 94   @par Dependency Injection (Standalone Usage)
89   95  
90   A default-constructed fuse is a no-op when used outside 96   A default-constructed fuse is a no-op when used outside
91   of @ref armed or @ref inert. This enables passing a fuse 97   of @ref armed or @ref inert. This enables passing a fuse
92   to classes for dependency injection without affecting 98   to classes for dependency injection without affecting
93   normal operation. 99   normal operation.
94   100  
95   @par !example example_4 101   @par !example example_4
96   102  
97   103  
98   @par Custom Error Code 104   @par Custom Error Code
99   105  
100   @par !example example_5 106   @par !example example_5
101   107  
102   108  
103   @par Checking the Result 109   @par Checking the Result
104   110  
105   @par !example example_6 111   @par !example example_6
106   112  
107   113  
108   @par Test Framework Integration 114   @par Test Framework Integration
109   115  
110   @par !example example_7 116   @par !example example_7
111   117  
112   */ 118   */
113   class fuse 119   class fuse
114   { 120   {
115   struct state 121   struct state
116   { 122   {
117   std::size_t n = (std::numeric_limits<std::size_t>::max)(); 123   std::size_t n = (std::numeric_limits<std::size_t>::max)();
118   std::size_t i = 0; 124   std::size_t i = 0;
119   bool triggered = false; 125   bool triggered = false;
120   bool throws = false; 126   bool throws = false;
121   bool stopped = false; 127   bool stopped = false;
122   bool inert = true; 128   bool inert = true;
123   std::error_code ec; 129   std::error_code ec;
124   std::source_location loc; 130   std::source_location loc;
125   std::exception_ptr ep; 131   std::exception_ptr ep;
126   }; 132   };
127   133  
128   std::shared_ptr<state> p_; 134   std::shared_ptr<state> p_;
129   135  
130   /** Return true if testing should continue. 136   /** Return true if testing should continue.
131   137  
132   On the first call, initializes the failure point to 0. 138   On the first call, initializes the failure point to 0.
133   After a triggered failure, increments the failure point 139   After a triggered failure, increments the failure point
134   and resets for the next iteration. Returns false when 140   and resets for the next iteration. Returns false when
135   the test completes without triggering a failure. 141   the test completes without triggering a failure.
136   */ 142   */
HITCBC 137   1326 explicit operator bool() const noexcept 143   1326 explicit operator bool() const noexcept
138   { 144   {
HITCBC 139   1326 auto& s = *p_; 145   1326 auto& s = *p_;
HITCBC 140   1326 if(s.n == (std::numeric_limits<std::size_t>::max)()) 146   1326 if(s.n == (std::numeric_limits<std::size_t>::max)())
141   { 147   {
142   // First call: start round 0 148   // First call: start round 0
HITCBC 143   313 s.n = 0; 149   313 s.n = 0;
HITCBC 144   313 return true; 150   313 return true;
145   } 151   }
HITCBC 146   1013 if(s.triggered) 152   1013 if(s.triggered)
147   { 153   {
148   // Previous round triggered, try next failure point 154   // Previous round triggered, try next failure point
HITCBC 149   707 s.n++; 155   707 s.n++;
HITCBC 150   707 s.i = 0; 156   707 s.i = 0;
HITCBC 151   707 s.triggered = false; 157   707 s.triggered = false;
HITCBC 152   707 return true; 158   707 return true;
153   } 159   }
154   // Test completed without trigger: success 160   // Test completed without trigger: success
HITCBC 155   306 return false; 161   306 return false;
156   } 162   }
157   163  
158   public: 164   public:
159   /** Converts to `bool`, reporting success, and carries the failure point on failure. 165   /** Converts to `bool`, reporting success, and carries the failure point on failure.
160   166  
161   Contains the outcome of @ref armed or @ref inert 167   Contains the outcome of @ref armed or @ref inert
162   and, on failure, the source location of the failing 168   and, on failure, the source location of the failing
163   point. Converts to `bool` for convenient success 169   point. Converts to `bool` for convenient success
164   checking. 170   checking.
165   171  
166   @par Example 172   @par Example
167   173  
168   @par !example example 174   @par !example example
169   175  
170   */ 176   */
171   struct result 177   struct result
172   { 178   {
173   /// Source location of the failing point, set only on failure. 179   /// Source location of the failing point, set only on failure.
174   std::source_location loc = {}; 180   std::source_location loc = {};
175   181  
176 - /// Exception captured by @ref fail, or null if none. 182 + /** Exception associated with the failure, or null if none.
  183 +
  184 + Holds the exception passed to @ref fail. Under @ref inert
  185 + it also holds an exception that escaped the test function.
  186 + Under @ref armed an escaping exception is not stored.
  187 + */
177   std::exception_ptr ep = nullptr; 188   std::exception_ptr ep = nullptr;
178   189  
179   /// True if the test completed without a failure. 190   /// True if the test completed without a failure.
180   bool success = true; 191   bool success = true;
181   192  
182   /** Return whether the test completed without a failure. 193   /** Return whether the test completed without a failure.
183   194  
184   @return @ref success. 195   @return @ref success.
185   */ 196   */
HITCBC 186   42 constexpr explicit operator bool() const noexcept 197   42 constexpr explicit operator bool() const noexcept
187   { 198   {
HITCBC 188   42 return success; 199   42 return success;
189   } 200   }
190   }; 201   };
191   202  
192   /** Construct a fuse with a custom error code. 203   /** Construct a fuse with a custom error code.
193   204  
194   @par Example 205   @par Example
195   206  
196   @par !example example_1 207   @par !example example_1
197   208  
198   209  
199   @param ec The error code to deliver at failure points. 210   @param ec The error code to deliver at failure points.
200   */ 211   */
HITCBC 201   258 explicit fuse(std::error_code ec) 212   259 explicit fuse(std::error_code ec)
HITCBC 202   258 : p_(std::make_shared<state>()) 213   259 : p_(std::make_shared<state>())
203   { 214   {
HITCBC 204   258 p_->ec = ec; 215   259 p_->ec = ec;
HITCBC 205   258 } 216   259 }
206   217  
207   /** Construct a fuse with the default error code. 218   /** Construct a fuse with the default error code.
208   219  
209   The default error code is `error::test_failure`. 220   The default error code is `error::test_failure`.
210   221  
211   @par Example 222   @par Example
212   223  
213   @par !example example_2 224   @par !example example_2
214   225  
215   */ 226   */
HITCBC 216   255 fuse() 227   256 fuse()
HITCBC 217   255 : fuse(error::test_failure) 228   256 : fuse(error::test_failure)
218   { 229   {
HITCBC 219   255 } 230   256 }
220   231  
221   /** Return an error or throw at the current failure point. 232   /** Return an error or throw at the current failure point.
222   233  
223   When running under @ref armed, increments the internal 234   When running under @ref armed, increments the internal
224   counter. When the counter reaches the current failure 235   counter. When the counter reaches the current failure
225   point, returns the stored error code (or throws 236   point, returns the stored error code (or throws
226   `std::system_error` in exception mode) and records 237   `std::system_error` in exception mode) and records
227   the source location. 238   the source location.
228   239  
229   When called outside of @ref armed or @ref inert (standalone 240   When called outside of @ref armed or @ref inert (standalone
230   usage), or when running under @ref inert, always returns 241   usage), or when running under @ref inert, always returns
231   an empty error code. This enables dependency injection 242   an empty error code. This enables dependency injection
232   where the fuse is a no-op in production code. 243   where the fuse is a no-op in production code.
233   244  
234   @par Example 245   @par Example
235   246  
236   @par !example example_1 247   @par !example example_1
237   248  
238   249  
239   @par Standalone Usage 250   @par Standalone Usage
240   251  
241   @par !example example_2 252   @par !example example_2
242   253  
243   254  
244   @param loc The source location of the call site, 255   @param loc The source location of the call site,
245   captured automatically. 256   captured automatically.
246 - @return The stored error code if at the failure point,  
247 - otherwise an empty error code. In exception mode,  
248 - throws instead of returning an error. When called  
249 - outside @ref armed, or when running under @ref inert,  
250 - always returns an empty error code.  
251 -  
252   257  
253   @throws std::system_error When in exception mode 258   @throws std::system_error When in exception mode
254   and at the failure point (not thrown outside @ref armed). 259   and at the failure point (not thrown outside @ref armed).
  260 +
  261 + @return The stored error code at the failure point, otherwise
  262 + an empty error code.
255   */ 263   */
256   std::error_code 264   std::error_code
HITCBC 257   1734 maybe_fail( 265   1749 maybe_fail(
258   std::source_location loc = std::source_location::current()) 266   std::source_location loc = std::source_location::current())
259   { 267   {
HITCBC 260   1734 auto& s = *p_; 268   1749 auto& s = *p_;
HITCBC 261   1734 if(s.inert) 269   1749 if(s.inert)
HITCBC 262   311 return {}; 270   326 return {};
HITCBC 263   1423 if(s.i < s.n) 271   1423 if(s.i < s.n)
HITCBC 264   1152 ++s.i; 272   1152 ++s.i;
HITCBC 265   1423 if(s.i == s.n) 273   1423 if(s.i == s.n)
266   { 274   {
HITCBC 267   707 s.triggered = true; 275   707 s.triggered = true;
HITCBC 268   707 s.loc = loc; 276   707 s.loc = loc;
HITCBC 269   707 if(s.throws) 277   707 if(s.throws)
HITCBC 270   347 throw std::system_error(s.ec); 278   347 throw std::system_error(s.ec);
HITCBC 271   360 return s.ec; 279   360 return s.ec;
272   } 280   }
HITCBC 273   716 return {}; 281   716 return {};
274   } 282   }
275   283  
276 - /** Signal a test failure and stop execution. 284 + /** Record a test failure.
277   285  
278   Call this from the test function to indicate a failure 286   Call this from the test function to indicate a failure
279 - condition. Both @ref armed and @ref inert return 287 + condition. The call only records the failure: the test
280 - a failed @ref result immediately. 288 + function keeps running, so return from it right after the
  289 + call. Once the test function returns, @ref armed or
  290 + @ref inert reports a failed @ref result and runs no
  291 + further rounds.
281   292  
282   @par Example 293   @par Example
283   294  
284   @par !example example_1 295   @par !example example_1
285   296  
286   297  
287   @param loc The source location of the call site, 298   @param loc The source location of the call site,
288   captured automatically. 299   captured automatically.
289   */ 300   */
290   void 301   void
HITCBC 291   3 fail( 302   3 fail(
292   std::source_location loc = 303   std::source_location loc =
293   std::source_location::current()) noexcept 304   std::source_location::current()) noexcept
294   { 305   {
HITCBC 295   3 p_->loc = loc; 306   3 p_->loc = loc;
HITCBC 296   3 p_->stopped = true; 307   3 p_->stopped = true;
HITCBC 297   3 } 308   3 }
298   309  
299 - /** Signal a test failure with an exception and stop execution. 310 + /** Record a test failure with an associated exception.
300   311  
301   Call this from the test function to indicate a failure 312   Call this from the test function to indicate a failure
302 - condition with an associated exception. Both @ref armed 313 + condition with an associated exception. The call only
303 - and @ref inert return a failed @ref result with 314 + records the failure: the test function keeps running, so
304 - the captured exception pointer. 315 + return from it right after the call. Once the test function
  316 + returns, @ref armed or @ref inert reports a failed
  317 + @ref result holding `ep`.
305   318  
306   @par Example 319   @par Example
307   320  
308   @par !example example_2 321   @par !example example_2
309   322  
310   323  
311   @param ep The exception pointer to capture. 324   @param ep The exception pointer to capture.
312   325  
313   @param loc The source location of the call site, 326   @param loc The source location of the call site,
314   captured automatically. 327   captured automatically.
315   */ 328   */
316   void 329   void
HITCBC 317   2 fail( 330   2 fail(
318   std::exception_ptr ep, 331   std::exception_ptr ep,
319   std::source_location loc = 332   std::source_location loc =
320   std::source_location::current()) noexcept 333   std::source_location::current()) noexcept
321   { 334   {
HITCBC 322   2 p_->ep = ep; 335   2 p_->ep = ep;
HITCBC 323   2 p_->loc = loc; 336   2 p_->loc = loc;
HITCBC 324   2 p_->stopped = true; 337   2 p_->stopped = true;
HITCBC 325   2 } 338   2 }
326   339  
327   private: 340   private:
328   /* Drive the two-phase armed loop, invoking `do_iter` once per round. 341   /* Drive the two-phase armed loop, invoking `do_iter` once per round.
329   342  
330   Phase 1 delivers injected failures as error codes; phase 2 as 343   Phase 1 delivers injected failures as error codes; phase 2 as
331   exceptions. Shared by the two coroutine `armed` overloads: each 344   exceptions. Shared by the two coroutine `armed` overloads: each
332   supplies a nullary `do_iter` that runs one iteration — via 345   supplies a nullary `do_iter` that runs one iteration — via
333   @ref run_blocking, or via a caller-supplied runner — so the round 346   @ref run_blocking, or via a caller-supplied runner — so the round
334   sequence and failure handling stay identical across them. 347   sequence and failure handling stay identical across them.
335   */ 348   */
336   template<class DoIter> 349   template<class DoIter>
337   result 350   result
HITCBC 338   134 run_phases(DoIter&& do_iter) 351   134 run_phases(DoIter&& do_iter)
339   { 352   {
HITCBC 340   134 result r; 353   134 result r;
341   354  
342   // Phase 1: error code mode 355   // Phase 1: error code mode
HITCBC 343   134 p_->throws = false; 356   134 p_->throws = false;
HITCBC 344   134 p_->inert = false; 357   134 p_->inert = false;
HITCBC 345   134 p_->n = (std::numeric_limits<std::size_t>::max)(); 358   134 p_->n = (std::numeric_limits<std::size_t>::max)();
HITCBC 346   581 while(*this) 359   581 while(*this)
347   { 360   {
348   try 361   try
349   { 362   {
HITCBC 350   448 do_iter(); 363   448 do_iter();
351   } 364   }
HITCBC 352   2 catch(...) 365   2 catch(...)
353   { 366   {
HITCBC 354   1 r.success = false; 367   1 r.success = false;
HITCBC 355   1 r.loc = p_->loc; 368   1 r.loc = p_->loc;
HITCBC 356   1 r.ep = p_->ep; 369   1 r.ep = p_->ep;
HITCBC 357   1 p_->inert = true; 370   1 p_->inert = true;
HITCBC 358   1 return r; 371   1 return r;
359   } 372   }
HITCBC 360   447 if(p_->stopped) 373   447 if(p_->stopped)
361   { 374   {
MISUBC 362   ✗ r.success = false; 375   ✗ r.success = false;
MISUBC 363   ✗ r.loc = p_->loc; 376   ✗ r.loc = p_->loc;
MISUBC 364   ✗ r.ep = p_->ep; 377   ✗ r.ep = p_->ep;
MISUBC 365   ✗ p_->inert = true; 378   ✗ p_->inert = true;
MISUBC 366   ✗ return r; 379   ✗ return r;
367   } 380   }
368   } 381   }
369   382  
370   // Phase 2: exception mode 383   // Phase 2: exception mode
HITCBC 371   133 p_->throws = true; 384   133 p_->throws = true;
HITCBC 372   133 p_->n = (std::numeric_limits<std::size_t>::max)(); 385   133 p_->n = (std::numeric_limits<std::size_t>::max)();
HITCBC 373   133 p_->i = 0; 386   133 p_->i = 0;
HITCBC 374   133 p_->triggered = false; 387   133 p_->triggered = false;
HITCBC 375   578 while(*this) 388   578 while(*this)
376   { 389   {
377   try 390   try
378   { 391   {
HITCBC 379   445 do_iter(); 392   445 do_iter();
380   } 393   }
HITCBC 381   624 catch(std::system_error const& ex) 394   624 catch(std::system_error const& ex)
382   { 395   {
HITCBC 383   312 if(ex.code() != p_->ec) 396   312 if(ex.code() != p_->ec)
384   { 397   {
MISUBC 385   ✗ r.success = false; 398   ✗ r.success = false;
MISUBC 386   ✗ r.loc = p_->loc; 399   ✗ r.loc = p_->loc;
MISUBC 387   ✗ r.ep = p_->ep; 400   ✗ r.ep = p_->ep;
MISUBC 388   ✗ p_->inert = true; 401   ✗ p_->inert = true;
MISUBC 389   ✗ return r; 402   ✗ return r;
390   } 403   }
391   } 404   }
MISUBC 392   ✗ catch(...) 405   ✗ catch(...)
393   { 406   {
MISUBC 394   ✗ r.success = false; 407   ✗ r.success = false;
MISUBC 395   ✗ r.loc = p_->loc; 408   ✗ r.loc = p_->loc;
MISUBC 396   ✗ r.ep = p_->ep; 409   ✗ r.ep = p_->ep;
MISUBC 397   ✗ p_->inert = true; 410   ✗ p_->inert = true;
MISUBC 398   ✗ return r; 411   ✗ return r;
399   } 412   }
HITCBC 400   445 if(p_->stopped) 413   445 if(p_->stopped)
401   { 414   {
MISUBC 402   ✗ r.success = false; 415   ✗ r.success = false;
MISUBC 403   ✗ r.loc = p_->loc; 416   ✗ r.loc = p_->loc;
MISUBC 404   ✗ r.ep = p_->ep; 417   ✗ r.ep = p_->ep;
MISUBC 405   ✗ p_->inert = true; 418   ✗ p_->inert = true;
MISUBC 406   ✗ return r; 419   ✗ return r;
407   } 420   }
408   } 421   }
HITCBC 409   133 p_->inert = true; 422   133 p_->inert = true;
HITCBC 410   133 return r; 423   133 return r;
MISUBC 411   ✗ } 424   ✗ }
412   425  
413   public: 426   public:
414   /** Run a test function with systematic failure injection. 427   /** Run a test function with systematic failure injection.
415   428  
416   Repeatedly invokes the provided function, failing at 429   Repeatedly invokes the provided function, failing at
417   successive points until the function completes without 430   successive points until the function completes without
418   encountering a failure. First runs the complete loop 431   encountering a failure. First runs the complete loop
419   using error codes, then runs using exceptions. 432   using error codes, then runs using exceptions.
420   433  
  434 + In the error-code phase, any exception escaping `fn` ends the
  435 + run with a failed @ref result. In the exception phase, a
  436 + `std::system_error` carrying the fuse's error code counts as an
  437 + injected failure, and the next round starts. Any other
  438 + exception ends the run with a failed @ref result. An exception
  439 + escaping `fn` is not stored in `result::ep`.
  440 +
  441 + On failure, `result::loc` holds the location of the last
  442 + triggered @ref maybe_fail or of the @ref fail call.
  443 +
421   @par Example 444   @par Example
422   445  
423   @par !example example_2 446   @par !example example_2
424   447  
425   448  
426   @param fn The test function to invoke. It receives 449   @param fn The test function to invoke. It receives
427   a reference to the fuse and should call @ref maybe_fail 450   a reference to the fuse and should call @ref maybe_fail
428   at each potential failure point. 451   at each potential failure point.
429   452  
430 - On failure, `result::loc` contains the source location  
431 - of the last @ref maybe_fail or @ref fail call.  
432   @return A @ref result indicating success or failure. 453   @return A @ref result indicating success or failure.
433   */ 454   */
434   template<class F> 455   template<class F>
435   result 456   result
HITCBC 436   26 armed(F&& fn) 457   26 armed(F&& fn)
437   { 458   {
HITCBC 438   26 result r; 459   26 result r;
439   460  
440   // Phase 1: error code mode 461   // Phase 1: error code mode
HITCBC 441   26 p_->throws = false; 462   26 p_->throws = false;
HITCBC 442   26 p_->inert = false; 463   26 p_->inert = false;
HITCBC 443   26 p_->n = (std::numeric_limits<std::size_t>::max)(); 464   26 p_->n = (std::numeric_limits<std::size_t>::max)();
HITCBC 444   92 while(*this) 465   92 while(*this)
445   { 466   {
446   try 467   try
447   { 468   {
HITCBC 448   72 fn(*this); 469   72 fn(*this);
449   } 470   }
HITCBC 450   6 catch(...) 471   6 catch(...)
451   { 472   {
HITCBC 452   3 r.success = false; 473   3 r.success = false;
HITCBC 453   3 r.loc = p_->loc; 474   3 r.loc = p_->loc;
HITCBC 454   3 r.ep = p_->ep; 475   3 r.ep = p_->ep;
HITCBC 455   3 p_->inert = true; 476   3 p_->inert = true;
HITCBC 456   3 return r; 477   3 return r;
457   } 478   }
HITCBC 458   69 if(p_->stopped) 479   69 if(p_->stopped)
459   { 480   {
HITCBC 460   3 r.success = false; 481   3 r.success = false;
HITCBC 461   3 r.loc = p_->loc; 482   3 r.loc = p_->loc;
HITCBC 462   3 r.ep = p_->ep; 483   3 r.ep = p_->ep;
HITCBC 463   3 p_->inert = true; 484   3 p_->inert = true;
HITCBC 464   3 return r; 485   3 return r;
465   } 486   }
466   } 487   }
467   488  
468   // Phase 2: exception mode 489   // Phase 2: exception mode
HITCBC 469   20 p_->throws = true; 490   20 p_->throws = true;
HITCBC 470   20 p_->n = (std::numeric_limits<std::size_t>::max)(); 491   20 p_->n = (std::numeric_limits<std::size_t>::max)();
HITCBC 471   20 p_->i = 0; 492   20 p_->i = 0;
HITCBC 472   20 p_->triggered = false; 493   20 p_->triggered = false;
HITCBC 473   75 while(*this) 494   75 while(*this)
474   { 495   {
475   try 496   try
476   { 497   {
HITCBC 477   55 fn(*this); 498   55 fn(*this);
478   } 499   }
HITCBC 479   70 catch(std::system_error const& ex) 500   70 catch(std::system_error const& ex)
480   { 501   {
HITCBC 481   35 if(ex.code() != p_->ec) 502   35 if(ex.code() != p_->ec)
482   { 503   {
MISUBC 483   ✗ r.success = false; 504   ✗ r.success = false;
MISUBC 484   ✗ r.loc = p_->loc; 505   ✗ r.loc = p_->loc;
MISUBC 485   ✗ r.ep = p_->ep; 506   ✗ r.ep = p_->ep;
MISUBC 486   ✗ p_->inert = true; 507   ✗ p_->inert = true;
MISUBC 487   ✗ return r; 508   ✗ return r;
488   } 509   }
489   } 510   }
MISUBC 490   ✗ catch(...) 511   ✗ catch(...)
491   { 512   {
MISUBC 492   ✗ r.success = false; 513   ✗ r.success = false;
MISUBC 493   ✗ r.loc = p_->loc; 514   ✗ r.loc = p_->loc;
MISUBC 494   ✗ r.ep = p_->ep; 515   ✗ r.ep = p_->ep;
MISUBC 495   ✗ p_->inert = true; 516   ✗ p_->inert = true;
MISUBC 496   ✗ return r; 517   ✗ return r;
497   } 518   }
HITCBC 498   55 if(p_->stopped) 519   55 if(p_->stopped)
499   { 520   {
MISUBC 500   ✗ r.success = false; 521   ✗ r.success = false;
MISUBC 501   ✗ r.loc = p_->loc; 522   ✗ r.loc = p_->loc;
MISUBC 502   ✗ r.ep = p_->ep; 523   ✗ r.ep = p_->ep;
MISUBC 503   ✗ p_->inert = true; 524   ✗ p_->inert = true;
MISUBC 504   ✗ return r; 525   ✗ return r;
505   } 526   }
506   } 527   }
HITCBC 507   20 p_->inert = true; 528   20 p_->inert = true;
HITCBC 508   20 return r; 529   20 return r;
MISUBC 509   ✗ } 530   ✗ }
510   531  
511   /** Run a coroutine test function with systematic failure injection. 532   /** Run a coroutine test function with systematic failure injection.
512   533  
513   Repeatedly invokes the provided coroutine function, failing at 534   Repeatedly invokes the provided coroutine function, failing at
514   successive points until the function completes without 535   successive points until the function completes without
515   encountering a failure. First runs the complete loop 536   encountering a failure. First runs the complete loop
516   using error codes, then runs using exceptions. 537   using error codes, then runs using exceptions.
517   538  
518   This overload handles lambdas that return an @ref ContextRunnable 539   This overload handles lambdas that return an @ref ContextRunnable
519   (such as `task<void>`), executing them synchronously via 540   (such as `task<void>`), executing them synchronously via
520   @ref run_blocking. 541   @ref run_blocking.
521   542  
  543 + In the error-code phase, any exception escaping `fn` ends the
  544 + run with a failed @ref result. In the exception phase, a
  545 + `std::system_error` carrying the fuse's error code counts as an
  546 + injected failure, and the next round starts. Any other
  547 + exception ends the run with a failed @ref result. An exception
  548 + escaping `fn` is not stored in `result::ep`.
  549 +
  550 + On failure, `result::loc` holds the location of the last
  551 + triggered @ref maybe_fail or of the @ref fail call.
  552 +
522   @par Example 553   @par Example
523   554  
524   @par !example example_3 555   @par !example example_3
525   556  
526   557  
527   @param fn The coroutine test function to invoke. It receives 558   @param fn The coroutine test function to invoke. It receives
528   a reference to the fuse and should call @ref maybe_fail 559   a reference to the fuse and should call @ref maybe_fail
529   at each potential failure point. 560   at each potential failure point.
530   561  
531 - On failure, `result::loc` contains the source location  
532 - of the last @ref maybe_fail or @ref fail call.  
533   @return A @ref result indicating success or failure. 562   @return A @ref result indicating success or failure.
534   */ 563   */
535   template<class F> 564   template<class F>
536   requires ContextRunnable<std::invoke_result_t<F, fuse&>> 565   requires ContextRunnable<std::invoke_result_t<F, fuse&>>
537   result 566   result
HITCBC 538   131 armed(F&& fn) 567   131 armed(F&& fn)
539   { 568   {
HITCBC 540   1445 return run_phases([&]{ run_blocking()(fn(*this)); }); 569   1445 return run_phases([&]{ run_blocking()(fn(*this)); });
541   } 570   }
542   571  
543   /** Run a coroutine test function on a caller-supplied runner. 572   /** Run a coroutine test function on a caller-supplied runner.
544   573  
545   Behaves like the @ref ContextRunnable overload of @ref armed, but 574   Behaves like the @ref ContextRunnable overload of @ref armed, but
546   instead of driving each iteration through @ref run_blocking, it 575   instead of driving each iteration through @ref run_blocking, it
547   hands the coroutine to `run_one`. This lets a caller run each 576   hands the coroutine to `run_one`. This lets a caller run each
548   iteration on any execution context it chooses. Operations built 577   iteration on any execution context it chooses. Operations built
549   on `corosio::timeout` or `corosio::delay` in particular require 578   on `corosio::timeout` or `corosio::delay` in particular require
550   an `io_context`, because they abort on a non-`io_context` 579   an `io_context`, because they abort on a non-`io_context`
551   executor. `fuse` never learns about the context; 580   executor. `fuse` never learns about the context;
552   the caller owns the drive loop. 581   the caller owns the drive loop.
553   582  
554   @par Runner contract 583   @par Runner contract
555   `run_one` is invoked once per round with the @ref ContextRunnable 584   `run_one` is invoked once per round with the @ref ContextRunnable
556   produced by `fn`. It must run that task to completion 585   produced by `fn`. It must run that task to completion
557   synchronously and *return* any exception the task raised as a 586   synchronously and *return* any exception the task raised as a
558   `std::exception_ptr` (null on success). It must not rethrow. 587   `std::exception_ptr` (null on success). It must not rethrow.
559   `armed` rethrows the returned pointer from its own synchronous 588   `armed` rethrows the returned pointer from its own synchronous
560   code, so the exception phase observes injected failures. An 589   code, so the exception phase observes injected failures. An
561   exception escaping a `run_async` completion handler would 590   exception escaping a `run_async` completion handler would
562   instead call `std::terminate`. Capture the exception in the error 591   instead call `std::terminate`. Capture the exception in the error
563   handler and return it once the run loop is done. 592   handler and return it once the run loop is done.
564   593  
565   @par Example 594   @par Example
566   @par !example example_1 595   @par !example example_1
567   596  
568   597  
569   @param run_one A callable invoked with each iteration's task; it 598   @param run_one A callable invoked with each iteration's task; it
570   runs the task to completion and returns any escaped exception 599   runs the task to completion and returns any escaped exception
571   (null on success) without rethrowing. 600   (null on success) without rethrowing.
572   601  
573   @param fn The coroutine test function to invoke. 602   @param fn The coroutine test function to invoke.
574   603  
575   @return A @ref result indicating success or failure. 604   @return A @ref result indicating success or failure.
576   */ 605   */
577   template<class Runner, class F> 606   template<class Runner, class F>
578   requires ContextRunnable<std::invoke_result_t<F, fuse&>> 607   requires ContextRunnable<std::invoke_result_t<F, fuse&>>
579   && std::same_as< 608   && std::same_as<
580   std::invoke_result_t<Runner&, std::invoke_result_t<F, fuse&>>, 609   std::invoke_result_t<Runner&, std::invoke_result_t<F, fuse&>>,
581   std::exception_ptr> 610   std::exception_ptr>
582   result 611   result
HITCBC 583   3 armed(Runner&& run_one, F&& fn) 612   3 armed(Runner&& run_one, F&& fn)
584   { 613   {
HITCBC 585   14 return run_phases([&]{ 614   14 return run_phases([&]{
HITCBC 586   23 if(auto ep = run_one(fn(*this))) 615   23 if(auto ep = run_one(fn(*this)))
HITCBC 587   12 std::rethrow_exception(ep); 616   12 std::rethrow_exception(ep);
HITCBC 588   6 }); 617   6 });
589   } 618   }
590   619  
591   /** Alias for @ref armed. 620   /** Alias for @ref armed.
592   621  
593   Allows the fuse to be invoked directly as a function 622   Allows the fuse to be invoked directly as a function
594   object for more concise syntax. 623   object for more concise syntax.
595   624  
596   @par Example 625   @par Example
597   626  
598   @par !example example 627   @par !example example
599   628  
600   629  
601   @param fn The test function to run under failure injection. 630   @param fn The test function to run under failure injection.
602   631  
603   @return The @ref result of the armed run. 632   @return The @ref result of the armed run.
604   633  
605   @see @ref armed 634   @see @ref armed
606   */ 635   */
607   template<class F> 636   template<class F>
608   result 637   result
HITCBC 609   15 operator()(F&& fn) 638   15 operator()(F&& fn)
610   { 639   {
HITCBC 611   15 return armed(std::forward<F>(fn)); 640   15 return armed(std::forward<F>(fn));
612   } 641   }
613   642  
614   /** Alias for @ref armed (coroutine overload). 643   /** Alias for @ref armed (coroutine overload).
615   644  
616   @param fn The test coroutine factory to run under failure injection. 645   @param fn The test coroutine factory to run under failure injection.
617   646  
618   @return The @ref result of the armed run. 647   @return The @ref result of the armed run.
619   648  
620   @see @ref armed 649   @see @ref armed
621   */ 650   */
622   template<class F> 651   template<class F>
623   requires ContextRunnable<std::invoke_result_t<F, fuse&>> 652   requires ContextRunnable<std::invoke_result_t<F, fuse&>>
624   result 653   result
625   operator()(F&& fn) 654   operator()(F&& fn)
626   { 655   {
627   return armed(std::forward<F>(fn)); 656   return armed(std::forward<F>(fn));
628   } 657   }
629   658  
630   /** Run a test function once without failure injection. 659   /** Run a test function once without failure injection.
631   660  
632   Invokes the provided function exactly once. Calls to 661   Invokes the provided function exactly once. Calls to
633   @ref maybe_fail always return an empty error code and 662   @ref maybe_fail always return an empty error code and
634 - never throw. Only explicit calls to @ref fail can 663 + never throw. A call to @ref fail or an exception escaping
635 - signal a test failure. 664 + `fn` signals a test failure. The escaping exception is
  665 + stored in `result::ep`.
  666 +
  667 + On failure after a @ref fail call, `result::loc` holds the
  668 + location of that call.
636   669  
637   This is useful for running tests where you want to 670   This is useful for running tests where you want to
638   manually control failures, or for quick single-run 671   manually control failures, or for quick single-run
639   tests without systematic error injection. 672   tests without systematic error injection.
640   673  
641   @par Example 674   @par Example
642   675  
643   @par !example example_1 676   @par !example example_1
644   677  
645   678  
646   @param fn The test function to invoke. It receives 679   @param fn The test function to invoke. It receives
647   a reference to the fuse. Calls to @ref maybe_fail 680   a reference to the fuse. Calls to @ref maybe_fail
648   always succeed. 681   always succeed.
649   682  
650 - On failure, `result::loc` contains the source location  
651 - of the @ref fail call.  
652   @return A @ref result indicating success or failure. 683   @return A @ref result indicating success or failure.
653   */ 684   */
654   template<class F> 685   template<class F>
655   result 686   result
HITCBC 656   9 inert(F&& fn) 687   9 inert(F&& fn)
657   { 688   {
HITCBC 658   9 result r; 689   9 result r;
HITCBC 659   9 p_->inert = true; 690   9 p_->inert = true;
660   try 691   try
661   { 692   {
HITCBC 662   9 fn(*this); 693   9 fn(*this);
663   } 694   }
HITCBC 664   2 catch(...) 695   2 catch(...)
665   { 696   {
HITCBC 666   1 r.success = false; 697   1 r.success = false;
HITCBC 667   1 r.loc = p_->loc; 698   1 r.loc = p_->loc;
HITCBC 668   1 r.ep = std::current_exception(); 699   1 r.ep = std::current_exception();
HITCBC 669   1 return r; 700   1 return r;
670   } 701   }
HITCBC 671   8 if(p_->stopped) 702   8 if(p_->stopped)
672   { 703   {
HITCBC 673   2 r.success = false; 704   2 r.success = false;
HITCBC 674   2 r.loc = p_->loc; 705   2 r.loc = p_->loc;
HITCBC 675   2 r.ep = p_->ep; 706   2 r.ep = p_->ep;
676   } 707   }
HITCBC 677   8 return r; 708   8 return r;
MISUBC 678   ✗ } 709   ✗ }
679   710  
680   /** Run a coroutine test function once without failure injection. 711   /** Run a coroutine test function once without failure injection.
681   712  
682   Invokes the provided coroutine function exactly once using 713   Invokes the provided coroutine function exactly once using
683   @ref run_blocking. Calls to @ref maybe_fail always return 714   @ref run_blocking. Calls to @ref maybe_fail always return
684 - an empty error code and never throw. Only explicit calls 715 + an empty error code and never throw. A call to @ref fail or
685 - to @ref fail can signal a test failure. 716 + an exception escaping the coroutine signals a test failure.
  717 + The escaping exception is stored in `result::ep`.
  718 +
  719 + On failure after a @ref fail call, `result::loc` holds the
  720 + location of that call.
686   721  
687   @par Example 722   @par Example
688   723  
689   @par !example example_2 724   @par !example example_2
690   725  
691   726  
692   @param fn The coroutine test function to invoke. It receives 727   @param fn The coroutine test function to invoke. It receives
693   a reference to the fuse. Calls to @ref maybe_fail 728   a reference to the fuse. Calls to @ref maybe_fail
694   always succeed. 729   always succeed.
695   730  
696 - On failure, `result::loc` contains the source location  
697 - of the @ref fail call.  
698   @return A @ref result indicating success or failure. 731   @return A @ref result indicating success or failure.
699   */ 732   */
700   template<class F> 733   template<class F>
701   requires ContextRunnable<std::invoke_result_t<F, fuse&>> 734   requires ContextRunnable<std::invoke_result_t<F, fuse&>>
702   result 735   result
HITCBC 703   44 inert(F&& fn) 736   44 inert(F&& fn)
704   { 737   {
HITCBC 705   44 result r; 738   44 result r;
HITCBC 706   44 p_->inert = true; 739   44 p_->inert = true;
707   try 740   try
708   { 741   {
HITCBC 709   44 run_blocking()(fn(*this)); 742   44 run_blocking()(fn(*this));
710   } 743   }
MISUBC 711   ✗ catch(...) 744   ✗ catch(...)
712   { 745   {
MISUBC 713   ✗ r.success = false; 746   ✗ r.success = false;
MISUBC 714   ✗ r.loc = p_->loc; 747   ✗ r.loc = p_->loc;
MISUBC 715   ✗ r.ep = std::current_exception(); 748   ✗ r.ep = std::current_exception();
MISUBC 716   ✗ return r; 749   ✗ return r;
717   } 750   }
HITCBC 718   44 if(p_->stopped) 751   44 if(p_->stopped)
719   { 752   {
MISUBC 720   ✗ r.success = false; 753   ✗ r.success = false;
MISUBC 721   ✗ r.loc = p_->loc; 754   ✗ r.loc = p_->loc;
MISUBC 722   ✗ r.ep = p_->ep; 755   ✗ r.ep = p_->ep;
723   } 756   }
HITCBC 724   44 return r; 757   44 return r;
MISUBC 725   ✗ } 758   ✗ }
726   }; 759   };
727   760  
728   } // test 761   } // test
729   } // capy 762   } // capy
730   } // boost 763   } // boost
731   764  
732   #endif 765   #endif