fail_count.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_TEST_FAIL_COUNT_HPP
  10. #define BOOST_BEAST_TEST_FAIL_COUNT_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/_experimental/test/error.hpp>
  13. #include <cstdlib>
  14. namespace boost {
  15. namespace beast {
  16. namespace test {
  17. /** A countdown to simulated failure
  18. On the Nth operation, the class will fail with the specified
  19. error code, or the default error code of @ref error::test_failure.
  20. Instances of this class may be used to build objects which
  21. are specifically designed to aid in writing unit tests, for
  22. interfaces which can throw exceptions or return `error_code`
  23. values representing failure.
  24. */
  25. class fail_count
  26. {
  27. std::size_t n_;
  28. std::size_t i_ = 0;
  29. error_code ec_;
  30. public:
  31. fail_count(fail_count&&) = default;
  32. /** Construct a counter
  33. @param n The 0-based index of the operation to fail on or after
  34. @param ev An optional error code to use when generating a simulated failure
  35. */
  36. BOOST_BEAST_DECL
  37. explicit
  38. fail_count(
  39. std::size_t n,
  40. error_code ev = error::test_failure);
  41. /// Throw an exception on the Nth failure
  42. BOOST_BEAST_DECL
  43. void
  44. fail();
  45. /// Set an error code on the Nth failure
  46. BOOST_BEAST_DECL
  47. bool
  48. fail(error_code& ec);
  49. };
  50. } // test
  51. } // beast
  52. } // boost
  53. #ifdef BOOST_BEAST_HEADER_ONLY
  54. #include <boost/beast/_experimental/test/impl/fail_count.ipp>
  55. #endif
  56. #endif