]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
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 | |
92f5a8d4 | 11 | //#define BOOST_TEST_IGNORE_SIGCHLD |
b32b8144 FG |
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 <thread> | |
17 | #include <atomic> | |
18 | #include <system_error> | |
19 | #include <boost/asio.hpp> | |
20 | #if defined(BOOST_POSIX_API) | |
21 | # include <signal.h> | |
22 | #endif | |
23 | ||
24 | namespace bp = boost::process; | |
92f5a8d4 | 25 | BOOST_AUTO_TEST_SUITE( wait_test); |
b32b8144 FG |
26 | |
27 | BOOST_AUTO_TEST_CASE(wait_for) | |
28 | { | |
29 | using boost::unit_test::framework::master_test_suite; | |
30 | ||
31 | std::error_code ec; | |
92f5a8d4 TL |
32 | |
33 | auto launch_time = std::chrono::system_clock::now(); | |
b32b8144 FG |
34 | bp::child c( |
35 | master_test_suite().argv[1], | |
36 | bp::args+={"test", "--wait", "1"}, | |
37 | ec | |
38 | ); | |
39 | BOOST_REQUIRE(!ec); | |
40 | ||
92f5a8d4 | 41 | |
b32b8144 FG |
42 | BOOST_CHECK(!c.wait_for(std::chrono::milliseconds(200))); |
43 | BOOST_CHECK( c.wait_for(std::chrono::milliseconds(1000))); | |
92f5a8d4 TL |
44 | |
45 | auto timeout_t = std::chrono::system_clock::now(); | |
46 | ||
47 | BOOST_CHECK_LE(std::chrono::duration_cast<std::chrono::seconds>(timeout_t - launch_time).count(), 5); //should be less | |
b32b8144 FG |
48 | } |
49 | ||
50 | BOOST_AUTO_TEST_CASE(wait_for_ec) | |
51 | { | |
52 | using boost::unit_test::framework::master_test_suite; | |
53 | ||
54 | std::error_code ec; | |
55 | bp::child c( | |
56 | master_test_suite().argv[1], | |
57 | bp::args+={"test", "--wait", "1"}, | |
58 | ec | |
59 | ); | |
60 | BOOST_REQUIRE(!ec); | |
61 | ||
62 | BOOST_CHECK(!c.wait_for(std::chrono::milliseconds(400),ec)); | |
63 | BOOST_CHECK( c.wait_for(std::chrono::milliseconds(1000),ec)); | |
64 | ||
65 | BOOST_CHECK_MESSAGE(!ec, ec.message()); | |
66 | } | |
67 | ||
68 | ||
69 | BOOST_AUTO_TEST_CASE(wait_until) | |
70 | { | |
71 | using boost::unit_test::framework::master_test_suite; | |
72 | std::error_code ec; | |
73 | ||
74 | bp::child c( | |
75 | master_test_suite().argv[1], | |
76 | bp::args+={"test", "--wait", "1"}, | |
77 | ec | |
78 | ); | |
79 | BOOST_REQUIRE(!ec); | |
80 | ||
81 | auto now = std::chrono::system_clock::now(); | |
82 | ||
83 | auto t1 = now + std::chrono::milliseconds(400); | |
84 | auto t2 = now + std::chrono::milliseconds(1200); | |
85 | ||
86 | BOOST_CHECK(!c.wait_until(t1)); | |
87 | BOOST_CHECK( c.wait_until(t2)); | |
88 | ||
89 | } | |
90 | ||
91 | BOOST_AUTO_TEST_CASE(wait_until_ec) | |
92 | { | |
93 | using boost::unit_test::framework::master_test_suite; | |
94 | std::error_code ec; | |
95 | ||
96 | bp::child c( | |
97 | master_test_suite().argv[1], | |
98 | bp::args+={"test", "--wait", "1"}, | |
99 | ec | |
100 | ); | |
101 | BOOST_REQUIRE(!ec); | |
102 | ||
103 | auto now = std::chrono::system_clock::now(); | |
104 | ||
105 | auto t1 = now + std::chrono::milliseconds(400); | |
106 | auto t2 = now + std::chrono::milliseconds(1200); | |
107 | ||
108 | BOOST_CHECK(!c.wait_until(t1, ec)); | |
109 | BOOST_CHECK( c.wait_until(t2, ec)); | |
110 | ||
111 | BOOST_CHECK_MESSAGE(!ec, ec.message()); | |
92f5a8d4 TL |
112 | } |
113 | ||
114 | BOOST_AUTO_TEST_CASE(wait_for_exit_before_timeout) | |
115 | { | |
116 | using boost::unit_test::framework::master_test_suite; | |
117 | ||
118 | std::error_code ec; | |
119 | ||
120 | auto launch_time = std::chrono::system_clock::now(); | |
121 | bp::child c( | |
122 | master_test_suite().argv[1], | |
123 | bp::args+={"test", "--wait", "1"}, | |
124 | ec | |
125 | ); | |
126 | BOOST_REQUIRE(!ec); | |
127 | ||
128 | BOOST_CHECK(c.wait_for(std::chrono::seconds(20))); | |
129 | ||
130 | auto timeout_t = std::chrono::system_clock::now(); | |
131 | ||
132 | // check that we didn't wait the entire timeout period | |
133 | BOOST_CHECK_LT(std::chrono::duration_cast<std::chrono::seconds>(timeout_t - launch_time).count(), 20); | |
134 | } | |
135 | ||
136 | BOOST_AUTO_TEST_SUITE_END(); |