Add tests for bind_back_handler

This commit is contained in:
Vinnie Falco committed 2019-01-09 18:24:34 -08:00
1 parent 5292df6e72
commit b4c63028e2
4 files changed
+315 -607

No files matched your search

+167 -51
View File
@@ -10,6 +10,8 @@
// Test that header file is self-contained.
#include <boost/beast/core/bind_handler.hpp>
#include "test_handler.hpp"
#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/beast/_experimental/test/stream.hpp>
@@ -28,8 +30,6 @@
namespace boost {
namespace beast {
//------------------------------------------------------------------------------
class bind_handler_test : public unit_test::suite
{
public:
@@ -42,54 +42,6 @@ public:
}
};
template <class AsyncReadStream, class ReadHandler>
void
signal_aborted (AsyncReadStream& stream, ReadHandler&& handler)
{
net::post(
stream.get_executor(),
bind_handler (std::forward <ReadHandler> (handler),
net::error::operation_aborted, 0));
}
template <class AsyncReadStream, class ReadHandler>
void
signal_eof (AsyncReadStream& stream, ReadHandler&& handler)
{
net::post(
stream.get_executor(),
bind_front_handler (std::forward<ReadHandler> (handler),
net::error::eof, 0));
}
template <class AsyncReadStream, class ReadHandler>
void
signal_unreachable (AsyncReadStream& stream, ReadHandler&& handler)
{
net::post(
stream.get_executor(),
bind_back_handler (std::forward<ReadHandler> (handler),
net::error::network_unreachable, 0));
}
void
testJavadocs()
{
BEAST_EXPECT((
&bind_handler_test::signal_aborted<
test::stream, handler<error_code, std::size_t>>));
BEAST_EXPECT((
&bind_handler_test::signal_eof<
test::stream, handler<error_code, std::size_t>>));
BEAST_EXPECT((
&bind_handler_test::signal_unreachable<
test::stream, handler<error_code, std::size_t>>));
}
//--------------------------------------------------------------------------
struct copyable
{
template<class... Args>
@@ -307,6 +259,12 @@ public:
{
std::bind(bind_front_handler(test_cb{*this}));
}
void
failStdBindBack()
{
std::bind(bind_back_handler(test_cb{*this}));
}
#endif
//--------------------------------------------------------------------------
@@ -440,6 +398,13 @@ public:
bind_handler(test_cb{*this}, 42));
ioc.run();
}
// legacy hooks
legacy_handler::test(*this,
[](legacy_handler h)
{
return bind_handler(h);
});
}
void
@@ -513,14 +478,165 @@ public:
test_executor(*this, ioc), test_cb{*this}),
ec, n));
}
// legacy hooks
legacy_handler::test(*this,
[](legacy_handler h)
{
return bind_front_handler(h);
});
legacy_handler::test(*this,
[](legacy_handler h)
{
return bind_front_handler(
h, error_code{}, std::size_t{});
});
}
void
testBindBackHandler()
{
using m1 = move_arg<1>;
using m2 = move_arg<2>;
// 0-ary
bind_back_handler(test_cb{*this})();
// 1-ary
bind_back_handler(test_cb{*this}, 42)();
bind_back_handler(test_cb{*this})(42);
// 2-ary
bind_back_handler(test_cb{*this}, 42, "s")();
bind_back_handler(test_cb{*this}, "s")(42);
bind_back_handler(test_cb{*this})(42, "s");
// 3-ary
bind_back_handler(test_cb{*this}, 42, "s", m1{})();
bind_back_handler(test_cb{*this}, m1{})(42, "s");
bind_back_handler(test_cb{*this}, "s", m1{})(42);
bind_back_handler(test_cb{*this})(42, "s", m1{});
// 4-ary
bind_back_handler(test_cb{*this}, 42, "s", m1{}, m2{})();
bind_back_handler(test_cb{*this}, "s", m1{}, m2{})(42);
bind_back_handler(test_cb{*this}, m1{}, m2{})(42, "s");
bind_back_handler(test_cb{*this}, "s", m1{}, m2{})(42);
bind_back_handler(test_cb{*this})(42, "s", m1{}, m2{});
error_code ec;
std::size_t n = 256;
// void(error_code, size_t)
bind_back_handler(test_cb{*this}, ec, n)();
// void(error_code, size_t)(string_view)
bind_back_handler(test_cb{*this}, "s")(ec, n);
// perfect forwarding
{
std::shared_ptr<int> const sp =
std::make_shared<int>(42);
bind_back_handler(test_cb{*this}, sp)();
BEAST_EXPECT(sp.get() != nullptr);
}
// associated executor
{
net::io_context ioc;
testHooks(ioc, bind_back_handler(net::bind_executor(
test_executor(*this, ioc), test_cb{*this})
));
testHooks(ioc, bind_back_handler(net::bind_executor(
test_executor(*this, ioc), test_cb{*this}),
42));
testHooks(ioc, bind_back_handler(net::bind_executor(
test_executor(*this, ioc), test_cb{*this}),
42, "s"));
testHooks(ioc, bind_back_handler(net::bind_executor(
test_executor(*this, ioc), test_cb{*this}),
42, "s", m1{}));
testHooks(ioc, bind_back_handler(net::bind_executor(
test_executor(*this, ioc), test_cb{*this}),
42, "s", m1{}, m2{}));
testHooks(ioc, bind_back_handler(net::bind_executor(
test_executor(*this, ioc), test_cb{*this}),
ec, n));
}
// legacy hooks
legacy_handler::test(*this,
[](legacy_handler h)
{
return bind_back_handler(h);
});
legacy_handler::test(*this,
[](legacy_handler h)
{
return bind_back_handler(
h, error_code{}, std::size_t{});
});
}
//--------------------------------------------------------------------------
template <class AsyncReadStream, class ReadHandler>
void
signal_aborted (AsyncReadStream& stream, ReadHandler&& handler)
{
net::post(
stream.get_executor(),
bind_handler (std::forward <ReadHandler> (handler),
net::error::operation_aborted, 0));
}
template <class AsyncReadStream, class ReadHandler>
void
signal_eof (AsyncReadStream& stream, ReadHandler&& handler)
{
net::post(
stream.get_executor(),
bind_front_handler (std::forward<ReadHandler> (handler),
net::error::eof, 0));
}
template <class AsyncReadStream, class ReadHandler>
void
signal_unreachable (AsyncReadStream& stream, ReadHandler&& handler)
{
net::post(
stream.get_executor(),
bind_back_handler (std::forward<ReadHandler> (handler),
net::error::network_unreachable, 0));
}
void
testJavadocs()
{
BEAST_EXPECT((
&bind_handler_test::signal_aborted<
test::stream, handler<error_code, std::size_t>>));
BEAST_EXPECT((
&bind_handler_test::signal_eof<
test::stream, handler<error_code, std::size_t>>));
BEAST_EXPECT((
&bind_handler_test::signal_unreachable<
test::stream, handler<error_code, std::size_t>>));
}
//--------------------------------------------------------------------------
void
run() override
{
testJavadocs();
testBindHandler();
testBindFrontHandler();
testBindBackHandler();
testJavadocs();
}
};
+129
View File
@@ -0,0 +1,129 @@
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_TEST_HANDLER_HPP
#define BOOST_BEAST_TEST_HANDLER_HPP
#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/asio/associated_allocator.hpp>
#include <boost/asio/associated_executor.hpp>
#include <boost/asio/handler_alloc_hook.hpp>
#include <boost/asio/handler_continuation_hook.hpp>
#include <boost/asio/handler_invoke_hook.hpp>
namespace boost {
namespace beast {
// Used to test the legacy handler hooks
struct legacy_handler
{
bool& hook_invoked;
struct executor
{
void* context() { return nullptr; }
void on_work_started() {}
void on_work_finished() {}
template<class F> void dispatch(F&&) {}
template<class F> void post(F&&) {}
template<class F> void defer(F&&) {}
};
executor
get_executor() const noexcept
{
return {};
};
// Signature of `f` is H<legacy_handler> where H is the wrapper to test
template<class F>
static
void
test(unit_test::suite& suite, F const& f)
{
{
bool hook_invoked = false;
bool lambda_invoked = false;
auto h = f(legacy_handler{hook_invoked});
using net::asio_handler_invoke;
asio_handler_invoke(
[&lambda_invoked]
{
lambda_invoked =true;
}, &h);
suite.BEAST_EXPECT(hook_invoked);
suite.BEAST_EXPECT(lambda_invoked);
}
{
bool hook_invoked = false;
auto h = f(legacy_handler{hook_invoked});
using net::asio_handler_allocate;
asio_handler_allocate(0, &h);
suite.BEAST_EXPECT(hook_invoked);
}
{
bool hook_invoked = false;
auto h = f(legacy_handler{hook_invoked});
using net::asio_handler_deallocate;
asio_handler_deallocate(nullptr, 0, &h);
suite.BEAST_EXPECT(hook_invoked);
}
{
bool hook_invoked = false;
auto h = f(legacy_handler{hook_invoked});
using net::asio_handler_is_continuation;
asio_handler_is_continuation(&h);
suite.BEAST_EXPECT(hook_invoked);
}
}
};
template<class Function>
void
asio_handler_invoke(
Function&& f,
legacy_handler* p)
{
p->hook_invoked = true;
f();
}
inline
void*
asio_handler_allocate(
std::size_t,
legacy_handler* p)
{
p->hook_invoked = true;
return nullptr;
}
inline
void
asio_handler_deallocate(
void*, std::size_t,
legacy_handler* p)
{
p->hook_invoked = true;
}
inline
bool
asio_handler_is_continuation(
legacy_handler* p)
{
p->hook_invoked = true;
return false;
}
} // beast
} // boost
#endif