Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / process / test / exit_code.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/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
51 #if defined(BOOST_WINDOWS_API)
52 struct wait_handler
53 {
54     HANDLE handle_;
55
56     wait_handler(HANDLE handle) : handle_(handle) {}
57
58     void operator()(const boost::system::error_code &ec)
59     {
60         BOOST_REQUIRE(!ec);
61         DWORD exit_code;
62         BOOST_REQUIRE(GetExitCodeProcess(handle_, &exit_code));
63         BOOST_CHECK_EQUAL(123, exit_code);
64     }
65 };
66 #elif defined(BOOST_POSIX_API)
67 struct wait_handler
68 {
69     void operator()(const boost::system::error_code &ec, int signal)
70     {
71         BOOST_REQUIRE(!ec);
72         BOOST_REQUIRE_EQUAL(SIGCHLD, signal);
73         int status;
74         wait(&status);
75         BOOST_CHECK_EQUAL(123, WEXITSTATUS(status));
76     }
77 };
78 #endif
79
80 BOOST_AUTO_TEST_CASE(async_wait)
81 {
82     using boost::unit_test::framework::master_test_suite;
83     using namespace boost::asio;
84
85     boost::asio::io_service io_service;
86
87 #if defined(BOOST_POSIX_API)
88     signal_set set(io_service, SIGCHLD);
89     set.async_wait(wait_handler());
90 #endif
91
92     std::error_code ec;
93     bp::child c(
94         master_test_suite().argv[1],
95         "test", "--exit-code", "123",
96         ec
97     );
98     BOOST_REQUIRE(!ec);
99
100 #if defined(BOOST_WINDOWS_API)
101     windows::object_handle handle(io_service, c.native_handle());
102     handle.async_wait(wait_handler(handle.native()));
103 #endif
104     std::cout << "async_wait 1" << std::endl;
105     io_service.run();
106     std::cout << "async_wait 2" << std::endl;
107 }