2 // Copyright (c) 2016-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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_TEST_FAIL_COUNTER_HPP
11 #define BOOST_BEAST_TEST_FAIL_COUNTER_HPP
13 #include <boost/beast/core/error.hpp>
14 #include <boost/throw_exception.hpp>
27 class fail_error_category : public boost::system::error_category
31 name() const noexcept override
37 message(int ev) const override
39 switch(static_cast<error>(ev))
42 case error::fail_error:
47 boost::system::error_condition
48 default_error_condition(int ev) const noexcept override
50 return boost::system::error_condition{ev, *this};
55 boost::system::error_condition const& condition
56 ) const noexcept override
58 return condition.value() == ev &&
59 &condition.category() == this;
63 equivalent(error_code const& error, int ev) const noexcept override
65 return error.value() == ev &&
66 &error.category() == this;
71 boost::system::error_category const&
74 static fail_error_category const cat{};
82 make_error_code(error ev)
85 static_cast<std::underlying_type<error>::type>(ev),
86 detail::get_error_category()};
89 /** An error code with an error set on default construction
91 Default constructed versions of this object will have
92 an error code set right away. This helps tests find code
93 which forgets to clear the error code on success.
95 struct fail_error_code : error_code
98 : error_code(make_error_code(error::fail_error))
102 template<class Arg0, class... ArgN>
103 fail_error_code(Arg0&& arg0, ArgN&&... argn)
104 : error_code(arg0, std::forward<ArgN>(argn)...)
109 /** A countdown to simulated failure.
111 On the Nth operation, the class will fail with the specified
112 error code, or the default error code of @ref error::fail_error.
121 fail_counter(fail_counter&&) = default;
123 /** Construct a counter.
125 @param The 0-based index of the operation to fail on or after.
128 fail_counter(std::size_t n,
129 error_code ev = make_error_code(error::fail_error))
135 /// Returns the fail index
142 /// Throw an exception on the Nth failure
149 BOOST_THROW_EXCEPTION(system_error{ec_});
152 /// Set an error code on the Nth failure
163 ec.assign(0, ec.category());
175 struct is_error_code_enum<beast::test::error>
177 static bool const value = true;