fork_and_run.h
Go to the documentation of this file.
/*
* Copyright © 2012-2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#ifndef CORE_TESTING_FORK_AND_RUN_H_
#define CORE_TESTING_FORK_AND_RUN_H_
#include <core/posix/exit.h>
#include <core/posix/fork.h>
#include <core/posix/visibility.h>
#include <functional>
namespace core
{
namespace testing
{
enum class ForkAndRunResult
{
empty = 0,
client_failed = 1 << 0,
service_failed = 1 << 1
};
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator|(ForkAndRunResult lhs, ForkAndRunResult rhs);
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator&(ForkAndRunResult lhs, ForkAndRunResult rhs);
CORE_POSIX_DLL_PUBLIC ForkAndRunResult fork_and_run(
const std::function<core::posix::exit::Status()>& service,
const std::function<core::posix::exit::Status()>& client);
}
}
#define TESTP(test_suite, test_name, CODE) \
TEST(test_suite, test_name) { \
auto test = [&]() { \
CODE \
return HasFailure() ? core::posix::exit::Status::failure \
: core::posix::exit::Status::success; \
}; \
auto child = core::posix::fork( \
test, \
core::posix::StandardStream::empty); \
auto result = child.wait_for(core::posix::wait::Flags::untraced); \
EXPECT_EQ(core::posix::wait::Result::Status::exited, result.status); \
EXPECT_EQ(core::posix::exit::Status::success, result.detail.if_exited.status); \
} \
#define TESTP_F(test_fixture, test_name, CODE) \
TEST_F(test_fixture, test_name) { \
auto test = [&]() { \
CODE \
return HasFailure() ? core::posix::exit::Status::failure \
: core::posix::exit::Status::success; \
}; \
auto child = core::posix::fork( \
test, \
core::posix::StandardStream::empty); \
auto result = child.wait_for(core::posix::wait::Flags::untraced); \
EXPECT_EQ(core::posix::wait::Result::Status::exited, result.status); \
EXPECT_EQ(core::posix::exit::Status::success, result.detail.if_exited.status); \
} \
#endif // CORE_TESTING_FORK_AND_RUN_H_
exit.h
fork.h
core::posix::exit::Status
Status
The Status enum wrap's the posix exit status.
Definition exit.h:34
core::testing
Definition cross_process_sync.h:32
core::testing::operator&
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator&(ForkAndRunResult lhs, ForkAndRunResult rhs)
Definition fork_and_run.cpp:33
core::testing::operator|
CORE_POSIX_DLL_PUBLIC ForkAndRunResult operator|(ForkAndRunResult lhs, ForkAndRunResult rhs)
Definition fork_and_run.cpp:25
core::testing::ForkAndRunResult
ForkAndRunResult
The ForkAndRunResult enum models the different failure modes of fork_and_run.
Definition fork_and_run.h:35
core::testing::ForkAndRunResult::client_failed
@ client_failed
The client failed.
Definition fork_and_run.h:37
core::testing::ForkAndRunResult::empty
@ empty
Special value indicating no bit being set.
Definition fork_and_run.h:36
core::testing::ForkAndRunResult::service_failed
@ service_failed
The service failed.
Definition fork_and_run.h:38
core::testing::fork_and_run
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.
Definition fork_and_run.cpp:41
core
Definition child_process.h:32
visibility.h
CORE_POSIX_DLL_PUBLIC
#define CORE_POSIX_DLL_PUBLIC
Definition visibility.h:26