fail_count.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Copyright (c) 2016-2017 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/error.hpp>
  12. #include <boost/beast/experimental/test/error.hpp>
  13. #include <boost/throw_exception.hpp>
  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. explicit
  37. fail_count(
  38. std::size_t n,
  39. error_code ev = make_error_code(error::test_failure));
  40. /// Throw an exception on the Nth failure
  41. void
  42. fail();
  43. /// Set an error code on the Nth failure
  44. bool
  45. fail(error_code& ec);
  46. };
  47. } // test
  48. } // beast
  49. } // boost
  50. #include <boost/beast/experimental/test/impl/fail_count.ipp>
  51. #endif