Add tests for bind_back_handler

This commit is contained in:
Vinnie Falco
2019-01-09 18:24:34 -08:00
parent 5292df6e72
commit b4c63028e2
4 changed files with 315 additions and 607 deletions
+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();
}
};