process-cpp  3.0.0
A simple convenience library for handling processes in C++11.
fork_and_run.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012-2013 Canonical Ltd.
3  *
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.
7  *
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.
12  *
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/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 #ifndef CORE_TESTING_FORK_AND_RUN_H_
19 #define CORE_TESTING_FORK_AND_RUN_H_
20 
21 #include <core/posix/exit.h>
22 #include <core/posix/fork.h>
23 #include <core/posix/visibility.h>
24 
25 #include <functional>
26 
27 namespace core
28 {
29 namespace testing
30 {
34 enum class ForkAndRunResult
35 {
36  empty = 0,
37  client_failed = 1 << 0,
38  service_failed = 1 << 1
39 };
40 
43 
59  const std::function<core::posix::exit::Status()>& service,
60  const std::function<core::posix::exit::Status()>& client);
61 }
62 }
63 
75 #define TESTP(test_suite, test_name, CODE) \
76  TEST(test_suite, test_name) { \
77  auto test = [&]() { \
78  CODE \
79  return HasFailure() ? core::posix::exit::Status::failure \
80  : core::posix::exit::Status::success; \
81  }; \
82  auto child = core::posix::fork( \
83  test, \
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); \
88  } \
89 
90 
100 #define TESTP_F(test_fixture, test_name, CODE) \
101  TEST_F(test_fixture, test_name) { \
102  auto test = [&]() { \
103  CODE \
104  return HasFailure() ? core::posix::exit::Status::failure \
105  : core::posix::exit::Status::success; \
106  }; \
107  auto child = core::posix::fork( \
108  test, \
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); \
113  } \
114 
115 #endif // CORE_TESTING_FORK_AND_RUN_H_
ForkAndRunResult
The ForkAndRunResult enum models the different failure modes of fork_and_run.
Definition: fork_and_run.h:34
#define CORE_POSIX_DLL_PUBLIC
Definition: visibility.h:26
Status
The Status enum wrap&#39;s the posix exit status.
Definition: exit.h:33
CORE_POSIX_DLL_PUBLIC ForkAndRunResult fork_and_run(const std::function< core::posix::exit::Status()> &service, const std::function< core::posix::exit::Status()> &client)
Forks two processes for both the service and the client.
Special value indicating no bit being set.
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator&(ForkAndRunResult lhs, ForkAndRunResult rhs)
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator|(ForkAndRunResult lhs, ForkAndRunResult rhs)