Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:23

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_TEST_HANDLER_HPP
0011 #define BOOST_BEAST_TEST_HANDLER_HPP
0012 
0013 #include <boost/beast/_experimental/unit_test/suite.hpp>
0014 #include <boost/beast/core/error.hpp>
0015 #include <boost/asio/io_context.hpp>
0016 #include <boost/core/exchange.hpp>
0017 #include <boost/optional.hpp>
0018 
0019 namespace boost {
0020 namespace beast {
0021 namespace test {
0022 
0023 /** A CompletionHandler used for testing.
0024 
0025     This completion handler is used by tests to ensure correctness
0026     of behavior. It is designed as a single type to reduce template
0027     instantiations, with configurable settings through constructor
0028     arguments. Typically this type will be used in type lists and
0029     not instantiated directly; instances of this class are returned
0030     by the helper functions listed below.
0031 
0032     @see success_handler, @ref fail_handler, @ref any_handler
0033 */
0034 class handler
0035 {
0036     boost::optional<error_code> ec_;
0037     bool pass_ = false;
0038     boost::source_location loc_{BOOST_CURRENT_LOCATION};
0039 public:
0040     handler(boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc) {}
0041 
0042     explicit
0043     handler(error_code ec, boost::source_location loc = BOOST_CURRENT_LOCATION)
0044         : ec_(ec), loc_(loc)
0045     {
0046     }
0047 
0048     explicit
0049     handler(boost::none_t, boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc)
0050     {
0051     }
0052 
0053     handler(handler&& other)
0054         : ec_(other.ec_)
0055         , pass_(boost::exchange(other.pass_, true))
0056         , loc_(other.loc_)
0057 
0058     {
0059     }
0060 
0061     ~handler()
0062     {
0063         ::boost::beast::unit_test::suite::this_suite()->expect(pass_, loc_.file_name(), loc_.line());
0064     }
0065 
0066     template<class... Args>
0067     void
0068     operator()(error_code ec, Args&&...)
0069     {
0070         ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
0071         if (ec_ && ec != *ec_)
0072             ::boost::beast::unit_test::suite::this_suite()->fail(ec.message(), loc_.file_name(), loc_.line());
0073         else
0074             ::boost::beast::unit_test::suite::this_suite()->pass();
0075         pass_ = true;
0076     }
0077 
0078     void
0079     operator()()
0080     {
0081         ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
0082         if (ec_ && ec_->failed())
0083             ::boost::beast::unit_test::suite::this_suite()->fail(ec_->message(), loc_.file_name(), loc_.line());
0084         else
0085             ::boost::beast::unit_test::suite::this_suite()->pass();
0086 
0087         pass_ = true;
0088     }
0089 
0090     template<class Arg0, class... Args,
0091         class = typename std::enable_if<
0092             ! std::is_convertible<Arg0, error_code>::value>::type>
0093     void
0094     operator()(Arg0&&, Args&&...)
0095     {
0096         ::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
0097         if (ec_ && ec_->failed())
0098             ::boost::beast::unit_test::suite::this_suite()->fail(ec_->message(), loc_.file_name(), loc_.line());
0099         else
0100             ::boost::beast::unit_test::suite::this_suite()->pass();
0101         pass_ = true;
0102     }
0103 };
0104 
0105 /** Return a test CompletionHandler which requires success.
0106     
0107     The returned handler can be invoked with any signature whose
0108     first parameter is an `error_code`. The handler fails the test
0109     if:
0110 
0111     @li The handler is destroyed without being invoked, or
0112 
0113     @li The handler is invoked with a non-successful error code.
0114 */
0115 inline
0116 handler
0117 success_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
0118 {
0119     return handler(error_code{}, loc);
0120 }
0121 
0122 /** Return a test CompletionHandler which requires invocation.
0123 
0124     The returned handler can be invoked with any signature.
0125     The handler fails the test if:
0126 
0127     @li The handler is destroyed without being invoked.
0128 */
0129 inline
0130 handler
0131 any_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
0132 {
0133     return handler(boost::none, loc);
0134 }
0135 
0136 /** Return a test CompletionHandler which requires a specific error code.
0137 
0138     This handler can be invoked with any signature whose first
0139     parameter is an `error_code`. The handler fails the test if:
0140 
0141     @li The handler is destroyed without being invoked.
0142 
0143     @li The handler is invoked with an error code different from
0144     what is specified.
0145 
0146     @param ec The error code to specify.
0147 */
0148 inline
0149 handler
0150 fail_handler(error_code ec,boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
0151 {
0152     return handler(ec, loc);
0153 }
0154 
0155 /** Run an I/O context.
0156     
0157     This function runs and dispatches handlers on the specified
0158     I/O context, until one of the following conditions is true:
0159         
0160     @li The I/O context runs out of work.
0161 
0162     @param ioc The I/O context to run
0163 */
0164 inline
0165 void
0166 run(net::io_context& ioc)
0167 {
0168     ioc.run();
0169     ioc.restart();
0170 }
0171 
0172 /** Run an I/O context for a certain amount of time.
0173     
0174     This function runs and dispatches handlers on the specified
0175     I/O context, until one of the following conditions is true:
0176         
0177     @li The I/O context runs out of work.
0178 
0179     @li No completions occur and the specified amount of time has elapsed.
0180 
0181     @param ioc The I/O context to run
0182 
0183     @param elapsed The maximum amount of time to run for.
0184 */
0185 template<class Rep, class Period>
0186 void
0187 run_for(
0188     net::io_context& ioc,
0189     std::chrono::duration<Rep, Period> elapsed)
0190 {
0191     ioc.run_for(elapsed);
0192     ioc.restart();
0193 }
0194 
0195 } // test
0196 } // beast
0197 } // boost
0198 
0199 #endif