2 * Copyright (c) 2013-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_child.cpp
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief This file is the implementation file of test runner
23 #include <dpl/test/test_runner.h>
24 #include <dpl/test/test_runner_child.h>
25 #include <dpl/test/test_results_collector.h>
26 #include <dpl/binary_queue.h>
27 #include <dpl/exception.h>
28 #include <dpl/scoped_free.h>
29 #include <dpl/colors.h>
41 #include <sys/types.h>
46 const int CHILD_TEST_FAIL = 0;
47 const int CHILD_TEST_PASS = 1;
48 const int CHILD_TEST_IGNORED = 2;
53 if (-1 == (devnull = TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY))))
56 // replace stdout with /dev/null
57 if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDOUT_FILENO)))
60 // replace stderr with /dev/null
61 if (-1 == TEMP_FAILURE_RETRY(dup2(devnull, STDERR_FILENO)))
71 } // namespace anonymous
76 PipeWrapper::PipeWrapper()
78 if (-1 == pipe(m_pipefd)) {
79 m_pipefd[0] = PIPE_CLOSED;
80 m_pipefd[1] = PIPE_CLOSED;
84 PipeWrapper::~PipeWrapper()
90 bool PipeWrapper::isReady()
92 return m_pipefd[0] != PIPE_CLOSED || m_pipefd[1] != PIPE_CLOSED;
95 void PipeWrapper::setUsage(Usage usage)
97 if (usage == READONLY) {
100 if (usage == WRITEONLY) {
105 PipeWrapper::Status PipeWrapper::send(int code, std::string &message)
107 if (m_pipefd[1] == PIPE_CLOSED) {
111 std::ostringstream output;
112 output << toBinaryString(code);
113 output << toBinaryString(static_cast<int>(message.size()));
116 std::string binary = output.str();
117 int size = binary.size();
119 if ((writeHelp(&size,
120 sizeof(int)) == ERROR) ||
121 (writeHelp(binary.c_str(), size) == ERROR))
128 PipeWrapper::Status PipeWrapper::sendPerformance(const ConstPerformanceResultPtr &performance)
131 if (m_pipefd[1] == PIPE_CLOSED) {
135 return writeHelp(&foo, sizeof(int));
137 std::string binary = performance->ToBinaryString();
138 int size = binary.size();
140 if ((writeHelp(&size,
141 sizeof(int)) == ERROR) ||
142 (writeHelp(binary.c_str(), size) == ERROR))
149 PipeWrapper::Status PipeWrapper::receive(int &code,
151 PerformanceResultPtr &performance,
154 if (m_pipefd[0] == PIPE_CLOSED) {
161 if ((ret = readHelp(&size, sizeof(int), deadline)) != SUCCESS) {
165 std::vector<char> buffer;
168 if ((ret = readHelp(&buffer[0], size, deadline)) != SUCCESS) {
173 DPL::BinaryQueue queue;
174 queue.AppendCopy(&buffer[0], size);
176 queue.FlattenConsume(&code, sizeof(int));
177 queue.FlattenConsume(&size, sizeof(int));
181 queue.FlattenConsume(&buffer[0], size);
182 data.assign(buffer.begin(), buffer.end());
184 if (code != CHILD_TEST_PASS)
187 if ((ret = readHelp(&size, sizeof(int), deadline)) != SUCCESS) {
192 performance = nullptr;
198 if ((ret = readHelp(buffer.data(), size, deadline)) != SUCCESS) {
202 queue.AppendCopy(buffer.data(), size);
204 performance.reset(new PerformanceResult(queue));
205 } catch (DPL::BinaryQueue::Exception::Base &e) {
211 void PipeWrapper::closeAll()
217 std::string PipeWrapper::toBinaryString(int data)
219 char buffer[sizeof(int)];
220 memcpy(buffer, &data, sizeof(int));
221 return std::string(buffer, buffer + sizeof(int));
224 void PipeWrapper::closeHelp(int desc)
226 if (m_pipefd[desc] != PIPE_CLOSED) {
227 TEMP_FAILURE_RETRY(close(m_pipefd[desc]));
228 m_pipefd[desc] = PIPE_CLOSED;
232 PipeWrapper::Status PipeWrapper::writeHelp(const void *buffer, int size)
235 const char *p = static_cast<const char *>(buffer);
236 while (ready != size) {
237 int ret = write(m_pipefd[1], &p[ready], size - ready);
239 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
253 PipeWrapper::Status PipeWrapper::readHelp(void *buf, int size, time_t deadline)
256 char *buffer = static_cast<char*>(buf);
257 while (ready != size) {
258 time_t wait = deadline - time(0);
259 wait = wait < 1 ? 1 : wait;
260 pollfd fds = { m_pipefd[0], POLLIN, 0 };
262 int pollReturn = poll(&fds, 1, wait * 1000);
264 if (pollReturn == 0) {
265 return TIMEOUT; // Timeout
268 if (pollReturn < -1) {
272 int ret = read(m_pipefd[0], &buffer[ready], size - ready);
274 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
278 if (ret == -1 || ret == 0) {
288 void RunChildProc(TestRunner::TestCase procChild)
291 if (!pipe.isReady()) {
292 throw TestRunner::TestFailed("Pipe creation failed");
298 throw TestRunner::TestFailed("Child creation failed");
303 pipe.setUsage(PipeWrapper::READONLY);
307 PerformanceResultPtr performance;
309 int pipeReturn = pipe.receive(code, message, performance, time(0) + 10);
311 if (pipeReturn != PipeWrapper::SUCCESS) { // Timeout or reading error
317 waitpid(pid, &status, 0);
319 if (pipeReturn == PipeWrapper::TIMEOUT) {
320 throw TestRunner::TestFailed("Timeout");
323 if (pipeReturn == PipeWrapper::ERROR) {
324 throw TestRunner::TestFailed("Reading pipe error");
327 TestRunnerSingleton::Instance().setCurrentTestCasePerformanceResult(performance);
329 if (code == CHILD_TEST_FAIL) {
330 throw TestRunner::TestFailed(message);
331 } else if (code == CHILD_TEST_IGNORED) {
332 throw TestRunner::Ignored(message);
337 // End Runner after current test
338 TestRunnerSingleton::Instance().Terminate();
340 int code = CHILD_TEST_PASS;
343 bool allowLogs = TestRunnerSingleton::Instance().GetAllowChildLogs();
347 closeOutput(); // if fails nothing we can do
350 pipe.setUsage(PipeWrapper::WRITEONLY);
354 } catch (const DPL::Test::TestRunner::TestFailed &e) {
355 msg = e.GetMessage();
356 code = CHILD_TEST_FAIL;
357 } catch (const DPL::Test::TestRunner::Ignored &e) {
358 msg = e.GetMessage();
359 code = CHILD_TEST_IGNORED;
360 } catch (...) { // catch all exception generated by "user" code
361 msg = "unhandled exeception";
362 code = CHILD_TEST_FAIL;
369 pipe.send(code, msg);
370 if (code == CHILD_TEST_PASS){
371 pipe.sendPerformance(TestRunnerSingleton::Instance() \
372 .getCurrentTestCasePerformanceResult());