Debian Codegraph @

Source/Test/CReasonerTestfileTester.cpp

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "CReasonerTestfileTester.h"
22
23
24 namespace Konclude {
25
26 namespace Test {
27
28
29
30 CReasonerTestfileTester::CReasonerTestfileTester(CConfiguration *config, CWatchDog *watchDog)
31 : CIntervalThread("Testfile-Tester-Thread",watchDog), CLogIdentifier("::Konclude::Test::ReasonerTestfileTester",this) {
32 wCount = 1;
33 testProcess = 0;
34 processCrashed = false;
35 processTimedout = false;
36 testCommand = 0;
37 testfileTest = 0;
38 evaluator = 0;
39 commandRecordRouter = 0;
40 reasonerString = QString("ExperimentalReasonerLoader");
41
42 closeTimeout = 1000;
43 killTimeout = 1000;
44 runTimeout = 60000;
45
46 CConfigData *confData = config->getRelatedConfigChange("Konclude.Calculation.WorkerCount");
47 if (confData) {
48 QString workerCountString = confData->getString();
49 bool ok = true;
50 wCount = qMax(workerCountString.toInt(&ok),0);
51 if (!ok) {
52 wCount = CThread::idealThreadCount();
53 }
54 }
55
56 confData = config->getRelatedConfigChange("Konclude.Execution.CalculationManager");
57 if (confData && confData->supportsStringConvertion()) {
58 reasonerConfigString += QString(" +=%1=%2 ").arg(confData->getConfigDescription()->getConfigName()).arg(confData->getString());
59 }
60 confData = config->getRelatedConfigChange("Konclude.Calculation.WorkerCount");
61 if (confData && confData->supportsStringConvertion()) {
62 reasonerConfigString += QString(" +=%1=%2 ").arg(confData->getConfigDescription()->getConfigName()).arg(confData->getString());
63 }
64 confData = config->getRelatedConfigChange("Konclude.Calculation.ProcessorCount");
65 if (confData && confData->supportsStringConvertion()) {
66 reasonerConfigString += QString(" +=%1=%2 ").arg(confData->getConfigDescription()->getConfigName()).arg(confData->getString());
67 }
68 confData = config->getRelatedConfigChange("Konclude.OWLlink.Test.RunTimeout");
69 if (confData) {
70 runTimeout = confData->getInteger();
71 }
72 confData = config->getRelatedConfigChange("Konclude.OWLlink.Test.CloseTimeout");
73 if (confData) {
74 closeTimeout = confData->getInteger();
75 }
76 confData = config->getRelatedConfigChange("Konclude.OWLlink.Test.KillTimeout");
77 if (confData) {
78 killTimeout = confData->getInteger();
79 }
80 confData = config->getRelatedConfigChange("Konclude.Execution.DefaultReasonerManager");
81 if (confData) {
82 reasonerString = confData->getString();
83 if (reasonerString == "ASExperimentalReasonerManager") {
84 reasonerString = QString("ExperimentalReasonerLoader");
85 } else if (reasonerString == "ASAnalyseReasonerManager") {
86 reasonerString = QString("AnalyseReasonerLoader");
87 } else {
88 reasonerString = QString("DefaultReasonerLoader");
89 }
90 }
91
92 CConfigurationGroup* configGroup = config->getConfigurationGroup();
93 QList<CConfigDescription*> configDesList(configGroup->getConfigDescriptonList());
94 FOREACHIT (CConfigDescription* configDes, configDesList) {
95 QString configName = configDes->getConfigName();
96 bool propagateConfig = false;
97 if (configName.startsWith("Konclude.Calculation.")) {
98 propagateConfig = true;
99 } else if (configName.startsWith("Konclude.Execution.")) {
100 if (!configName.startsWith("Konclude.Execution.CommandLine")) {
101 propagateConfig = true;
102 }
103 }
104
105 if (propagateConfig) {
106 confData = config->getRelatedConfigChange(configName);
107 if (confData && confData->supportsStringConvertion()) {
108 reasonerConfigString += QString(" +=%1=%2 ").arg(configName).arg(confData->getString());
109 }
110 }
111 }
112
113
114 //runTimeout = 120000;
115
116 startThread();
117
118 }
119
120
121 CReasonerTestfileTester::~CReasonerTestfileTester() {
122 }
123
124
125 CReasonerTestfileTester *CReasonerTestfileTester::realizeTestCommand(CTestTestfileCommand *command, CReasonerTestfileEvaluator *testEvaluator) {
126 if (commandRecordRouter) {
127 delete commandRecordRouter;
128 commandRecordRouter = 0;
129 }
130 commandRecordRouter = new CCommandRecordRouter(command,this);
131 CStartProcessCommandRecord::makeRecord(commandRecordRouter);
132 testCommand = command;
133 testfileTest = testCommand->getReasonerTestfileTest();
134 testfile = testfileTest->getTestRequestFile();
135 evaluator = testEvaluator;
136 sendStartTestfileTestEvent();
137 return this;
138 }
139
140
141 bool CReasonerTestfileTester::processTimer(qint64 timerID) {
142 if (timerID == TIMERCLOSEPROCESSID) {
143 CUnspecifiedMessageInformationRecord::makeRecord(QString("Test %1 for testfile '%2' timed out.").arg(testfileTest->getTestResults()->getTotalTestCount()).arg(testfile),commandRecordRouter);
144 LOG(WARNING,getLogDomain(),logTr("Test %1 for '%2' timed out.").arg(testfileTest->getTestResults()->getTotalTestCount()+1).arg(testfile),this);
145 processTimedout = true;
146 sendEvaluateTestfileTestEvent();
147 return true;
148 }
149 return false;
150 }
151
152 bool CReasonerTestfileTester::processCustomsEvents(QEvent::Type type, CCustomEvent *event) {
153 if (CIntervalThread::processCustomsEvents(type,event)) {
154 return true;
155 } else if (type == EVENTTESTFILE) {
156 CTestTestfileEvent *tte = (CTestTestfileEvent *)event;
157 startTestfileTest();
158 } else if (type == EVENTEVALUATETESTFILE) {
159 CEvaluateTestfileTestEvent *ette = (CEvaluateTestfileTestEvent *)event;
160 evaluateTestfileTest();
161 }
162 return false;
163 }
164
165 void CReasonerTestfileTester::sendEvaluateTestfileTestEvent() {
166 QCoreApplication::postEvent(this,new CEvaluateTestfileTestEvent());
167 }
168
169 void CReasonerTestfileTester::sendStartTestfileTestEvent() {
170 QCoreApplication::postEvent(this,new CTestTestfileEvent());
171 }
172
173 void CReasonerTestfileTester::startTestfileTest() {
174 if (!testProcess) {
175 qint64 testTimeout = testfileTest->getTimeout();
176 if (testTimeout <= 0) {
177 testTimeout = runTimeout;
178 }
179 startTimerWithInterval(TIMERCLOSEPROCESSID,testTimeout);
180 processCrashed = false;
181 processTimedout = false;
182 testProcess = new QProcess();
183 QString workDir = QDir::currentPath();
184 if (testfileTest->hasWorkingDirectory()) {
185 workDir = testfileTest->getWorkingDirectory();
186 }
187 testProcess->setWorkingDirectory(workDir);
188 QString appString = QCoreApplication::applicationFilePath();
189 QStringList argumentList;
190 argumentList += QString("-ConfigurableCoutLogObserverLoader");
191 argumentList += QString("-%1 %2").arg(reasonerString).arg(reasonerConfigString);
192 argumentList += QString("-OWLlinkBatchFileLoader +=Konclude.OWLlink.RequestFile=%1").arg(testfile);
193 argumentList += QString("+=Konclude.OWLlink.WriteResponseToStandardError=true +=Konclude.OWLlink.CloseAfterProcessedRequest=true");
194
195 connect(testProcess,SIGNAL(error(QProcess::ProcessError)),this,SLOT(processError(QProcess::ProcessError)),Qt::DirectConnection);
196 connect(testProcess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(processFinished(int,QProcess::ExitStatus)),Qt::DirectConnection);
197
198 LOG(INFO,getLogDomain(),logTr("Starting %1. test for '%2'.").arg(testfileTest->getTestResults()->getTotalTestCount()+1).arg(testfile),this);
199
200 testProcess->start(appString,argumentList);
201 if (!testProcess->waitForStarted(10000)) {
202 testfileTest->getTestResults()->addFailedResult();
203 sendEvaluateTestfileTestEvent();
204 }
205 //testProcess->start("echo",QStringList()<<"hallo");
206
207 }
208 }
209
210
211 void CReasonerTestfileTester::evaluateTestfileTest() {
212 if (testProcess) {
213 stopTimer(TIMERCLOSEPROCESSID);
214 QString errorString = testProcess->errorString();
215 QByteArray standOutput = testProcess->readAllStandardOutput();
216 QString outputString(standOutput);
217 QByteArray standError = testProcess->readAllStandardError();
218 testProcess->close();
219 if (!testProcess->waitForFinished(closeTimeout)) {
220 testProcess->kill();
221 testProcess->waitForFinished(killTimeout);
222 }
223 if (processCrashed) {
224 testfileTest->getTestResults()->addTestResult(true,processTimedout,false,false,0.);
225 if (!processTimedout) {
226 LOG(WARNING,getLogDomain(),logTr("Test %1 for '%2' has CRASHED.").arg(testfileTest->getTestResults()->getTotalTestCount()).arg(testfile),this);
227 }
228 } else if (processTimedout) {
229 testfileTest->getTestResults()->addTestResult(false,true,false,false,0.);
230 } else {
231 evaluator->evaluateTestfile(testfileTest,standError);
232 }
233 disconnect(testProcess);
234 delete testProcess;
235 testProcess = 0;
236 CUnspecifiedMessageInformationRecord::makeRecord(QString("Test %1 for testfile '%2' concluded.").arg(testfileTest->getTestResults()->getTotalTestCount()).arg(testfile),commandRecordRouter);
237 //LOG(INFO,getLogDomain(),logTr("Test %1 for testfile '%2' concluded.").arg(testfileTest->getTestResults()->getTotalTestCount()).arg(testfile),this);
238
239 if (testfileTest->getTestResults()->getTotalTestCount() < testfileTest->getTestLoopCount()) {
240 sendStartTestfileTestEvent();
241 } else {
242 CStopProcessCommandRecord::makeRecord(commandRecordRouter);
243 CFinishProcessCommandRecord::makeRecord(commandRecordRouter);
244 }
245 }
246 }
247
248
249
250 void CReasonerTestfileTester::processError(QProcess::ProcessError error) {
251 processCrashed = true;
252 sendEvaluateTestfileTestEvent();
253 }
254
255 void CReasonerTestfileTester::processFinished(int exitCode, QProcess::ExitStatus exitStatus) {
256 if (exitStatus != QProcess::NormalExit) {
257 processCrashed = true;
258 }
259 sendEvaluateTestfileTestEvent();
260 }
261
262
263 }; // end namespace Test
264
265 }; // end namespace Konclude