2 // Copyright (c) 2016-2019 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_HANDLER_XXX_HPP
11 #define BOOST_BEAST_TEST_HANDLER_XXX_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/_experimental/unit_test/suite.hpp>
15 #include <boost/asio/associated_allocator.hpp>
16 #include <boost/asio/associated_executor.hpp>
17 #include <boost/asio/handler_alloc_hook.hpp>
18 #include <boost/asio/handler_continuation_hook.hpp>
19 #include <boost/asio/handler_invoke_hook.hpp>
24 class simple_allocator
32 static std::size_t n = 0;
40 simple_allocator const& lhs,
41 simple_allocator const& rhs) noexcept
43 return lhs.id_ == rhs.id_;
48 simple_allocator const& lhs,
49 simple_allocator const& rhs) noexcept
51 return lhs.id_ != rhs.id_;
63 static std::size_t n = 0;
69 void* context() { return nullptr; }
70 void on_work_started() {}
71 void on_work_finished() {}
72 template<class F> void dispatch(F&&) {}
73 template<class F> void post(F&&) {}
74 template<class F> void defer(F&&) {}
78 simple_executor const& lhs,
79 simple_executor const& rhs) noexcept
81 return lhs.id_ == rhs.id_;
86 simple_executor const& lhs,
87 simple_executor const& rhs) noexcept
89 return lhs.id_ != rhs.id_;
93 // A move-only handler
94 struct move_only_handler
96 move_only_handler() = default;
97 move_only_handler(move_only_handler&&) = default;
98 move_only_handler(move_only_handler const&) = delete;
99 void operator()() const{};
102 // Used to test the legacy handler hooks
103 struct legacy_handler
108 get_executor() const noexcept
113 // Signature of `f` is H<legacy_handler> where H is the wrapper to test
120 bool hook_invoked = false;
121 bool lambda_invoked = false;
122 auto h = f(legacy_handler{hook_invoked});
123 using net::asio_handler_invoke;
127 lambda_invoked =true;
129 BEAST_EXPECT(hook_invoked);
130 BEAST_EXPECT(lambda_invoked);
133 bool hook_invoked = false;
134 auto h = f(legacy_handler{hook_invoked});
135 using net::asio_handler_allocate;
136 asio_handler_allocate(0, &h);
137 BEAST_EXPECT(hook_invoked);
140 bool hook_invoked = false;
141 auto h = f(legacy_handler{hook_invoked});
142 using net::asio_handler_deallocate;
143 asio_handler_deallocate(nullptr, 0, &h);
144 BEAST_EXPECT(hook_invoked);
147 bool hook_invoked = false;
148 auto h = f(legacy_handler{hook_invoked});
149 using net::asio_handler_is_continuation;
150 asio_handler_is_continuation(&h);
151 BEAST_EXPECT(hook_invoked);
156 template<class Function>
162 p->hook_invoked = true;
168 asio_handler_allocate(
172 p->hook_invoked = true;
178 asio_handler_deallocate(
182 p->hook_invoked = true;
187 asio_handler_is_continuation(
190 p->hook_invoked = true;
200 template<class Allocator>
201 struct associated_allocator<
202 boost::beast::legacy_handler, Allocator>
204 using type = std::allocator<int>;
207 boost::beast::legacy_handler const&,
208 Allocator const& = Allocator()) noexcept
214 template<class Executor>
215 struct associated_executor<
216 boost::beast::legacy_handler, Executor>
218 using type = boost::beast::simple_executor;
221 boost::beast::legacy_handler const&,
222 Executor const& = Executor()) noexcept