]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/process/test/group.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / process / test / group.cpp
1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10 #define BOOST_TEST_MAIN
11 #define BOOST_TEST_IGNORE_SIGCHLD
12 #include <boost/test/included/unit_test.hpp>
13
14 #include <boost/system/error_code.hpp>
15
16 #include <boost/asio.hpp>
17 #include <boost/algorithm/string/predicate.hpp>
18
19 #include <boost/process/error.hpp>
20 #include <boost/process/io.hpp>
21 #include <boost/process/args.hpp>
22 #include <boost/process/child.hpp>
23 #include <boost/process/group.hpp>
24 #include <system_error>
25
26 #include <string>
27 #include <thread>
28 #include <istream>
29 #include <iostream>
30 #include <cstdlib>
31 #if defined(BOOST_WINDOWS_API)
32 # include <windows.h>
33 typedef boost::asio::windows::stream_handle pipe_end;
34 #elif defined(BOOST_POSIX_API)
35 # include <sys/wait.h>
36 # include <unistd.h>
37 typedef boost::asio::posix::stream_descriptor pipe_end;
38 #endif
39
40 namespace bp = boost::process;
41
42 BOOST_AUTO_TEST_CASE(group_test, *boost::unit_test::timeout(5))
43 {
44 std::cout << "group_test" << std::endl;
45 using boost::unit_test::framework::master_test_suite;
46
47 std::error_code ec;
48 bp::group g;
49
50
51 bp::child c(
52 master_test_suite().argv[1],
53 g,
54 ec
55 );
56 BOOST_CHECK(c.running());
57
58 BOOST_REQUIRE(!ec);
59 BOOST_REQUIRE(c.in_group());
60 BOOST_CHECK(c);
61 BOOST_CHECK(c.running());
62
63 BOOST_REQUIRE_NO_THROW(g.terminate());
64 std::this_thread::sleep_for(std::chrono::milliseconds(50));
65
66 BOOST_CHECK(!c.running());
67 if (c.running())
68 c.terminate();
69 }
70
71 BOOST_AUTO_TEST_CASE(attached, *boost::unit_test::timeout(5))
72 {
73 using boost::unit_test::framework::master_test_suite;
74
75 bp::ipstream is;
76
77 bp::group g;
78
79 std::error_code ec;
80 bp::child c(
81 master_test_suite().argv[1],
82 bp::args+={"--launch-attached"},
83 bp::std_out>is,
84 g,
85 ec
86 );
87 BOOST_REQUIRE(!ec);
88 BOOST_REQUIRE(c.in_group(ec));
89 BOOST_CHECK(c);
90
91
92
93 bp::pid_t pid;
94 is >> pid;
95 bp::child sub_c(pid);
96 is >> pid; //invalid pid.
97
98
99 BOOST_REQUIRE(sub_c);
100 std::this_thread::sleep_for(std::chrono::milliseconds(50)); //just to be sure.
101
102
103 #if defined( BOOST_POSIX_API )
104 BOOST_CHECK(kill(sub_c.id(), 0) == 0);
105 #else
106 BOOST_CHECK(sub_c.running());
107 #endif
108
109 BOOST_REQUIRE_NO_THROW(g.terminate());
110
111 BOOST_CHECK(sub_c);
112 std::this_thread::sleep_for(std::chrono::milliseconds(50)); //just to be sure.
113
114 BOOST_CHECK(!c.running());
115
116 #if defined( BOOST_POSIX_API )
117 bool still_runs = kill(sub_c.id(), 0) == 0;
118 #else
119 bool still_runs = sub_c.running();
120 #endif
121
122 BOOST_CHECK(!still_runs);
123 if (still_runs)
124 sub_c.terminate();
125 BOOST_CHECK(!c.running());
126 if (c.running())
127 c.terminate();
128
129 }
130
131
132
133 BOOST_AUTO_TEST_CASE(detached, *boost::unit_test::timeout(5))
134 {
135 std::cerr << "detached" << std::endl;
136
137 using boost::unit_test::framework::master_test_suite;
138
139 bp::ipstream is;
140
141 bp::group g;
142
143
144 std::error_code ec;
145 bp::child c(
146 master_test_suite().argv[1],
147 bp::args+={"--launch-detached"},
148 bp::std_out>is,
149 g,
150 ec
151 );
152
153 BOOST_REQUIRE(!ec);
154 BOOST_CHECK(c);
155
156 bp::pid_t pid;
157 is >> pid;
158 is >> pid;
159 bp::child sub_c(pid);
160
161 std::this_thread::sleep_for(std::chrono::milliseconds(50)); //just to be sure.
162
163 #if defined( BOOST_POSIX_API )
164 BOOST_CHECK(kill(sub_c.id(), 0) == 0);
165 #else
166 BOOST_CHECK(sub_c.running());
167 #endif
168
169 BOOST_REQUIRE_NO_THROW(g.terminate());
170
171 BOOST_CHECK(sub_c);
172 std::this_thread::sleep_for(std::chrono::milliseconds(50)); //just to be sure.
173
174 #if defined( BOOST_POSIX_API )
175 bool still_runs = kill(sub_c.id(), 0) == 0;
176 #else
177 bool still_runs = sub_c.running();
178 #endif
179
180 BOOST_CHECK(still_runs);
181 if (still_runs)
182 sub_c.terminate();
183
184 BOOST_CHECK(!c.running());
185 if (c.running())
186 c.terminate();
187 }