async.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/child.hpp>
  16. #include <boost/thread.hpp>
  17. #include <future>
  18. #include <boost/system/error_code.hpp>
  19. #include <boost/algorithm/string/predicate.hpp>
  20. #include <boost/asio/deadline_timer.hpp>
  21. using namespace std;
  22. namespace bp = boost::process;
  23. #if __APPLE__
  24. auto abort_sig = signal(SIGALRM, +[](int){std::terminate();});
  25. #endif
  26. BOOST_AUTO_TEST_SUITE( async );
  27. BOOST_AUTO_TEST_CASE(async_wait, *boost::unit_test::timeout(5))
  28. {
  29. using boost::unit_test::framework::master_test_suite;
  30. using namespace boost::asio;
  31. boost::asio::io_context io_context;
  32. std::error_code ec;
  33. bool exit_called_for_c1 = false;
  34. int exit_code_c1 = 0;
  35. boost::asio::deadline_timer timeout{io_context, boost::posix_time::seconds(2)};
  36. timeout.async_wait([&](boost::system::error_code ec){if (!ec) io_context.stop();});
  37. bp::child c1(master_test_suite().argv[1],
  38. "test", "--exit-code", "123",
  39. ec, io_context,
  40. bp::on_exit([&](int exit, const std::error_code& ec_in)
  41. {
  42. BOOST_CHECK(!exit_called_for_c1);
  43. exit_code_c1 = exit; exit_called_for_c1=true;
  44. BOOST_CHECK(!ec_in);
  45. timeout.cancel();
  46. }));
  47. BOOST_REQUIRE(!ec);
  48. bool exit_called_for_c2 = false;
  49. int exit_code_c2 = 0;
  50. bp::child c2(master_test_suite().argv[1],
  51. "test", "--exit-code", "21",
  52. ec, io_context,
  53. bp::on_exit([&](int exit, const std::error_code& ec_in)
  54. {
  55. BOOST_CHECK(!exit_called_for_c2);
  56. exit_code_c2 = exit; exit_called_for_c2=true;
  57. BOOST_CHECK(!ec_in);
  58. })
  59. );
  60. BOOST_REQUIRE(!ec);
  61. io_context.run();
  62. BOOST_CHECK(exit_called_for_c1);
  63. BOOST_CHECK_EQUAL(exit_code_c1, 123);
  64. BOOST_CHECK_EQUAL(c1.exit_code(), 123);
  65. BOOST_CHECK(exit_called_for_c2);
  66. BOOST_CHECK_EQUAL(exit_code_c2, 21);
  67. BOOST_CHECK_EQUAL(c2.exit_code(), 21);
  68. }
  69. BOOST_AUTO_TEST_CASE(async_wait_sync_wait, *boost::unit_test::timeout(5))
  70. {
  71. using boost::unit_test::framework::master_test_suite;
  72. using namespace boost::asio;
  73. boost::asio::io_context io_context;
  74. bool exit_called = false;
  75. int exit_code = 0;
  76. std::error_code ec;
  77. boost::asio::deadline_timer timeout{io_context, boost::posix_time::seconds(3)};
  78. timeout.async_wait([&](boost::system::error_code ec){if (!ec) io_context.stop();});
  79. bp::child c1(
  80. master_test_suite().argv[1],
  81. "test", "--exit-code", "1",
  82. ec
  83. );
  84. BOOST_REQUIRE(!ec);
  85. bp::child c2(
  86. master_test_suite().argv[1],
  87. "test", "--exit-code", "2", "--wait", "1",
  88. ec,
  89. io_context,
  90. bp::on_exit([&](int exit, const std::error_code& ec_in)
  91. {
  92. exit_code = exit; exit_called=true;
  93. BOOST_CHECK(!ec_in);
  94. timeout.cancel();
  95. })
  96. );
  97. BOOST_REQUIRE(!ec);
  98. io_context.run();
  99. // Regression test for #143: make sure the async SIGCHLD handler on POSIX does not reap the
  100. // child c1 is watching (this will error if so)
  101. c1.wait(ec);
  102. BOOST_REQUIRE(!ec);
  103. BOOST_CHECK(exit_called);
  104. BOOST_CHECK_EQUAL(exit_code, 2);
  105. BOOST_CHECK_EQUAL(c2.exit_code(), 2);
  106. }
  107. BOOST_AUTO_TEST_CASE(async_wait_different_contexts, *boost::unit_test::timeout(10))
  108. {
  109. using boost::unit_test::framework::master_test_suite;
  110. using namespace boost::asio;
  111. boost::asio::io_context io_context1;
  112. boost::asio::io_context io_context2;
  113. boost::asio::deadline_timer timeout1{io_context1, boost::posix_time::seconds(2)};
  114. timeout1.async_wait([&](boost::system::error_code ec){if (!ec) io_context1.stop();});
  115. boost::asio::deadline_timer timeout2{io_context2, boost::posix_time::seconds(7)};
  116. timeout2.async_wait([&](boost::system::error_code ec){if (!ec) io_context2.stop();});
  117. std::error_code ec;
  118. bool exit_called_for_c1 = false;
  119. int exit_code_c1 = 0;
  120. bp::child c1(
  121. master_test_suite().argv[1],
  122. "test", "--exit-code", "1",
  123. ec,
  124. io_context1,
  125. bp::on_exit([&](int exit, const std::error_code& ec_in)
  126. {
  127. BOOST_CHECK(!exit_called_for_c1);
  128. exit_code_c1 = exit; exit_called_for_c1=true;
  129. BOOST_CHECK(!ec_in);
  130. timeout1.cancel();
  131. })
  132. );
  133. BOOST_REQUIRE(!ec);
  134. bool exit_called_for_c2 = false;
  135. int exit_code_c2 = 0;
  136. bp::child c2(
  137. master_test_suite().argv[1],
  138. "test", "--exit-code", "2", "--wait", "4",
  139. ec,
  140. io_context2,
  141. bp::on_exit([&](int exit, const std::error_code& ec_in)
  142. {
  143. BOOST_CHECK(!exit_called_for_c2);
  144. exit_code_c2 = exit; exit_called_for_c2=true;
  145. BOOST_CHECK(!ec_in);
  146. timeout2.cancel();
  147. })
  148. );
  149. BOOST_REQUIRE(!ec);
  150. // Regression test for #143: make sure each io_context handles its own children
  151. std::thread thr1{[&]{io_context1.run();}};
  152. std::thread thr2{[&]{io_context2.run();}};
  153. thr1.join();
  154. thr2.join();
  155. c1.wait(ec);
  156. BOOST_REQUIRE(!ec);
  157. BOOST_CHECK(exit_called_for_c1);
  158. BOOST_CHECK_EQUAL(exit_code_c1, 1);
  159. BOOST_CHECK_EQUAL(c1.exit_code(), 1);
  160. BOOST_CHECK(exit_called_for_c2);
  161. BOOST_CHECK_EQUAL(exit_code_c2, 2);
  162. BOOST_CHECK_EQUAL(c2.exit_code(), 2);
  163. }
  164. BOOST_AUTO_TEST_CASE(async_wait_abort, *boost::unit_test::timeout(5))
  165. {
  166. using boost::unit_test::framework::master_test_suite;
  167. using namespace boost::asio;
  168. boost::asio::io_context io_context;
  169. std::error_code ec;
  170. boost::asio::deadline_timer timeout{io_context, boost::posix_time::seconds(5)};
  171. timeout.async_wait([&](boost::system::error_code ec){if (!ec) io_context.stop();});
  172. bool exit_called = false;
  173. int exit_code = 0;
  174. bp::child c(
  175. master_test_suite().argv[1],
  176. "test", "--abort",
  177. ec,
  178. io_context,
  179. bp::on_exit([&](int exit, const std::error_code& ec_in)
  180. {
  181. BOOST_CHECK(!exit_called);
  182. exit_code = exit;
  183. exit_called=true;
  184. BOOST_TEST_MESSAGE(ec_in.message());
  185. BOOST_CHECK(!ec_in);
  186. timeout.cancel();
  187. })
  188. );
  189. BOOST_REQUIRE(!ec);
  190. io_context.run();
  191. BOOST_CHECK(exit_called);
  192. BOOST_CHECK_NE(exit_code, 0);
  193. BOOST_CHECK_EQUAL(c.exit_code(), exit_code);
  194. }
  195. BOOST_AUTO_TEST_CASE(async_future, *boost::unit_test::timeout(3))
  196. {
  197. using boost::unit_test::framework::master_test_suite;
  198. using namespace boost::asio;
  199. boost::asio::io_context io_context;
  200. boost::asio::deadline_timer timeout{io_context, boost::posix_time::seconds(2)};
  201. timeout.async_wait([&](boost::system::error_code ec){if (!ec) io_context.stop();});
  202. std::error_code ec;
  203. std::future<int> fut;
  204. bp::child c(
  205. master_test_suite().argv[1],
  206. "test", "--exit-code", "42",
  207. ec,
  208. io_context,
  209. bp::on_exit=fut
  210. );
  211. BOOST_REQUIRE(!ec);
  212. io_context.run();
  213. BOOST_REQUIRE(fut.valid());
  214. BOOST_CHECK_EQUAL(fut.get(), 42);
  215. }
  216. BOOST_AUTO_TEST_CASE(async_out_stream, *boost::unit_test::timeout(5))
  217. {
  218. using boost::unit_test::framework::master_test_suite;
  219. boost::asio::io_context io_context;
  220. std::error_code ec;
  221. boost::asio::streambuf buf;
  222. boost::asio::deadline_timer timeout{io_context, boost::posix_time::seconds(2)};
  223. timeout.async_wait([&](boost::system::error_code ec){if (!ec) io_context.stop();});
  224. bp::child c(master_test_suite().argv[1],
  225. "test", "--echo-stdout", "abc",
  226. bp::std_out > buf,
  227. io_context,
  228. ec);
  229. BOOST_REQUIRE(!ec);
  230. io_context.run();
  231. std::istream istr(&buf);
  232. std::string line;
  233. std::getline(istr, line);
  234. BOOST_REQUIRE_GE(line.size(), 3);
  235. BOOST_CHECK(boost::algorithm::starts_with(line, "abc"));
  236. c.wait();
  237. }
  238. BOOST_AUTO_TEST_CASE(async_in_stream, *boost::unit_test::timeout(5))
  239. {
  240. using boost::unit_test::framework::master_test_suite;
  241. boost::asio::io_context io_context;
  242. std::error_code ec;
  243. boost::asio::streambuf buf;
  244. boost::asio::streambuf in_buf;
  245. std::ostream ostr(&in_buf);
  246. ostr << "-string" << endl ;
  247. boost::asio::deadline_timer timeout{io_context, boost::posix_time::seconds(2)};
  248. timeout.async_wait([&](boost::system::error_code ec){if (!ec) io_context.stop();});
  249. bp::child c(
  250. master_test_suite().argv[1],
  251. "test", "--prefix-once", "test",
  252. bp::std_in < in_buf,
  253. bp::std_out > buf,
  254. io_context,
  255. ec
  256. );
  257. BOOST_REQUIRE(!ec);
  258. io_context.run();
  259. std::istream istr(&buf);
  260. std::string line;
  261. std::getline(istr, line);
  262. std::string val = "test-string";
  263. BOOST_REQUIRE_GE(line.size(), val.size());
  264. if (line >= val)
  265. BOOST_CHECK(boost::algorithm::starts_with(line, val));
  266. c.wait();
  267. }
  268. BOOST_AUTO_TEST_CASE(async_error, *boost::unit_test::timeout(3))
  269. {
  270. using boost::unit_test::framework::master_test_suite;
  271. using namespace boost::asio;
  272. boost::asio::io_context io_context;
  273. boost::asio::deadline_timer timeout{io_context, boost::posix_time::seconds(2)};
  274. timeout.async_wait([&](boost::system::error_code ec){if (!ec) io_context.stop();});
  275. bool exit_called = false;
  276. std::error_code ec;
  277. bp::child c(
  278. "doesn't exist",
  279. ec,
  280. io_context,
  281. bp::on_exit([&](int exit, const std::error_code& ec_in)
  282. {
  283. exit_called=true;
  284. })
  285. );
  286. BOOST_REQUIRE(ec);
  287. io_context.run();
  288. BOOST_CHECK(!exit_called);
  289. }
  290. /*
  291. BOOST_AUTO_TEST_CASE(mixed_async, *boost::unit_test::timeout(5))
  292. {
  293. using boost::unit_test::framework::master_test_suite;
  294. using namespace boost::asio;
  295. boost::asio::io_context io_context;
  296. boost::asio::deadline_timer timeout{io_context, boost::posix_time::seconds(2)};
  297. timeout.async_wait([&](boost::system::error_code ec){if (!ec) io_context.stop();});
  298. bool exit_called = false;
  299. std::error_code ec;
  300. bp::child c(master_test_suite().argv[1],
  301. "--wait", "1", "--exit-code", "42",
  302. ec,
  303. io_context,
  304. bp::on_exit([&](int exit, const std::error_code& ec_in)
  305. {
  306. timeout.cancel();
  307. exit_called=true;
  308. BOOST_CHECK_EQUAL(exit, 42);
  309. })
  310. );
  311. BOOST_REQUIRE(!ec);
  312. std::thread thr([&]{c.wait();});
  313. io_context.run();
  314. BOOST_CHECK(exit_called);
  315. BOOST_CHECK_EQUAL(c.exit_code(), 42);
  316. thr.join();
  317. }*/
  318. BOOST_AUTO_TEST_SUITE_END();