on_exit.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.hpp>
  13. #include <boost/asio.hpp>
  14. #include <chrono>
  15. #include <thread>
  16. BOOST_AUTO_TEST_CASE(single_ios, *boost::unit_test::timeout(6))
  17. {
  18. using boost::unit_test::framework::master_test_suite;
  19. if (master_test_suite().argc > 2 && strcmp(master_test_suite().argv[1], "sleep") == 0)
  20. {
  21. auto s = atoi(master_test_suite().argv[2]);
  22. std::this_thread::sleep_for(std::chrono::seconds(s));
  23. return;
  24. }
  25. namespace bp = boost::process;
  26. boost::asio::io_context ios;
  27. std::chrono::steady_clock::time_point p1, p2;
  28. // launch a child that will sleep for 2s
  29. auto c1 = bp::child(master_test_suite().argv[0], "sleep", "2", ios,
  30. bp::on_exit([&p1](int, const std::error_code&)
  31. { p1 = std::chrono::steady_clock::now(); }));
  32. // wait a bit, make sure the child launch for my test
  33. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  34. // launch a child that will sleep for 4s
  35. auto c2 = bp::child(master_test_suite().argv[0], "sleep", "4", ios,
  36. bp::on_exit([&p2](int, const std::error_code&)
  37. { p2 = std::chrono::steady_clock::now(); }));
  38. // wait for the notifications
  39. while (!ios.stopped())
  40. ios.run_one();
  41. BOOST_REQUIRE((p2 - p1) > std::chrono::seconds(1));
  42. }