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