async_system_stackless.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) 2016 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #define BOOST_TEST_MAIN
  6. #define BOOST_TEST_IGNORE_SIGCHLD
  7. #include <boost/test/included/unit_test.hpp>
  8. #include <boost/process/error.hpp>
  9. #include <boost/process/io.hpp>
  10. #include <boost/process/async.hpp>
  11. #include <boost/process/child.hpp>
  12. #include <boost/process/async_system.hpp>
  13. #include <string>
  14. #include <boost/asio/io_context.hpp>
  15. #include <boost/asio/post.hpp>
  16. #include <boost/asio/spawn.hpp>
  17. #include <boost/asio/coroutine.hpp>
  18. #include <boost/asio/use_future.hpp>
  19. #include <boost/asio/yield.hpp>
  20. #include <vector>
  21. #include <array>
  22. BOOST_AUTO_TEST_SUITE( async );
  23. namespace bp = boost::process;
  24. BOOST_AUTO_TEST_CASE(stackless, *boost::unit_test::timeout(15))
  25. {
  26. using boost::unit_test::framework::master_test_suite;
  27. boost::asio::io_context ios;
  28. bool did_something_else = false;
  29. struct stackless_t : boost::asio::coroutine
  30. {
  31. boost::asio::io_context & ios;
  32. bool & did_something_else;
  33. stackless_t(boost::asio::io_context & ios_,
  34. bool & did_something_else)
  35. : ios(ios_), did_something_else(did_something_else) {}
  36. void operator()(
  37. boost::system::error_code ec = boost::system::error_code(),
  38. std::size_t exit_code = 0)
  39. {
  40. if (!ec) reenter (this)
  41. {
  42. yield bp::async_system(
  43. ios, *this,
  44. master_test_suite().argv[1],
  45. "test", "--exit-code", "42");
  46. BOOST_CHECK_EQUAL(exit_code, 42);
  47. BOOST_CHECK(did_something_else);
  48. }
  49. }
  50. } stackless{ios, did_something_else};
  51. boost::asio::post(ios.get_executor(), [&]{stackless();});
  52. boost::asio::post(ios.get_executor(), [&]{did_something_else = true;});
  53. ios.run();
  54. BOOST_CHECK(did_something_else);
  55. }
  56. BOOST_AUTO_TEST_SUITE_END();