lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*-------------------------------------------------------------------------
 * drawElements Quality Program Execution Server
 * ---------------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Test Driver.
 *//*--------------------------------------------------------------------*/
 
#include "xsTestDriver.hpp"
#include "deClock.h"
 
#include <string>
#include <vector>
#include <cstdio>
 
using std::string;
using std::vector;
 
#if 0
#    define DBG_PRINT(X) printf X
#else
#    define DBG_PRINT(X)
#endif
 
namespace xs
{
 
TestDriver::TestDriver (xs::TestProcess* testProcess)
   : m_state                (STATE_NOT_STARTED)
   , m_lastExitCode        (0)
   , m_process                (testProcess)
   , m_lastProcessDataTime    (0)
   , m_dataMsgTmpBuf        (SEND_RECV_TMP_BUFFER_SIZE)
{
}
 
TestDriver::~TestDriver (void)
{
   reset();
}
 
void TestDriver::reset (void)
{
   m_process->cleanup();
 
   m_state = STATE_NOT_STARTED;
}
 
void TestDriver::startProcess (const char* name, const char* params, const char* workingDir, const char* caseList)
{
   try
   {
       m_process->start(name, params, workingDir, caseList);
       m_state = STATE_PROCESS_STARTED;
   }
   catch (const TestProcessException& e)
   {
       printf("Failed to launch test process: %s\n", e.what());
       m_state                = STATE_PROCESS_LAUNCH_FAILED;
       m_lastLaunchFailure    = e.what();
   }
}
 
void TestDriver::stopProcess (void)
{
   m_process->terminate();
}
 
bool TestDriver::poll (ByteBuffer& messageBuffer)
{
   switch (m_state)
   {
       case STATE_NOT_STARTED:
           return false; // Nothing to report.
 
       case STATE_PROCESS_LAUNCH_FAILED:
           DBG_PRINT(("  STATE_PROCESS_LAUNCH_FAILED\n"));
           if (writeMessage(messageBuffer, ProcessLaunchFailedMessage(m_lastLaunchFailure.c_str())))
           {
               m_state                = STATE_NOT_STARTED;
               m_lastLaunchFailure    = "";
               return true;
           }
           else
               return false;
 
       case STATE_PROCESS_STARTED:
           DBG_PRINT(("  STATE_PROCESS_STARTED\n"));
           if (writeMessage(messageBuffer, ProcessStartedMessage()))
           {
               m_state = STATE_PROCESS_RUNNING;
               return true;
           }
           else
               return false;
 
       case STATE_PROCESS_RUNNING:
       {
           DBG_PRINT(("  STATE_PROCESS_RUNNING\n"));
           bool gotProcessData = false;
 
           // Poll log file and info buffer.
           gotProcessData = pollLogFile(messageBuffer)    || gotProcessData;
           gotProcessData = pollInfo(messageBuffer)    || gotProcessData;
 
           if (gotProcessData)
               return true; // Got IO.
 
           if (!m_process->isRunning())
           {
               // Process died.
               m_state                    = STATE_READING_DATA;
               m_lastExitCode            = m_process->getExitCode();
               m_lastProcessDataTime    = deGetMicroseconds();
 
               return true; // Got state change.
           }
 
           return false; // Nothing to report.
       }
 
       case STATE_READING_DATA:
       {
           DBG_PRINT(("  STATE_READING_DATA\n"));
           bool gotProcessData = false;
 
           // Poll log file and info buffer.
           gotProcessData = pollLogFile(messageBuffer)    || gotProcessData;
           gotProcessData = pollInfo(messageBuffer)    || gotProcessData;
 
           if (gotProcessData)
           {
               // Got data.
               m_lastProcessDataTime = deGetMicroseconds();
               return true;
           }
           else if (deGetMicroseconds() - m_lastProcessDataTime > READ_DATA_TIMEOUT*1000)
           {
               // Read timeout occurred.
               m_state = STATE_PROCESS_FINISHED;
               return true; // State change.
           }
           else
               return false; // Still waiting for data.
       }
 
       case STATE_PROCESS_FINISHED:
           DBG_PRINT(("  STATE_PROCESS_FINISHED\n"));
           if (writeMessage(messageBuffer, ProcessFinishedMessage(m_lastExitCode)))
           {
               // Signal TestProcess to clean up any remaining resources.
               m_process->cleanup();
 
               m_state            = STATE_NOT_STARTED;
               m_lastExitCode    = 0;
               return true;
           }
           else
               return false;
 
       default:
           DE_ASSERT(DE_FALSE);
           return false;
   }
}
 
bool TestDriver::pollLogFile (ByteBuffer& messageBuffer)
{
   return pollBuffer(messageBuffer, MESSAGETYPE_PROCESS_LOG_DATA);
}
 
bool TestDriver::pollInfo (ByteBuffer& messageBuffer)
{
   return pollBuffer(messageBuffer, MESSAGETYPE_INFO);
}
 
bool TestDriver::pollBuffer (ByteBuffer& messageBuffer, MessageType msgType)
{
   const int minBytesAvailable = MESSAGE_HEADER_SIZE + MIN_MSG_PAYLOAD_SIZE;
 
   if (messageBuffer.getNumFree() < minBytesAvailable)
       return false; // Not enough space in message buffer.
 
   const int    maxMsgSize    = de::min((int)m_dataMsgTmpBuf.size(), messageBuffer.getNumFree());
   int            numRead        = 0;
   int            msgSize        = MESSAGE_HEADER_SIZE+1; // One byte is reserved for terminating 0.
 
   // Fill in data \note Last byte is reserved for 0.
   numRead = msgType == MESSAGETYPE_PROCESS_LOG_DATA
           ? m_process->readTestLog(&m_dataMsgTmpBuf[MESSAGE_HEADER_SIZE], maxMsgSize-MESSAGE_HEADER_SIZE-1)
           : m_process->readInfoLog(&m_dataMsgTmpBuf[MESSAGE_HEADER_SIZE], maxMsgSize-MESSAGE_HEADER_SIZE-1);
 
   if (numRead <= 0)
       return false; // Didn't get any data.
 
   msgSize += numRead;
 
   // Terminate with 0.
   m_dataMsgTmpBuf[msgSize-1] = 0;
 
   // Write header.
   Message::writeHeader(msgType, msgSize, &m_dataMsgTmpBuf[0], MESSAGE_HEADER_SIZE);
 
   // Write to messagebuffer.
   messageBuffer.pushFront(&m_dataMsgTmpBuf[0], msgSize);
 
   DBG_PRINT(("  wrote %d bytes of %s data\n", msgSize, msgType == MESSAGETYPE_INFO ? "info" : "log"));
 
   return true;
}
 
bool TestDriver::writeMessage (ByteBuffer& messageBuffer, const Message& message)
{
   vector<deUint8> buf;
   message.write(buf);
 
   if (messageBuffer.getNumFree() < (int)buf.size())
       return false;
 
   messageBuffer.pushFront(&buf[0], (int)buf.size());
   return true;
}
 
} // xs