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/io.hpp> 17 #include <boost/process/spawn.hpp> 18 #include <boost/process/child.hpp> 19 20 #include <boost/thread.hpp> 21 #include <future> 22 23 #include <boost/system/error_code.hpp> 24 25 #include <boost/algorithm/string/predicate.hpp> 26 27 BOOST_AUTO_TEST_SUITE( async ); 28 29 30 using namespace std; 31 32 namespace bp = boost::process; 33 34 BOOST_AUTO_TEST_CASE(async_out_future, *boost::unit_test::timeout(2)) 35 { 36 37 using boost::unit_test::framework::master_test_suite; 38 39 boost::asio::io_context io_context; 40 41 42 std::error_code ec; 43 44 std::future<std::string> fut; 45 std::future<void> fut_in; 46 boost::asio::streambuf in_buf; 47 48 49 std::ostream ostr(&in_buf); 50 ostr << "-string" << endl ; 51 52 bp::spawn( 53 master_test_suite().argv[1], 54 "test", "--prefix-once", "test", 55 bp::std_in < in_buf > fut_in, 56 bp::std_out > fut, 57 io_context, 58 ec 59 ); 60 BOOST_REQUIRE(!ec); 61 62 63 io_context.run(); 64 65 BOOST_REQUIRE(fut.valid()); 66 BOOST_REQUIRE(fut_in.valid()); 67 BOOST_CHECK_NO_THROW(fut_in.get()); 68 69 std::string line; 70 71 BOOST_CHECK_NO_THROW(line = fut.get()); 72 73 std::string val = "test-string"; 74 BOOST_REQUIRE_GE(line.size(), val.size()); 75 if (line >= val) 76 BOOST_CHECK(boost::algorithm::starts_with(line, val)); 77 } 78 79 80 BOOST_AUTO_TEST_CASE(emtpy_out, *boost::unit_test::timeout(2)) 81 { 82 using boost::unit_test::framework::master_test_suite; 83 84 boost::asio::io_context io_context; 85 86 87 std::error_code ec; 88 std::future<std::string> fut; 89 90 bp::spawn( 91 master_test_suite().argv[1], 92 "test", "--exit-code", "0", 93 bp::std_out > fut, 94 io_context, 95 ec 96 ); 97 BOOST_REQUIRE(!ec); 98 99 100 io_context.run(); 101 102 BOOST_REQUIRE(fut.valid()); 103 BOOST_CHECK_EQUAL(fut.get(), ""); 104 } 105 106 BOOST_AUTO_TEST_SUITE_END();