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
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)
10 #define BOOST_TEST_MAIN
12 #include <boost/test/included/unit_test.hpp>
14 #include <boost/process/error.hpp>
15 #include <boost/process/async.hpp>
16 #include <boost/process/child.hpp>
18 #include <boost/system/error_code.hpp>
19 #include <boost/asio.hpp>
20 #if defined(BOOST_WINDOWS_API)
22 typedef boost::asio::windows::stream_handle pipe_end
;
23 #elif defined(BOOST_POSIX_API)
24 # include <sys/wait.h>
26 typedef boost::asio::posix::stream_descriptor pipe_end
;
29 namespace bp
= boost::process
;
31 BOOST_AUTO_TEST_CASE(sync_wait
)
33 using boost::unit_test::framework::master_test_suite
;
37 master_test_suite().argv
[1],
38 "test", "--exit-code", "123",
43 int exit_code
= c
.exit_code();
46 BOOST_CHECK_EQUAL(123, exit_code
);
51 BOOST_AUTO_TEST_CASE(sync_wait_abort
)
53 using boost::unit_test::framework::master_test_suite
;
57 master_test_suite().argv
[1],
63 int exit_code
= c
.exit_code();
66 BOOST_CHECK(exit_code
!= 0);
71 #if defined(BOOST_WINDOWS_API)
76 wait_handler(HANDLE handle
, bool &called
) : handle_(handle
), called_(called
) {}
78 void operator()(const boost::system::error_code
&ec
)
83 BOOST_REQUIRE(GetExitCodeProcess(handle_
, &exit_code
));
84 BOOST_CHECK_EQUAL(123, exit_code
);
87 #elif defined(BOOST_POSIX_API)
92 wait_handler (bool & called
) : called_(called
) {}
94 void operator()(const boost::system::error_code
&ec
, int signal
)
98 BOOST_REQUIRE_EQUAL(SIGCHLD
, signal
);
101 BOOST_CHECK_EQUAL(123, WEXITSTATUS(status
));
106 BOOST_AUTO_TEST_CASE(async_wait
)
108 using boost::unit_test::framework::master_test_suite
;
109 using namespace boost::asio
;
111 boost::asio::io_context io_context
;
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();});
117 bool wh_called
= false;
118 #if defined(BOOST_POSIX_API)
119 signal_set
set(io_context
, SIGCHLD
);
120 set
.async_wait(wait_handler(wh_called
));
125 master_test_suite().argv
[1],
126 "test", "--exit-code", "123",
131 #if defined(BOOST_WINDOWS_API)
132 windows::object_handle
handle(io_context
.get_executor(), c
.native_handle());
133 handle
.async_wait(wait_handler(handle
.native_handle(), wh_called
));
136 BOOST_CHECK_MESSAGE(wh_called
, "Wait handler not called");
141 BOOST_AUTO_TEST_CASE(async_nowait
)
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
;
148 boost::asio::io_context io_context
;
150 master_test_suite().argv
[1],
151 "test", "--exit-code", "221",
153 bp::on_exit
=[](int exit_code
, std::error_code
) mutable {},
158 BOOST_CHECK_EQUAL(221, c
.exit_code());