exit_code.cpp 3.9 KB

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