1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Execution Server
3 * ---------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 *//*--------------------------------------------------------------------*/
24 #include "xsTestDriver.hpp"
35 # define DBG_PRINT(X) printf X
43 TestDriver::TestDriver (xs::TestProcess* testProcess)
44 : m_state (STATE_NOT_STARTED)
46 , m_process (testProcess)
47 , m_lastProcessDataTime (0)
48 , m_dataMsgTmpBuf (SEND_RECV_TMP_BUFFER_SIZE)
52 TestDriver::~TestDriver (void)
57 void TestDriver::reset (void)
61 m_state = STATE_NOT_STARTED;
64 void TestDriver::startProcess (const char* name, const char* params, const char* workingDir, const char* caseList)
68 m_process->start(name, params, workingDir, caseList);
69 m_state = STATE_PROCESS_STARTED;
71 catch (const TestProcessException& e)
73 printf("Failed to launch test process: %s\n", e.what());
74 m_state = STATE_PROCESS_LAUNCH_FAILED;
75 m_lastLaunchFailure = e.what();
79 void TestDriver::stopProcess (void)
81 m_process->terminate();
84 bool TestDriver::poll (ByteBuffer& messageBuffer)
88 case STATE_NOT_STARTED:
89 return false; // Nothing to report.
91 case STATE_PROCESS_LAUNCH_FAILED:
92 DBG_PRINT((" STATE_PROCESS_LAUNCH_FAILED\n"));
93 if (writeMessage(messageBuffer, ProcessLaunchFailedMessage(m_lastLaunchFailure.c_str())))
95 m_state = STATE_NOT_STARTED;
96 m_lastLaunchFailure = "";
102 case STATE_PROCESS_STARTED:
103 DBG_PRINT((" STATE_PROCESS_STARTED\n"));
104 if (writeMessage(messageBuffer, ProcessStartedMessage()))
106 m_state = STATE_PROCESS_RUNNING;
112 case STATE_PROCESS_RUNNING:
114 DBG_PRINT((" STATE_PROCESS_RUNNING\n"));
115 bool gotProcessData = false;
117 // Poll log file and info buffer.
118 gotProcessData = pollLogFile(messageBuffer) || gotProcessData;
119 gotProcessData = pollInfo(messageBuffer) || gotProcessData;
122 return true; // Got IO.
124 if (!m_process->isRunning())
127 m_state = STATE_READING_DATA;
128 m_lastExitCode = m_process->getExitCode();
129 m_lastProcessDataTime = deGetMicroseconds();
131 return true; // Got state change.
134 return false; // Nothing to report.
137 case STATE_READING_DATA:
139 DBG_PRINT((" STATE_READING_DATA\n"));
140 bool gotProcessData = false;
142 // Poll log file and info buffer.
143 gotProcessData = pollLogFile(messageBuffer) || gotProcessData;
144 gotProcessData = pollInfo(messageBuffer) || gotProcessData;
149 m_lastProcessDataTime = deGetMicroseconds();
152 else if (deGetMicroseconds() - m_lastProcessDataTime > READ_DATA_TIMEOUT*1000)
154 // Read timeout occurred.
155 m_state = STATE_PROCESS_FINISHED;
156 return true; // State change.
159 return false; // Still waiting for data.
162 case STATE_PROCESS_FINISHED:
163 DBG_PRINT((" STATE_PROCESS_FINISHED\n"));
164 if (writeMessage(messageBuffer, ProcessFinishedMessage(m_lastExitCode)))
166 // Signal TestProcess to clean up any remaining resources.
167 m_process->cleanup();
169 m_state = STATE_NOT_STARTED;
182 bool TestDriver::pollLogFile (ByteBuffer& messageBuffer)
184 return pollBuffer(messageBuffer, MESSAGETYPE_PROCESS_LOG_DATA);
187 bool TestDriver::pollInfo (ByteBuffer& messageBuffer)
189 return pollBuffer(messageBuffer, MESSAGETYPE_INFO);
192 bool TestDriver::pollBuffer (ByteBuffer& messageBuffer, MessageType msgType)
194 const int minBytesAvailable = MESSAGE_HEADER_SIZE + MIN_MSG_PAYLOAD_SIZE;
196 if (messageBuffer.getNumFree() < minBytesAvailable)
197 return false; // Not enough space in message buffer.
199 const int maxMsgSize = de::min((int)m_dataMsgTmpBuf.size(), messageBuffer.getNumFree());
201 int msgSize = MESSAGE_HEADER_SIZE+1; // One byte is reserved for terminating 0.
203 // Fill in data \note Last byte is reserved for 0.
204 numRead = msgType == MESSAGETYPE_PROCESS_LOG_DATA
205 ? m_process->readTestLog(&m_dataMsgTmpBuf[MESSAGE_HEADER_SIZE], maxMsgSize-MESSAGE_HEADER_SIZE-1)
206 : m_process->readInfoLog(&m_dataMsgTmpBuf[MESSAGE_HEADER_SIZE], maxMsgSize-MESSAGE_HEADER_SIZE-1);
209 return false; // Didn't get any data.
214 m_dataMsgTmpBuf[msgSize-1] = 0;
217 Message::writeHeader(msgType, msgSize, &m_dataMsgTmpBuf[0], MESSAGE_HEADER_SIZE);
219 // Write to messagebuffer.
220 messageBuffer.pushFront(&m_dataMsgTmpBuf[0], msgSize);
222 DBG_PRINT((" wrote %d bytes of %s data\n", msgSize, msgType == MESSAGETYPE_INFO ? "info" : "log"));
227 bool TestDriver::writeMessage (ByteBuffer& messageBuffer, const Message& message)
232 if (messageBuffer.getNumFree() < (int)buf.size())
235 messageBuffer.pushFront(&buf[0], (int)buf.size());