2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
8 #ifndef BEAST_TEST_FAIL_COUNTER_HPP
9 #define BEAST_TEST_FAIL_COUNTER_HPP
11 #include <beast/core/error.hpp>
23 class fail_error_category : public boost::system::error_category
27 name() const noexcept override
33 message(int ev) const override
35 switch(static_cast<error>(ev))
38 case error::fail_error:
43 boost::system::error_condition
44 default_error_condition(int ev) const noexcept override
46 return boost::system::error_condition{ev, *this};
51 boost::system::error_condition const& condition
52 ) const noexcept override
54 return condition.value() == ev &&
55 &condition.category() == this;
59 equivalent(error_code const& error, int ev) const noexcept override
61 return error.value() == ev &&
62 &error.category() == this;
67 boost::system::error_category const&
70 static fail_error_category const cat{};
78 make_error_code(error ev)
81 static_cast<std::underlying_type<error>::type>(ev),
82 detail::get_error_category()};
85 /** A countdown to simulated failure.
87 On the Nth operation, the class will fail with the specified
88 error code, or the default error code of @ref error::fail_error.
96 fail_counter(fail_counter&&) = default;
98 /** Construct a counter.
100 @param The 0-based index of the operation to fail on or after.
103 fail_counter(std::size_t n,
104 error_code ev = make_error_code(error::fail_error))
110 /// Throw an exception on the Nth failure
117 throw system_error{ec_};
120 /// Set an error code on the Nth failure
141 struct is_error_code_enum<beast::test::error>
143 static bool const value = true;