From 27a35f452d57fb7e1748147d68ef1537e5cc79b5 Mon Sep 17 00:00:00 2001 From: Klemens Morgenstern Date: Tue, 21 Feb 2023 10:29:25 +0800 Subject: [PATCH] Updated pid test. --- test/v2/Jamfile.jam | 2 +- test/v2/pid.cpp | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/test/v2/Jamfile.jam b/test/v2/Jamfile.jam index 30262035..916e306c 100644 --- a/test/v2/Jamfile.jam +++ b/test/v2/Jamfile.jam @@ -59,11 +59,11 @@ test-suite standalone : [ run utf8.cpp test_impl ] [ run cstring_ref.cpp test_impl ] [ run environment.cpp test_impl ] - [ run pid.cpp test_impl : : : darwin:no ] [ run shell.cpp test_impl ] ; test-suite with_target : + [ run pid.cpp test_impl : --log_level=all --catch_system_errors=no -- : target ] [ run process.cpp test_impl : --log_level=all --catch_system_errors=no -- : target ] [ run windows.cpp test_impl : --log_level=all --catch_system_errors=no -- : target : no windows:yes windows:Advapi32 ] [ run ext.cpp test_impl : --log_level=all --catch_system_errors=no -- : target : darwin:no ] diff --git a/test/v2/pid.cpp b/test/v2/pid.cpp index 532bfd48..4102acec 100644 --- a/test/v2/pid.cpp +++ b/test/v2/pid.cpp @@ -5,10 +5,12 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include +#include #include #include +#include #include BOOST_AUTO_TEST_CASE(test_pid) @@ -22,21 +24,29 @@ BOOST_AUTO_TEST_CASE(test_pid) BOOST_CHECK_GT(all.size(), 0u); BOOST_CHECK(itr != all.end()); - std::vector children, grand_children; - auto grand_child_pids = [](bp2::pid_type pid, - std::vector & children, - std::vector & grand_children) - { - children = bp2::child_pids(pid); - for (unsigned i = 0; i < children.size(); i++) - { - std::vector tmp1; - std::vector tmp2 = bp2::child_pids(children[i]); - tmp1.insert(std::end(tmp1), std::begin(tmp2), std::end(tmp2)); - grand_children = tmp1; - } - return (!children.empty() || !grand_children.empty()); - }; - BOOST_CHECK_NE(grand_child_pids(bp2::root_pid, children, grand_children), false); + } + +BOOST_AUTO_TEST_CASE(child_pid) +{ + namespace bp2 = boost::process::v2; + + using boost::unit_test::framework::master_test_suite; + const auto pth = bp2::filesystem::absolute(master_test_suite().argv[1]); + + auto cs = bp2::child_pids(bp2::current_pid()); + boost::asio::io_context ctx; + bp2::process proc(ctx, pth, {"sleep", "50000"}); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + auto c2 = bp2::child_pids(bp2::current_pid()); + BOOST_CHECK_LT(cs.size(), c2.size()); + BOOST_CHECK(std::find(cs.begin(), cs.end(), proc.id()) == cs.end()); + BOOST_CHECK(std::find(c2.begin(), c2.end(), proc.id()) != c2.end()); + proc.terminate(); + proc.wait(); + + auto c3 = bp2::child_pids(bp2::current_pid()); + BOOST_CHECK(std::find(c3.begin(), c3.end(), proc.id()) == c3.end()); + BOOST_CHECK_LT(c3.size(), c2.size()); +} \ No newline at end of file