File: pid.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (51 lines) | stat: -rw-r--r-- 1,648 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2022 Klemens D. Morgenstern
// Copyright (c) 2022 Samuel Venable
//
// 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)

#include <boost/process/v2/pid.hpp>
#include <boost/process/v2/process.hpp>

#include <boost/test/unit_test.hpp>

#include <algorithm>
#include <thread>
#include <vector>

BOOST_AUTO_TEST_CASE(test_pid)
{
    namespace bp2 = boost::process::v2;
    BOOST_CHECK_NE(bp2::current_pid(), static_cast<bp2::pid_type>(0));

    auto all = bp2::all_pids();
    auto itr = std::find(all.begin(), all.end(), bp2::current_pid());

    BOOST_CHECK_GT(all.size(), 0u);
    BOOST_CHECK(itr != all.end());

}

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]);
    std::this_thread::sleep_for(std::chrono::milliseconds(100));

    auto cs = bp2::child_pids(bp2::current_pid());
    boost::asio::io_context ctx;
    bp2::process proc(ctx, pth, {"loop"});
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    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());
}