]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
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 | |
92f5a8d4 | 11 | |
b32b8144 FG |
12 | #include <boost/test/included/unit_test.hpp> |
13 | ||
14 | #include <boost/process/error.hpp> | |
15 | #include <boost/process/async.hpp> | |
16 | #include <boost/process/child.hpp> | |
17 | ||
18 | #include <boost/system/error_code.hpp> | |
19 | #include <boost/asio.hpp> | |
20 | #if defined(BOOST_WINDOWS_API) | |
21 | # include <windows.h> | |
22 | typedef boost::asio::windows::stream_handle pipe_end; | |
23 | #elif defined(BOOST_POSIX_API) | |
24 | # include <sys/wait.h> | |
25 | # include <signal.h> | |
26 | typedef boost::asio::posix::stream_descriptor pipe_end; | |
27 | #endif | |
28 | ||
29 | namespace bp = boost::process; | |
30 | ||
31 | BOOST_AUTO_TEST_CASE(sync_wait) | |
32 | { | |
33 | using boost::unit_test::framework::master_test_suite; | |
34 | ||
35 | std::error_code ec; | |
36 | bp::child c( | |
37 | master_test_suite().argv[1], | |
38 | "test", "--exit-code", "123", | |
39 | ec | |
40 | ); | |
41 | BOOST_REQUIRE(!ec); | |
42 | c.wait(); | |
43 | int exit_code = c.exit_code(); | |
44 | ||
45 | ||
46 | BOOST_CHECK_EQUAL(123, exit_code); | |
47 | ||
48 | c.wait(); | |
49 | } | |
50 | ||
11fdf7f2 TL |
51 | BOOST_AUTO_TEST_CASE(sync_wait_abort) |
52 | { | |
53 | using boost::unit_test::framework::master_test_suite; | |
54 | ||
55 | std::error_code ec; | |
56 | bp::child c( | |
57 | master_test_suite().argv[1], | |
58 | "test", "--abort", | |
59 | ec | |
60 | ); | |
61 | BOOST_REQUIRE(!ec); | |
62 | c.wait(); | |
63 | int exit_code = c.exit_code(); | |
64 | ||
65 | ||
66 | BOOST_CHECK(exit_code != 0); | |
67 | ||
68 | c.wait(); | |
69 | } | |
70 | ||
b32b8144 FG |
71 | #if defined(BOOST_WINDOWS_API) |
72 | struct wait_handler | |
73 | { | |
74 | HANDLE handle_; | |
92f5a8d4 TL |
75 | bool &called_; |
76 | wait_handler(HANDLE handle, bool &called) : handle_(handle), called_(called) {} | |
b32b8144 FG |
77 | |
78 | void operator()(const boost::system::error_code &ec) | |
79 | { | |
92f5a8d4 | 80 | called_ = true; |
b32b8144 FG |
81 | BOOST_REQUIRE(!ec); |
82 | DWORD exit_code; | |
83 | BOOST_REQUIRE(GetExitCodeProcess(handle_, &exit_code)); | |
84 | BOOST_CHECK_EQUAL(123, exit_code); | |
85 | } | |
86 | }; | |
87 | #elif defined(BOOST_POSIX_API) | |
88 | struct wait_handler | |
89 | { | |
92f5a8d4 TL |
90 | bool &called_; |
91 | ||
92 | wait_handler (bool & called) : called_(called) {} | |
93 | ||
b32b8144 FG |
94 | void operator()(const boost::system::error_code &ec, int signal) |
95 | { | |
92f5a8d4 | 96 | called_ = true; |
b32b8144 FG |
97 | BOOST_REQUIRE(!ec); |
98 | BOOST_REQUIRE_EQUAL(SIGCHLD, signal); | |
99 | int status; | |
100 | wait(&status); | |
101 | BOOST_CHECK_EQUAL(123, WEXITSTATUS(status)); | |
102 | } | |
103 | }; | |
104 | #endif | |
105 | ||
106 | BOOST_AUTO_TEST_CASE(async_wait) | |
107 | { | |
108 | using boost::unit_test::framework::master_test_suite; | |
109 | using namespace boost::asio; | |
110 | ||
111 | boost::asio::io_context io_context; | |
112 | ||
92f5a8d4 TL |
113 | boost::asio::deadline_timer timeout{io_context, boost::posix_time::seconds(3)}; |
114 | timeout.async_wait([&](boost::system::error_code ec){if (!ec) io_context.stop();}); | |
115 | ||
116 | ||
117 | bool wh_called = false; | |
b32b8144 FG |
118 | #if defined(BOOST_POSIX_API) |
119 | signal_set set(io_context, SIGCHLD); | |
92f5a8d4 | 120 | set.async_wait(wait_handler(wh_called)); |
b32b8144 FG |
121 | #endif |
122 | ||
123 | std::error_code ec; | |
124 | bp::child c( | |
125 | master_test_suite().argv[1], | |
126 | "test", "--exit-code", "123", | |
127 | ec | |
128 | ); | |
129 | BOOST_REQUIRE(!ec); | |
130 | ||
131 | #if defined(BOOST_WINDOWS_API) | |
92f5a8d4 TL |
132 | windows::object_handle handle(io_context.get_executor(), c.native_handle()); |
133 | handle.async_wait(wait_handler(handle.native_handle(), wh_called)); | |
b32b8144 | 134 | #endif |
b32b8144 | 135 | io_context.run(); |
92f5a8d4 | 136 | BOOST_CHECK_MESSAGE(wh_called, "Wait handler not called"); |
b32b8144 | 137 | } |
92f5a8d4 TL |
138 | |
139 | ||
140 | ||
141 | BOOST_AUTO_TEST_CASE(async_nowait) | |
142 | { | |
143 | // No need to call wait when passing an io_context | |
144 | using boost::unit_test::framework::master_test_suite; | |
145 | using namespace boost::asio; | |
146 | ||
147 | std::error_code ec; | |
148 | boost::asio::io_context io_context; | |
149 | bp::child c( | |
150 | master_test_suite().argv[1], | |
151 | "test", "--exit-code", "221", | |
152 | ec, | |
153 | bp::on_exit=[](int exit_code, std::error_code) mutable {}, | |
154 | io_context | |
155 | ); | |
156 | BOOST_REQUIRE(!ec); | |
157 | io_context.run(); | |
158 | BOOST_CHECK_EQUAL(221, c.exit_code()); | |
159 | } |