]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/process/test/async_pipe.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / process / test / async_pipe.cpp
1 // Copyright (c) 2016 Klemens D. Morgenstern
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6
7 #define BOOST_TEST_MAIN
8
9 #include <boost/test/included/unit_test.hpp>
10 #include <iostream>
11 #include <thread>
12 #include <vector>
13 #include <boost/algorithm/string/predicate.hpp>
14
15 #include <boost/process/async_pipe.hpp>
16 #include <boost/process/pipe.hpp>
17 #include <boost/asio/read.hpp>
18 #include <boost/asio/read_until.hpp>
19 #include <boost/asio/write.hpp>
20 #include <boost/asio/streambuf.hpp>
21
22 using namespace std;
23 namespace bp = boost::process;
24 namespace asio = boost::asio;
25
26 BOOST_AUTO_TEST_CASE(plain_async, *boost::unit_test::timeout(5))
27 {
28 asio::io_context ios;
29 bp::async_pipe pipe{ios};
30
31 std::string st = "test-string\n";
32
33 asio::streambuf buf;
34
35 asio::async_write(pipe, asio::buffer(st), [](const boost::system::error_code &, std::size_t){});
36 asio::async_read_until(pipe, buf, '\n', [](const boost::system::error_code &, std::size_t){});
37
38 ios.run();
39
40 std::string line;
41 std::istream istr(&buf);
42 BOOST_CHECK(std::getline(istr, line));
43
44 line.resize(11);
45 BOOST_CHECK_EQUAL(line, "test-string");
46
47 }
48
49 BOOST_AUTO_TEST_CASE(closed_transform)
50 {
51 asio::io_context ios;
52
53 bp::async_pipe ap{ios};
54
55 BOOST_CHECK(ap.is_open());
56 bp::pipe p2 = static_cast<bp::pipe>(ap);
57 BOOST_CHECK(p2.is_open());
58
59 ap.close();
60 BOOST_CHECK(!ap.is_open());
61
62 bp::pipe p = static_cast<bp::pipe>(ap);
63 BOOST_CHECK(!p.is_open());
64
65 }
66
67 BOOST_AUTO_TEST_CASE(multithreaded_async_pipe)
68 {
69 asio::io_context ioc;
70
71 std::vector<std::thread> threads;
72 for (int i = 0; i < std::thread::hardware_concurrency(); i++)
73 {
74 threads.emplace_back([&ioc]
75 {
76 std::vector<bp::async_pipe*> pipes;
77 for (size_t i = 0; i < 100; i++)
78 pipes.push_back(new bp::async_pipe(ioc));
79 for (auto &p : pipes)
80 delete p;
81 });
82 }
83 for (auto &t : threads)
84 t.join();
85 }