Add detail::bind_continuation

This commit is contained in:
Vinnie Falco committed 2019-02-13 12:59:24 -08:00
1 parent c08ce90e2c
commit 96b2944f70
7 files changed
+476 -1

No files matched your search

+1
View File
@@ -31,6 +31,7 @@ add_executable (tests-beast-core
_detail_varint.cpp
async_op_base.cpp
basic_stream.cpp
bind_continuation.cpp
bind_handler.cpp
buffer_size.cpp
buffer_traits.cpp
+1
View File
@@ -19,6 +19,7 @@ local SOURCES =
_detail_varint.cpp
async_op_base.cpp
basic_stream.cpp
bind_continuation.cpp
bind_handler.cpp
buffer_size.cpp
buffer_traits.cpp
+186
View File
@@ -0,0 +1,186 @@
//
// Copyright (c) 2018 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
//
// Test that header file is self-contained.
#include <boost/beast/core/bind_continuation.hpp>
#include "test_executor.hpp"
#include "test_handler.hpp"
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/beast/core/error.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/defer.hpp>
#include <boost/core/exchange.hpp>
namespace boost {
namespace beast {
class bind_continuation_test
: public beast::unit_test::suite
{
public:
class handler
{
bool pass_ = false;
public:
handler() = default;
~handler()
{
BEAST_EXPECT(pass_);
}
handler(handler&& other)
: pass_(boost::exchange(other.pass_, true))
{
}
void operator()()
{
pass_ = true;
}
};
void
testBinder()
{
net::io_context ioc;
// free functions
{
test_executor<> ex(ioc.get_executor());
BEAST_EXPECT(ex->total == 0);
net::dispatch(
bind_continuation(ex, handler{}));
ioc.run();
ioc.restart();
BEAST_EXPECT(ex->dispatch == 1);
}
{
test_executor<> ex(ioc.get_executor());
BEAST_EXPECT(ex->total == 0);
net::post(
bind_continuation(ex, handler{}));
ioc.run();
ioc.restart();
BEAST_EXPECT(ex->defer == 1);
}
{
test_executor<> ex(ioc.get_executor());
BEAST_EXPECT(ex->total == 0);
net::defer(
bind_continuation(ex, handler{}));
ioc.run();
ioc.restart();
BEAST_EXPECT(ex->defer == 1);
}
// members
{
test_executor<> ex(ioc.get_executor());
BEAST_EXPECT(ex->total == 0);
ex.dispatch(
bind_continuation(ex, handler{}),
std::allocator<void>{});
ioc.run();
ioc.restart();
BEAST_EXPECT(ex->dispatch == 1);
}
{
test_executor<> ex(ioc.get_executor());
BEAST_EXPECT(ex->total == 0);
ex.post(
bind_continuation(ex, handler{}),
std::allocator<void>{});
ioc.run();
ioc.restart();
BEAST_EXPECT(ex->post == 1);
}
{
test_executor<> ex(ioc.get_executor());
BEAST_EXPECT(ex->total == 0);
ex.defer(
bind_continuation(ex, handler{}),
std::allocator<void>{});
ioc.run();
ioc.restart();
BEAST_EXPECT(ex->defer == 1);
}
// relational
{
auto h1 = bind_continuation(
ioc.get_executor(), handler{});
auto h2 = bind_continuation(
ioc.get_executor(), handler{});
BEAST_EXPECT(
net::get_associated_executor(h1) ==
net::get_associated_executor(h2));
BEAST_EXPECT(
std::addressof(
net::get_associated_executor(h1).context()) ==
std::addressof(
net::get_associated_executor(h2).context()));
h1();
h2();
}
{
net::io_context ioc1;
net::io_context ioc2;
auto h1 = bind_continuation(
ioc1.get_executor(), handler{});
auto h2 = bind_continuation(
ioc2.get_executor(), handler{});
BEAST_EXPECT(
net::get_associated_executor(h1) !=
net::get_associated_executor(h2));
BEAST_EXPECT(
std::addressof(
net::get_associated_executor(h1).context()) !=
std::addressof(
net::get_associated_executor(h2).context()));
h1();
h2();
}
}
//--------------------------------------------------------------------------
void
testJavadoc()
{
}
//--------------------------------------------------------------------------
void
run() override
{
testBinder();
testJavadoc();
}
};
BEAST_DEFINE_TESTSUITE(beast,core,bind_continuation);
} // beast
} // boost
+122
View File
@@ -0,0 +1,122 @@
//
// Copyright (c) 2018 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_TEST_EXECUTOR_HPP
#define BOOST_BEAST_TEST_TEST_EXECUTOR_HPP
#include <boost/beast/core/detail/config.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_context.hpp>
#include <memory>
namespace boost {
namespace beast {
template<class Executor = net::io_context::executor_type>
class test_executor
{
public:
// VFALCO These need to be atomic or something
struct info
{
int dispatch = 0;
int post = 0;
int defer = 0;
int work = 0;
int total = 0;
};
private:
struct state
{
Executor ex;
info info_;
state(Executor const& ex_)
: ex(ex_)
{
}
};
std::shared_ptr<state> sp_;
public:
test_executor(test_executor const&) = default;
test_executor& operator=(test_executor const&) = default;
explicit
test_executor(Executor const& ex)
: sp_(std::make_shared<state>(ex))
{
}
decltype(sp_->ex.context())
context() const noexcept
{
return sp_->ex.context();
}
info&
operator*() noexcept
{
return sp_->info_;
}
info*
operator->() noexcept
{
return &sp_->info_;
}
void
on_work_started() const noexcept
{
++sp_->info_.work;
}
void
on_work_finished() const noexcept
{
}
template<class F, class A>
void
dispatch(F&& f, A const& a) const
{
++sp_->info_.dispatch;
++sp_->info_.total;
sp_->ex.dispatch(
std::forward<F>(f), a);
}
template<class F, class A>
void
post(F&& f, A const& a) const
{
++sp_->info_.post;
++sp_->info_.total;
sp_->ex.post(
std::forward<F>(f), a);
}
template<class F, class A>
void
defer(F&& f, A const& a) const
{
++sp_->info_.defer;
++sp_->info_.total;
sp_->ex.defer(
std::forward<F>(f), a);
}
};
} // beast
} // boost
#endif