2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-27 06:32:08 +00:00
Files
asio/test/experimental/channel.cpp
Christopher Kohlhoff 495e6367af Add experimental support for channels.
This adds experimental::channel and experimental::concurrent_channel.
Channels may be used to send completions as messages. For example:

  // Create a channel with no buffer space.
  channel<void(error_code, size_t)> ch(ctx);

  // The call to try_send fails as there is no buffer
  // space and no waiting receive operations.
  bool ok = ch.try_send(asio::error::eof, 123);
  assert(!ok);

  // The async_send operation blocks until a receive
  // operation consumes the message.
  ch.async_send(asio::error::eof, 123,
      [](error_code ec)
      {
        // ...
      });

  // The async_receive consumes the message. Both the
  // async_send and async_receive operations complete
  // immediately.
  ch.async_receive(
      [](error_code ec, size_t n)
      {
        // ...
      });
2021-11-04 01:56:32 +11:00

166 lines
3.9 KiB
C++

//
// experimental/channel.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff 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)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include <boost/asio/experimental/channel.hpp>
#include <utility>
#include <boost/asio/error.hpp>
#include <boost/asio/io_context.hpp>
#include "../unit_test.hpp"
using namespace boost::asio;
using namespace boost::asio::experimental;
void unbuffered_channel_test()
{
io_context ctx;
channel<void(boost::system::error_code, std::string)> ch1(ctx);
BOOST_ASIO_CHECK(ch1.is_open());
BOOST_ASIO_CHECK(!ch1.ready());
bool b1 = ch1.try_send(boost::asio::error::eof, "hello");
BOOST_ASIO_CHECK(!b1);
std::string s1 = "abcdefghijklmnopqrstuvwxyz";
bool b2 = ch1.try_send(boost::asio::error::eof, std::move(s1));
BOOST_ASIO_CHECK(!b2);
BOOST_ASIO_CHECK(!s1.empty());
boost::system::error_code ec1;
std::string s2;
ch1.async_receive(
[&](boost::system::error_code ec, std::string s)
{
ec1 = ec;
s2 = std::move(s);
});
bool b3 = ch1.try_send(boost::asio::error::eof, std::move(s1));
BOOST_ASIO_CHECK(b3);
BOOST_ASIO_CHECK(s1.empty());
ctx.run();
BOOST_ASIO_CHECK(ec1 == boost::asio::error::eof);
BOOST_ASIO_CHECK(s2 == "abcdefghijklmnopqrstuvwxyz");
bool b4 = ch1.try_receive([](boost::system::error_code, std::string){});
BOOST_ASIO_CHECK(!b4);
boost::system::error_code ec2 = boost::asio::error::would_block;
std::string s3 = "zyxwvutsrqponmlkjihgfedcba";
ch1.async_send(boost::asio::error::eof, std::move(s3),
[&](boost::system::error_code ec)
{
ec2 = ec;
});
boost::system::error_code ec3;
std::string s4;
bool b5 = ch1.try_receive(
[&](boost::system::error_code ec, std::string s)
{
ec3 = ec;
s4 = s;
});
BOOST_ASIO_CHECK(b5);
BOOST_ASIO_CHECK(ec3 == boost::asio::error::eof);
BOOST_ASIO_CHECK(s4 == "zyxwvutsrqponmlkjihgfedcba");
ctx.restart();
ctx.run();
BOOST_ASIO_CHECK(!ec2);
};
void buffered_channel_test()
{
io_context ctx;
channel<void(boost::system::error_code, std::string)> ch1(ctx, 1);
BOOST_ASIO_CHECK(ch1.is_open());
BOOST_ASIO_CHECK(!ch1.ready());
bool b1 = ch1.try_send(boost::asio::error::eof, "hello");
BOOST_ASIO_CHECK(b1);
std::string s1 = "abcdefghijklmnopqrstuvwxyz";
bool b2 = ch1.try_send(boost::asio::error::eof, std::move(s1));
BOOST_ASIO_CHECK(!b2);
BOOST_ASIO_CHECK(!s1.empty());
boost::system::error_code ec1;
std::string s2;
ch1.async_receive(
[&](boost::system::error_code ec, std::string s)
{
ec1 = ec;
s2 = std::move(s);
});
ctx.run();
BOOST_ASIO_CHECK(ec1 == boost::asio::error::eof);
BOOST_ASIO_CHECK(s2 == "hello");
bool b4 = ch1.try_receive([](boost::system::error_code, std::string){});
BOOST_ASIO_CHECK(!b4);
boost::system::error_code ec2 = boost::asio::error::would_block;
std::string s3 = "zyxwvutsrqponmlkjihgfedcba";
ch1.async_send(boost::asio::error::eof, std::move(s3),
[&](boost::system::error_code ec)
{
ec2 = ec;
});
boost::system::error_code ec3;
std::string s4;
bool b5 = ch1.try_receive(
[&](boost::system::error_code ec, std::string s)
{
ec3 = ec;
s4 = s;
});
BOOST_ASIO_CHECK(b5);
BOOST_ASIO_CHECK(ec3 == boost::asio::error::eof);
BOOST_ASIO_CHECK(s4 == "zyxwvutsrqponmlkjihgfedcba");
ctx.restart();
ctx.run();
BOOST_ASIO_CHECK(!ec2);
};
BOOST_ASIO_TEST_SUITE
(
"experimental/channel",
BOOST_ASIO_TEST_CASE(unbuffered_channel_test)
BOOST_ASIO_TEST_CASE(buffered_channel_test)
)