2 * Copyright © 2012-2013 Canonical Ltd.
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
18 #ifndef CORE_TESTING_FORK_AND_RUN_H_
19 #define CORE_TESTING_FORK_AND_RUN_H_
21 #include <core/posix/exit.h>
22 #include <core/posix/fork.h>
23 #include <core/posix/visibility.h>
32 * @brief The ForkAndRunResult enum models the different failure modes of fork_and_run.
34 enum class ForkAndRunResult
36 empty = 0, ///< Special value indicating no bit being set.
37 client_failed = 1 << 0, ///< The client failed.
38 service_failed = 1 << 1 ///< The service failed.
41 CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator|(ForkAndRunResult lhs, ForkAndRunResult rhs);
42 CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator&(ForkAndRunResult lhs, ForkAndRunResult rhs);
45 * @brief Forks two processes for both the service and the client.
47 * The function does the following:
48 * - Forks a process for the service and runs the respective closure.
49 * - Forks a process for the client and runs the respective closure.
50 * - After the client has finished, the service is signalled with sigterm.
52 * @throw std::system_error if an error occured during process interaction.
53 * @throw std::runtime_error for signalling all other error conditions.
54 * @param [in] service The service to be executed in a child process.
55 * @param [in] client The client to be executed in a child process.
56 * @return ForkAndRunResult indicating if either of service or client failed.
58 CORE_POSIX_DLL_PUBLIC ForkAndRunResult fork_and_run(
59 const std::function<core::posix::exit::Status()>& service,
60 const std::function<core::posix::exit::Status()>& client);
65 * Test definition macro which runs a TEST in a forked process.
66 * Note that you can only use EXPECT_*, not
70 * TESTP(test_suite, test_name, {
75 #define TESTP(test_suite, test_name, CODE) \
76 TEST(test_suite, test_name) { \
79 return HasFailure() ? core::posix::exit::Status::failure \
80 : core::posix::exit::Status::success; \
82 auto child = core::posix::fork( \
84 core::posix::StandardStream::empty); \
85 auto result = child.wait_for(core::posix::wait::Flags::untraced); \
86 EXPECT_EQ(core::posix::wait::Result::Status::exited, result.status); \
87 EXPECT_EQ(core::posix::exit::Status::success, result.detail.if_exited.status); \
91 * Test definition macro which runs a TEST_F in a forked process.
92 * Note that you can only use EXPECT_*, not ASSERT_*!
95 * TESTP_F(FixtureName, TestName, {
100 #define TESTP_F(test_fixture, test_name, CODE) \
101 TEST_F(test_fixture, test_name) { \
102 auto test = [&]() { \
104 return HasFailure() ? core::posix::exit::Status::failure \
105 : core::posix::exit::Status::success; \
107 auto child = core::posix::fork( \
109 core::posix::StandardStream::empty); \
110 auto result = child.wait_for(core::posix::wait::Flags::untraced); \
111 EXPECT_EQ(core::posix::wait::Result::Status::exited, result.status); \
112 EXPECT_EQ(core::posix::exit::Status::success, result.detail.if_exited.status); \
115 #endif // CORE_TESTING_FORK_AND_RUN_H_