Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / process / test / wait.cpp
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 #include <boost/process/error.hpp>
14 #include <boost/process/child.hpp>
15 #include <boost/process/args.hpp>
16 #include <boost/process/async.hpp>
17 #include <thread>
18 #include <atomic>
19 #include <system_error>
20 #include <boost/asio.hpp>
21 #if defined(BOOST_POSIX_API)
22 #   include <signal.h>
23 #endif
24
25 namespace bp = boost::process;
26
27 BOOST_AUTO_TEST_CASE(sync_wait, *boost::unit_test::timeout(2))
28 {
29     using boost::unit_test::framework::master_test_suite;
30
31     std::error_code ec;
32     bp::child c(
33         master_test_suite().argv[1],
34         bp::args+={"test", "--wait", "1"},
35         ec
36     );
37     BOOST_REQUIRE(!ec);
38
39     c.wait();
40
41 }
42
43 BOOST_AUTO_TEST_CASE(async_wait, *boost::unit_test::timeout(2))
44 {
45     using boost::unit_test::framework::master_test_suite;
46     using namespace boost::asio;
47
48     boost::asio::io_service io_service;
49
50     std::error_code ec;
51     bool called = false;
52
53     bp::child c(
54         master_test_suite().argv[1],
55         bp::args+={"test", "--wait", "1"},
56         ec,
57         io_service,
58         bp::on_exit([&](int, const std::error_code&){called = true;})
59
60     );
61     BOOST_REQUIRE(!ec);
62
63     io_service.run();
64     BOOST_CHECK(called);
65
66 }
67
68 BOOST_AUTO_TEST_CASE(double_wait, *boost::unit_test::timeout(5))
69 {
70     using boost::unit_test::framework::master_test_suite;
71     using namespace boost::asio;
72
73     boost::asio::io_service io_service;
74
75     std::error_code ec;
76     std::atomic<bool> called{false};
77
78     bp::child c(
79         master_test_suite().argv[1],
80         bp::args+={"test", "--wait", "1"},
81         ec,
82         io_service,
83         bp::on_exit([&](int, const std::error_code&){called.store(true);})
84
85     );
86     BOOST_REQUIRE(!ec);
87
88     std::thread th([&]{io_service.run();});
89
90     c.wait();
91
92     th.join();
93     BOOST_CHECK(called.load());
94
95 }