group_wait.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <fstream>
  13. #include <boost/system/error_code.hpp>
  14. #include <boost/asio.hpp>
  15. #include <boost/algorithm/string/predicate.hpp>
  16. #include <boost/process/error.hpp>
  17. #include <boost/process/io.hpp>
  18. #include <boost/process/args.hpp>
  19. #include <boost/process/child.hpp>
  20. #include <boost/process/group.hpp>
  21. #include <system_error>
  22. #include <string>
  23. #include <thread>
  24. #include <istream>
  25. #include <iostream>
  26. #include <cstdlib>
  27. namespace bp = boost::process;
  28. BOOST_AUTO_TEST_CASE(wait_group_test, *boost::unit_test::timeout(5))
  29. {
  30. std::atomic<bool> done{false};
  31. std::thread thr{
  32. [&]
  33. {
  34. for (int i = 0; i < 50 && !done.load(); i++)
  35. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  36. BOOST_REQUIRE(done.load());
  37. }};
  38. using boost::unit_test::framework::master_test_suite;
  39. std::error_code ec;
  40. bp::group g;
  41. bp::child c1(
  42. master_test_suite().argv[1],
  43. "--wait", "2",
  44. g,
  45. ec
  46. );
  47. bp::child c2(
  48. master_test_suite().argv[1],
  49. "--wait", "2",
  50. g,
  51. ec
  52. );
  53. BOOST_CHECK(c1.running());
  54. BOOST_CHECK(c2.running());
  55. BOOST_REQUIRE(!ec);
  56. BOOST_REQUIRE(c1.in_group(ec));
  57. BOOST_CHECK_MESSAGE(!ec, ec.message());
  58. BOOST_REQUIRE(c2.in_group(ec));
  59. BOOST_CHECK_MESSAGE(!ec, ec.message());
  60. g.wait();
  61. BOOST_CHECK(!c1.running());
  62. BOOST_CHECK(!c2.running());
  63. done.store(true);
  64. thr.join();
  65. }
  66. BOOST_AUTO_TEST_CASE(wait_group_test_timeout, *boost::unit_test::timeout(15))
  67. {
  68. using boost::unit_test::framework::master_test_suite;
  69. std::atomic<bool> done{false};
  70. std::thread thr{
  71. [&]
  72. {
  73. for (int i = 0; i < 150 && !done.load(); i++)
  74. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  75. BOOST_REQUIRE(done.load());
  76. }};
  77. std::error_code ec;
  78. bp::group g;
  79. bp::child c1(
  80. master_test_suite().argv[1],
  81. "--wait", "1",
  82. g,
  83. ec
  84. );
  85. bp::child c2(
  86. master_test_suite().argv[1],
  87. "--wait", "4",
  88. g,
  89. ec
  90. );
  91. BOOST_CHECK(c1.running());
  92. BOOST_CHECK(c2.running());
  93. BOOST_REQUIRE(!ec);
  94. BOOST_REQUIRE(c1.in_group());
  95. BOOST_REQUIRE(c2.in_group());
  96. BOOST_CHECK(!g.wait_for(std::chrono::seconds(2), ec));
  97. BOOST_CHECK_MESSAGE(!ec, std::to_string(ec.value()) + " == " + ec.message());
  98. BOOST_CHECK(!c1.running());
  99. BOOST_CHECK(c2.running());
  100. BOOST_CHECK(g.wait_for(std::chrono::seconds(5), ec));
  101. BOOST_CHECK_MESSAGE(!ec, std::to_string(ec.value()) + " == " + ec.message());
  102. BOOST_CHECK(!c1.running());
  103. BOOST_CHECK(!c2.running());
  104. done.store(true);
  105. thr.join();
  106. }