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 
14 
15 
16 #include <boost/process/error.hpp>
17 #include <boost/process/exe.hpp>
18 #include <boost/process/child.hpp>
19 #include <boost/process/args.hpp>
20 #include <system_error>
21 #include <thread>
22 
23 namespace bp = boost::process;
24 
25 BOOST_AUTO_TEST_CASE(terminate_set_on_error, *boost::unit_test::timeout(5))
26 {
27     std::atomic<bool> done{false};
28     std::thread thr{
29             [&]
__anonb8929c220102null30             {
31                 for (int i = 0; i < 50 && !done.load(); i++)
32                     std::this_thread::sleep_for(std::chrono::milliseconds(100));
33                 BOOST_REQUIRE(done.load());
34             }};
35 
36     using boost::unit_test::framework::master_test_suite;
37     std::error_code ec;
38     bp::child c(
39         bp::exe(master_test_suite().argv[1]),
40         bp::args+={"test", "--loop"},
41         ec
42     );
43     BOOST_REQUIRE(!ec);
44 
45     BOOST_CHECK(c.valid());
46     BOOST_CHECK(c.running(ec));
47 
48     BOOST_CHECK(!c.wait_for(std::chrono::milliseconds(100), ec));
49     BOOST_CHECK(c.running(ec));
50     BOOST_CHECK(c.valid());
51 
52     c.terminate(ec);
53     std::this_thread::sleep_for(std::chrono::milliseconds(100));
54     BOOST_CHECK(!c.running(ec));
55     BOOST_CHECK(!ec);
56 
57     done.store(true);
58     thr.join();
59 }
60 
61 BOOST_AUTO_TEST_CASE(terminate_throw_on_error, *boost::unit_test::timeout(5))
62 {
63     std::atomic<bool> done{false};
64     std::thread thr{
65             [&]
__anonb8929c220202null66             {
67                 for (int i = 0; i < 50 && !done.load(); i++)
68                     std::this_thread::sleep_for(std::chrono::milliseconds(100));
69                 BOOST_REQUIRE(done.load());
70             }};
71 
72     using boost::unit_test::framework::master_test_suite;
73 
74     std::error_code ec;
75     bp::child c(
76         master_test_suite().argv[1],
77         bp::args+="test",
78         bp::args+="--loop",
79         ec
80     );
81     BOOST_REQUIRE(!ec);
82     BOOST_CHECK(c.valid());
83     BOOST_CHECK(c.running());
84 
85     BOOST_CHECK(!c.wait_for(std::chrono::milliseconds(100), ec));
86     BOOST_CHECK(c.running(ec));
87     BOOST_CHECK(c.valid());
88 
89     c.terminate();
90     std::this_thread::sleep_for(std::chrono::milliseconds(5));
91     BOOST_CHECK(!c.running());
92 
93     done.store(true);
94     thr.join();
95 }
96