2 * Copyright (c) 2013 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
25 #include <dpl/test/test_runner.h>
26 #include <dpl/test/test_runner_child.h>
27 #include <dpl/test/test_runner_multiprocess.h>
35 const int MULTI_TEST_ERROR = -1;
36 const int MULTI_TEST_PASS = 0;
37 const int MULTI_TEST_FAILED = 1;
38 const int MULTI_TEST_IGNORED = 2;
39 const int MULTI_TEST_INTERNAL = 3;
46 SimplePipeWrapper::SimplePipeWrapper()
52 SimplePipeWrapper::~SimplePipeWrapper()
57 PipeWrapper::Status SimplePipeWrapper::send(std::string &message)
59 if (m_pipefd[1] == PIPE_CLOSED) {
63 if (message.size() > PIPE_BUF-1) {
67 char buffer[PIPE_BUF] = { 0 };
70 for(unsigned int i = 0; i < message.size(); ++i) {
71 buffer[i] = message[i];
74 return writeHelp(buffer, PIPE_BUF);
77 PipeWrapper::Status SimplePipeWrapper::receive(std::string &data, bool &empty, time_t deadline)
79 if (m_pipefd[0] == PIPE_CLOSED) {
85 data.resize(PIPE_BUF);
87 char buffer[PIPE_BUF] = { 0 };
90 while (ready != PIPE_BUF) {
91 time_t wait = deadline - time(0);
92 wait = wait < 1 ? 1 : wait;
93 pollfd fds = { m_pipefd[0], POLLIN, 0 };
95 int pollReturn = poll(&fds, 1, wait * 1000);
97 if (pollReturn == 0) {
98 return TIMEOUT; // Timeout
101 if (pollReturn < -1) {
104 int ret = read(m_pipefd[0], &buffer[ready], PIPE_BUF - ready);
105 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
122 for(unsigned int i = 0; i < PIPE_BUF; ++i){
133 void RunMultiProc(TestRunner::TestCase procMulti)
135 SimplePipeWrapper pipe;
136 int code = MULTI_TEST_PASS;
137 std::string msg = "";
142 pid_t top_pid = getpid();
144 if (!pipe.isReady()) {
145 throw TestRunner::TestFailed("Pipe creation failed");
151 } catch (const TestRunner::TestFailed &e) {
152 code = MULTI_TEST_FAILED;
153 msg = e.GetMessage();
154 } catch (const TestRunner::Ignored &e) {
155 code = MULTI_TEST_IGNORED;
156 msg = e.GetMessage();
157 } catch (const DPL::Exception &e) {
158 code = MULTI_TEST_INTERNAL;
159 msg = "DPL exception:" + e.GetMessage();
160 } catch (const std::exception &) {
161 code = MULTI_TEST_INTERNAL;
162 msg = "std exception";
164 // Unknown exception failure
165 code = MULTI_TEST_INTERNAL;
166 msg = "unknown exception";
170 pid_t child_pid = wait(&waitStatus);
171 if (child_pid == -1) {
172 if (errno == ECHILD) {
173 if (top_pid == getpid()) {
174 std::string recMsg="";
176 pipe.setUsage(PipeWrapper::READONLY);
180 pipeReturn = pipe.receive(recMsg, empty, time(0) + 10);
185 if (pipeReturn == PipeWrapper::ERROR) {
187 throw TestRunner::TestFailed("Reading pipe error");
188 } else if (pipeReturn == PipeWrapper::TIMEOUT) {
190 throw TestRunner::TestFailed("Timeout error");
192 msg = msg + "\n" + recMsg;
197 case MULTI_TEST_PASS:
199 case MULTI_TEST_FAILED:
200 throw TestRunner::TestFailed(msg);
201 case MULTI_TEST_IGNORED:
202 throw TestRunner::Ignored(msg);
203 case MULTI_TEST_INTERNAL:
204 throw TestRunner::TestFailed(msg);
206 throw TestRunner::TestFailed(msg);
209 pipe.setUsage(PipeWrapper::WRITEONLY);
211 pipeReturn = pipe.send(msg);
213 if (pipeReturn == PipeWrapper::ERROR) {
215 code = MULTI_TEST_ERROR;
221 } else if (WIFEXITED(waitStatus)) {
222 if ((signed char)WEXITSTATUS(waitStatus) == MULTI_TEST_FAILED) {
224 case MULTI_TEST_PASS:
225 code = MULTI_TEST_FAILED;
227 case MULTI_TEST_FAILED:
229 case MULTI_TEST_IGNORED:
230 code = MULTI_TEST_FAILED;
232 case MULTI_TEST_INTERNAL:
237 } else if ((signed char)WEXITSTATUS(waitStatus) == MULTI_TEST_IGNORED) {
239 case MULTI_TEST_PASS:
240 code = MULTI_TEST_IGNORED;
242 case MULTI_TEST_FAILED:
244 case MULTI_TEST_IGNORED:
246 case MULTI_TEST_INTERNAL:
251 } else if ((signed char)WEXITSTATUS(waitStatus) == MULTI_TEST_INTERNAL) {
253 case MULTI_TEST_PASS:
254 code = MULTI_TEST_INTERNAL;
256 case MULTI_TEST_FAILED:
257 code = MULTI_TEST_INTERNAL;
259 case MULTI_TEST_IGNORED:
260 code = MULTI_TEST_INTERNAL;
262 case MULTI_TEST_INTERNAL:
267 } else if ((signed char)WEXITSTATUS(waitStatus) != MULTI_TEST_PASS) {
268 code = MULTI_TEST_ERROR;
269 msg = "PROCESS BAD CODE RETURN";