group.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #define BOOST_TEST_IGNORE_SIGCHLD
  11. #include <boost/test/included/unit_test.hpp>
  12. #include <boost/system/error_code.hpp>
  13. #include <boost/asio.hpp>
  14. #include <boost/algorithm/string/predicate.hpp>
  15. #include <boost/process/error.hpp>
  16. #include <boost/process/io.hpp>
  17. #include <boost/process/args.hpp>
  18. #include <boost/process/child.hpp>
  19. #include <boost/process/group.hpp>
  20. #include <system_error>
  21. #include <string>
  22. #include <thread>
  23. #include <istream>
  24. #include <iostream>
  25. #include <cstdlib>
  26. namespace bp = boost::process;
  27. BOOST_AUTO_TEST_CASE(group_test, *boost::unit_test::timeout(5))
  28. {
  29. std::cout << "group_test" << std::endl;
  30. using boost::unit_test::framework::master_test_suite;
  31. std::error_code ec;
  32. bp::group g;
  33. bp::child c(
  34. master_test_suite().argv[1],
  35. g,
  36. ec
  37. );
  38. BOOST_CHECK(c.running());
  39. BOOST_REQUIRE(!ec);
  40. BOOST_REQUIRE(c.in_group());
  41. BOOST_CHECK(c);
  42. BOOST_CHECK(c.running());
  43. BOOST_REQUIRE_NO_THROW(g.terminate());
  44. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  45. BOOST_CHECK(!c.running());
  46. if (c.running())
  47. c.terminate();
  48. std::cout << "group_test out" << std::endl;
  49. }
  50. BOOST_AUTO_TEST_CASE(attached, *boost::unit_test::timeout(5))
  51. {
  52. std::cout << "attached" << std::endl;
  53. using boost::unit_test::framework::master_test_suite;
  54. bp::ipstream is;
  55. bp::group g;
  56. std::error_code ec;
  57. bp::child c(
  58. master_test_suite().argv[1],
  59. bp::args+={"--launch-attached"},
  60. bp::std_out>is,
  61. g,
  62. ec
  63. );
  64. BOOST_REQUIRE(!ec);
  65. BOOST_REQUIRE(c.in_group(ec));
  66. BOOST_CHECK(c);
  67. bp::pid_t pid;
  68. is >> pid;
  69. bp::child sub_c(pid);
  70. is >> pid; //invalid pid.
  71. BOOST_REQUIRE(sub_c);
  72. std::this_thread::sleep_for(std::chrono::milliseconds(100)); //just to be sure.
  73. #if defined( BOOST_POSIX_API )
  74. ::waitpid(sub_c.id(), nullptr, WNOHANG);
  75. BOOST_CHECK(kill(sub_c.id(), 0) == 0);
  76. #else
  77. BOOST_CHECK(sub_c.running());
  78. #endif
  79. BOOST_REQUIRE_NO_THROW(g.terminate());
  80. BOOST_CHECK(sub_c);
  81. std::this_thread::sleep_for(std::chrono::milliseconds(100)); //just to be sure.
  82. BOOST_CHECK(!c.running());
  83. #if defined( BOOST_POSIX_API )
  84. errno = 0;
  85. ::waitpid(sub_c.id(), nullptr, WNOHANG);
  86. bool still_runs = (kill(sub_c.id(), 0) == 0) && (errno != ECHILD) && (errno != ESRCH);
  87. #else
  88. bool still_runs = sub_c.running();
  89. #endif
  90. BOOST_CHECK_MESSAGE(!still_runs, boost::process::detail::get_last_error().message());
  91. if (still_runs)
  92. sub_c.terminate();
  93. BOOST_CHECK(!c.running());
  94. if (c.running())
  95. c.terminate();
  96. std::cout << "attached out" << std::endl;
  97. }
  98. BOOST_AUTO_TEST_CASE(detached, *boost::unit_test::timeout(5))
  99. {
  100. std::cerr << "detached" << std::endl;
  101. using boost::unit_test::framework::master_test_suite;
  102. bp::ipstream is;
  103. bp::group g;
  104. std::error_code ec;
  105. bp::child c(
  106. master_test_suite().argv[1],
  107. bp::args+={"--launch-detached"},
  108. bp::std_out>is,
  109. g,
  110. ec
  111. );
  112. BOOST_REQUIRE(!ec);
  113. BOOST_CHECK(c);
  114. bp::pid_t pid;
  115. is >> pid;
  116. is >> pid;
  117. bp::child sub_c(pid);
  118. std::this_thread::sleep_for(std::chrono::milliseconds(50)); //just to be sure.
  119. #if defined( BOOST_POSIX_API )
  120. BOOST_CHECK(kill(sub_c.id(), 0) == 0);
  121. #else
  122. BOOST_CHECK(sub_c.running());
  123. #endif
  124. BOOST_REQUIRE_NO_THROW(g.terminate());
  125. BOOST_CHECK(sub_c);
  126. std::this_thread::sleep_for(std::chrono::milliseconds(50)); //just to be sure.
  127. #if defined( BOOST_POSIX_API )
  128. bool still_runs = kill(sub_c.id(), 0) == 0;
  129. #else
  130. bool still_runs = sub_c.running();
  131. #endif
  132. BOOST_CHECK(still_runs);
  133. if (still_runs)
  134. sub_c.terminate();
  135. BOOST_CHECK(!c.running());
  136. if (c.running())
  137. c.terminate();
  138. std::cerr << "detached out" << std::endl;
  139. }