| 1 | // Copyright (c) 2022 Klemens D. Morgenstern |
| 2 | // Copyright (c) 2022 Samuel Venable |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #include <boost/process/v2/pid.hpp> |
| 8 | #include <boost/process/v2/process.hpp> |
| 9 | |
| 10 | #include <boost/test/unit_test.hpp> |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <thread> |
| 14 | #include <vector> |
| 15 | |
| 16 | BOOST_AUTO_TEST_CASE(test_pid) |
| 17 | { |
| 18 | namespace bp2 = boost::process::v2; |
| 19 | BOOST_CHECK_NE(bp2::current_pid(), static_cast<bp2::pid_type>(0)); |
| 20 | |
| 21 | auto all = bp2::all_pids(); |
| 22 | auto itr = std::find(first: all.begin(), last: all.end(), val: bp2::current_pid()); |
| 23 | |
| 24 | BOOST_CHECK_GT(all.size(), 0u); |
| 25 | BOOST_CHECK(itr != all.end()); |
| 26 | |
| 27 | } |
| 28 | |
| 29 | BOOST_AUTO_TEST_CASE(child_pid) |
| 30 | { |
| 31 | namespace bp2 = boost::process::v2; |
| 32 | |
| 33 | using boost::unit_test::framework::master_test_suite; |
| 34 | const auto pth = bp2::filesystem::absolute(p: master_test_suite().argv[1]); |
| 35 | std::this_thread::sleep_for(rtime: std::chrono::milliseconds(100)); |
| 36 | |
| 37 | auto cs = bp2::child_pids(pid: bp2::current_pid()); |
| 38 | boost::asio::io_context ctx; |
| 39 | bp2::process proc(ctx, pth, {"loop" }); |
| 40 | std::this_thread::sleep_for(rtime: std::chrono::milliseconds(200)); |
| 41 | auto c2 = bp2::child_pids(pid: bp2::current_pid()); |
| 42 | BOOST_CHECK_LE(cs.size(), c2.size()); |
| 43 | BOOST_CHECK(std::find(cs.begin(), cs.end(), proc.id()) == cs.end()); |
| 44 | if (!c2.empty()) |
| 45 | BOOST_CHECK(std::find(c2.begin(), c2.end(), proc.id()) != c2.end()); |
| 46 | boost::system::error_code ec; |
| 47 | proc.terminate(ec); |
| 48 | if (ec) |
| 49 | BOOST_CHECK(ec == boost::system::errc::permission_denied); |
| 50 | else |
| 51 | proc.wait(); |
| 52 | |
| 53 | auto c3 = bp2::child_pids(pid: bp2::current_pid()); |
| 54 | BOOST_CHECK(std::find(c3.begin(), c3.end(), proc.id()) == c3.end()); |
| 55 | BOOST_CHECK_LE(c3.size(), c2.size()); |
| 56 | } |