2 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file test_runner_multiprocess.cpp
18 * @author Marcin Niesluchowski (m.niesluchow@samsung.com)
20 * @brief This file is the implementation file of multiprocess test runner
24 #include <dpl/exception.h>
25 #include <dpl/test/test_failed.h>
26 #include <dpl/test/test_ignored.h>
27 #include <dpl/test/test_runner.h>
28 #include <dpl/test/test_runner_child.h>
29 #include <dpl/test/test_runner_multiprocess.h>
37 const int MULTI_TEST_ERROR = -1;
38 const int MULTI_TEST_PASS = 0;
39 const int MULTI_TEST_FAILED = 1;
40 const int MULTI_TEST_IGNORED = 2;
47 SimplePipeWrapper::SimplePipeWrapper()
53 SimplePipeWrapper::~SimplePipeWrapper()
58 PipeWrapper::Status SimplePipeWrapper::send(std::string &message)
60 if (m_pipefd[1] == PIPE_CLOSED) {
64 if (message.size() > PIPE_BUF-1) {
68 char buffer[PIPE_BUF] = { 0 };
71 for(unsigned int i = 0; i < message.size(); ++i) {
72 buffer[i] = message[i];
75 return writeHelp(buffer, PIPE_BUF);
78 PipeWrapper::Status SimplePipeWrapper::receive(std::string &data, bool &empty, time_t deadline)
80 if (m_pipefd[0] == PIPE_CLOSED) {
86 data.resize(PIPE_BUF);
88 char buffer[PIPE_BUF] = { 0 };
91 while (ready != PIPE_BUF) {
92 time_t wait = deadline - time(0);
93 wait = wait < 1 ? 1 : wait;
94 pollfd fds = { m_pipefd[0], POLLIN, 0 };
96 int pollReturn = poll(&fds, 1, wait * 1000);
98 if (pollReturn == 0) {
99 return TIMEOUT; // Timeout
102 if (pollReturn < -1) {
105 int ret = read(m_pipefd[0], &buffer[ready], PIPE_BUF - ready);
106 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
123 for(unsigned int i = 0; i < PIPE_BUF; ++i){
134 void RunMultiProc(TestRunner::TestCase procMulti)
136 SimplePipeWrapper pipe;
137 int code = MULTI_TEST_PASS;
138 std::string msg = "";
143 pid_t top_pid = getpid();
145 if (!pipe.isReady()) {
146 throw TestFailed("Pipe creation failed");
152 } catch (const TestFailed &e) {
153 code = MULTI_TEST_FAILED;
154 msg = e.GetMessage();
155 } catch (const TestIgnored &e) {
156 code = MULTI_TEST_IGNORED;
157 msg = e.GetMessage();
158 } catch (const std::exception &) {
159 code = MULTI_TEST_FAILED;
160 msg = "std exception";
162 // Unknown exception failure
163 code = MULTI_TEST_FAILED;
164 msg = "unknown exception";
168 pid_t child_pid = wait(&waitStatus);
169 if (child_pid == -1) {
170 if (errno == ECHILD) {
171 if (top_pid == getpid()) {
172 std::string recMsg="";
174 pipe.setUsage(PipeWrapper::READONLY);
178 pipeReturn = pipe.receive(recMsg, empty, time(0) + 10);
183 if (pipeReturn == PipeWrapper::ERROR) {
185 throw TestFailed("Reading pipe error");
186 } else if (pipeReturn == PipeWrapper::TIMEOUT) {
188 throw TestFailed("Timeout error");
190 msg = msg + "\n" + recMsg;
195 case MULTI_TEST_PASS:
197 case MULTI_TEST_FAILED:
198 throw TestFailed(msg);
199 case MULTI_TEST_IGNORED:
200 throw TestIgnored(msg);
202 throw TestFailed(msg);
205 pipe.setUsage(PipeWrapper::WRITEONLY);
207 pipeReturn = pipe.send(msg);
209 if (pipeReturn == PipeWrapper::ERROR) {
211 code = MULTI_TEST_ERROR;
217 } else if (WIFEXITED(waitStatus)) {
218 if ((signed char)WEXITSTATUS(waitStatus) == MULTI_TEST_FAILED) {
220 case MULTI_TEST_PASS:
221 code = MULTI_TEST_FAILED;
223 case MULTI_TEST_FAILED:
225 case MULTI_TEST_IGNORED:
226 code = MULTI_TEST_FAILED;
231 } else if ((signed char)WEXITSTATUS(waitStatus) == MULTI_TEST_IGNORED) {
233 case MULTI_TEST_PASS:
234 code = MULTI_TEST_IGNORED;
236 case MULTI_TEST_FAILED:
238 case MULTI_TEST_IGNORED:
243 } else if ((signed char)WEXITSTATUS(waitStatus) != MULTI_TEST_PASS) {
244 code = MULTI_TEST_ERROR;
245 msg = "PROCESS BAD CODE RETURN";