File: pid.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (57 lines) | stat: -rw-r--r-- 1,834 bytes parent folder | download | duplicates (2)
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
52
53
54
55
56
57
// 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/asio/io_context.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(200));
    auto c2 = bp2::child_pids(bp2::current_pid());
    BOOST_CHECK_LE(cs.size(), c2.size());
    BOOST_CHECK(std::find(cs.begin(), cs.end(), proc.id()) == cs.end());
    if (!c2.empty())
      BOOST_CHECK(std::find(c2.begin(), c2.end(), proc.id()) != c2.end());
    boost::system::error_code ec;
    proc.terminate(ec);
    if (ec)
      BOOST_CHECK(ec == boost::system::errc::permission_denied);
    else
      proc.wait(ec);

    auto c3 = bp2::child_pids(bp2::current_pid());
    BOOST_CHECK(std::find(c3.begin(), c3.end(), proc.id()) == c3.end());
    BOOST_CHECK_LE(c3.size(), c2.size());
}