async_fut.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/process/error.hpp>
  13. #include <boost/process/async.hpp>
  14. #include <boost/process/io.hpp>
  15. #include <boost/process/spawn.hpp>
  16. #include <boost/process/child.hpp>
  17. #include <boost/thread.hpp>
  18. #include <future>
  19. #include <boost/system/error_code.hpp>
  20. #include <boost/algorithm/string/predicate.hpp>
  21. BOOST_AUTO_TEST_SUITE( async );
  22. using namespace std;
  23. namespace bp = boost::process;
  24. BOOST_AUTO_TEST_CASE(async_out_future, *boost::unit_test::timeout(2))
  25. {
  26. using boost::unit_test::framework::master_test_suite;
  27. boost::asio::io_context io_context;
  28. std::error_code ec;
  29. std::future<std::string> fut;
  30. std::future<void> fut_in;
  31. boost::asio::streambuf in_buf;
  32. std::ostream ostr(&in_buf);
  33. ostr << "-string" << endl ;
  34. bp::spawn(
  35. master_test_suite().argv[1],
  36. "test", "--prefix-once", "test",
  37. bp::std_in < in_buf > fut_in,
  38. bp::std_out > fut,
  39. io_context,
  40. ec
  41. );
  42. BOOST_REQUIRE(!ec);
  43. io_context.run();
  44. BOOST_REQUIRE(fut.valid());
  45. BOOST_REQUIRE(fut_in.valid());
  46. BOOST_CHECK_NO_THROW(fut_in.get());
  47. std::string line;
  48. BOOST_CHECK_NO_THROW(line = fut.get());
  49. std::string val = "test-string";
  50. BOOST_REQUIRE_GE(line.size(), val.size());
  51. if (line >= val)
  52. BOOST_CHECK(boost::algorithm::starts_with(line, val));
  53. }
  54. BOOST_AUTO_TEST_CASE(emtpy_out, *boost::unit_test::timeout(2))
  55. {
  56. using boost::unit_test::framework::master_test_suite;
  57. boost::asio::io_context io_context;
  58. std::error_code ec;
  59. std::future<std::string> fut;
  60. bp::spawn(
  61. master_test_suite().argv[1],
  62. "test", "--exit-code", "0",
  63. bp::std_out > fut,
  64. io_context,
  65. ec
  66. );
  67. BOOST_REQUIRE(!ec);
  68. io_context.run();
  69. BOOST_REQUIRE(fut.valid());
  70. BOOST_CHECK_EQUAL(fut.get(), "");
  71. }
  72. BOOST_AUTO_TEST_SUITE_END();