handler.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_HANDLER_HPP
  10. #define BOOST_BEAST_TEST_HANDLER_HPP
  11. #include <boost/beast/_experimental/unit_test/suite.hpp>
  12. #include <boost/beast/core/error.hpp>
  13. #include <boost/asio/io_context.hpp>
  14. #include <boost/core/exchange.hpp>
  15. #include <boost/optional.hpp>
  16. namespace boost {
  17. namespace beast {
  18. namespace test {
  19. /** A CompletionHandler used for testing.
  20. This completion handler is used by tests to ensure correctness
  21. of behavior. It is designed as a single type to reduce template
  22. instantiations, with configurable settings through constructor
  23. arguments. Typically this type will be used in type lists and
  24. not instantiated directly; instances of this class are returned
  25. by the helper functions listed below.
  26. @see success_handler, @ref fail_handler, @ref any_handler
  27. */
  28. class handler
  29. {
  30. boost::optional<error_code> ec_;
  31. bool pass_ = false;
  32. public:
  33. handler() = default;
  34. explicit
  35. handler(error_code ec)
  36. : ec_(ec)
  37. {
  38. }
  39. explicit
  40. handler(boost::none_t)
  41. {
  42. }
  43. handler(handler&& other)
  44. : ec_(other.ec_)
  45. , pass_(boost::exchange(other.pass_, true))
  46. {
  47. }
  48. ~handler()
  49. {
  50. BEAST_EXPECT(pass_);
  51. }
  52. template<class... Args>
  53. void
  54. operator()(error_code ec, Args&&...)
  55. {
  56. BEAST_EXPECT(! pass_); // can't call twice
  57. BEAST_EXPECTS(! ec_ || ec == *ec_,
  58. ec.message());
  59. pass_ = true;
  60. }
  61. void
  62. operator()()
  63. {
  64. BEAST_EXPECT(! pass_); // can't call twice
  65. BEAST_EXPECT(! ec_);
  66. pass_ = true;
  67. }
  68. template<class Arg0, class... Args,
  69. class = typename std::enable_if<
  70. ! std::is_convertible<Arg0, error_code>::value>::type>
  71. void
  72. operator()(Arg0&&, Args&&...)
  73. {
  74. BEAST_EXPECT(! pass_); // can't call twice
  75. BEAST_EXPECT(! ec_);
  76. pass_ = true;
  77. }
  78. };
  79. /** Return a test CompletionHandler which requires success.
  80. The returned handler can be invoked with any signature whose
  81. first parameter is an `error_code`. The handler fails the test
  82. if:
  83. @li The handler is destroyed without being invoked, or
  84. @li The handler is invoked with a non-successful error code.
  85. */
  86. inline
  87. handler
  88. success_handler() noexcept
  89. {
  90. return handler(error_code{});
  91. }
  92. /** Return a test CompletionHandler which requires invocation.
  93. The returned handler can be invoked with any signature.
  94. The handler fails the test if:
  95. @li The handler is destroyed without being invoked.
  96. */
  97. inline
  98. handler
  99. any_handler() noexcept
  100. {
  101. return handler(boost::none);
  102. }
  103. /** Return a test CompletionHandler which requires a specific error code.
  104. This handler can be invoked with any signature whose first
  105. parameter is an `error_code`. The handler fails the test if:
  106. @li The handler is destroyed without being invoked.
  107. @li The handler is invoked with an error code different from
  108. what is specified.
  109. @param ec The error code to specify.
  110. */
  111. inline
  112. handler
  113. fail_handler(error_code ec) noexcept
  114. {
  115. return handler(ec);
  116. }
  117. /** Run an I/O context.
  118. This function runs and dispatches handlers on the specified
  119. I/O context, until one of the following conditions is true:
  120. @li The I/O context runs out of work.
  121. @param ioc The I/O context to run
  122. */
  123. inline
  124. void
  125. run(net::io_context& ioc)
  126. {
  127. ioc.run();
  128. ioc.restart();
  129. }
  130. /** Run an I/O context for a certain amount of time.
  131. This function runs and dispatches handlers on the specified
  132. I/O context, until one of the following conditions is true:
  133. @li The I/O context runs out of work.
  134. @li No completions occur and the specified amount of time has elapsed.
  135. @param ioc The I/O context to run
  136. @param elapsed The maximum amount of time to run for.
  137. */
  138. template<class Rep, class Period>
  139. void
  140. run_for(
  141. net::io_context& ioc,
  142. std::chrono::duration<Rep, Period> elapsed)
  143. {
  144. ioc.run_for(elapsed);
  145. ioc.restart();
  146. }
  147. } // test
  148. } // beast
  149. } // boost
  150. #endif