1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 or 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mir_test_framework/process.h"
#include <condition_variable>
#include <mutex>
#include <gtest/gtest.h>
namespace mtf = mir_test_framework;
namespace mir
{
struct MainFunctionFactory
{
static void an_empty_main_function()
{
}
static void an_infinitely_waiting_main_function()
{
std::mutex m;
std::unique_lock ul(m);
std::condition_variable cv;
cv.wait(ul);
}
static void a_value_altering_main_function(
int& value,
int expected_value_after_increment)
{
value++;
EXPECT_EQ(
expected_value_after_increment,
value);
}
};
struct ExitFunctionFactory
{
static int a_successful_exit_function()
{
return EXIT_SUCCESS;
}
static int a_failing_exit_function()
{
return EXIT_FAILURE;
}
static int a_gtest_exit_function()
{
return ::testing::Test::HasFailure() ? EXIT_FAILURE : EXIT_SUCCESS;
}
};
TEST(ProcessResult,
a_default_result_never_succeeds)
{
mtf::Result r;
EXPECT_FALSE(r.succeeded());
}
TEST(ProcessResult,
a_signalled_result_does_not_succeed)
{
mtf::Result r;
r.reason = mtf::TerminationReason::child_terminated_by_signal;
EXPECT_FALSE(r.succeeded());
}
TEST(ProcessResult,
a_normally_terminated_result_succeeds_only_with_exit_success)
{
mtf::Result r;
r.reason = mtf::TerminationReason::child_terminated_normally;
r.exit_code = EXIT_FAILURE;
EXPECT_FALSE(r.succeeded());
r.exit_code = EXIT_SUCCESS;
EXPECT_TRUE(r.succeeded());
}
TEST(Process,
a_main_fn_is_executed)
{
int value = 0;
auto p = mtf::fork_and_run_in_a_different_process(
std::bind(
MainFunctionFactory::a_value_altering_main_function,
value,
1),
ExitFunctionFactory::a_gtest_exit_function);
EXPECT_TRUE(p->wait_for_termination().succeeded());
}
TEST(Process,
a_successful_exit_function_succeeds)
{
auto p = mtf::fork_and_run_in_a_different_process(
MainFunctionFactory::an_empty_main_function,
ExitFunctionFactory::a_successful_exit_function);
EXPECT_TRUE(p->wait_for_termination().succeeded());
}
TEST(Process,
a_failing_exit_function_does_not_succeed)
{
auto p = mtf::fork_and_run_in_a_different_process(
MainFunctionFactory::an_empty_main_function,
ExitFunctionFactory::a_failing_exit_function);
EXPECT_FALSE(p->wait_for_termination().succeeded());
}
TEST(Process,
a_terminated_child_is_recognized_as_being_signalled)
{
auto p = mtf::fork_and_run_in_a_different_process(
MainFunctionFactory::an_infinitely_waiting_main_function,
ExitFunctionFactory::a_successful_exit_function);
p->terminate();
EXPECT_TRUE(p->wait_for_termination().signalled());
}
TEST(Process,
a_killed_child_is_recognized_as_being_signalled)
{
auto p = mtf::fork_and_run_in_a_different_process(
MainFunctionFactory::an_infinitely_waiting_main_function,
ExitFunctionFactory::a_successful_exit_function);
p->kill();
EXPECT_TRUE(p->wait_for_termination().signalled());
}
}
|