forked from qt-creator/qt-creator
Add a test ensuring we don't receive a warning when killing process
Before we were receiving the following warning when detroying the QtcProcess while still running: QProcess: Destroyed while process ("[program_name]") is still running. With the new implementation it won't appear anymore since terminating running QtcProcesses is done internally by the reaper. Change-Id: I14a0de136f463966cb1e4c43c5cb26b4de47e7ce Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -57,6 +57,7 @@ const char kLineCallback[] = "QTC_TST_QTCPROCESS_LINECALLBACK";
|
|||||||
const char kTestProcess[] = "QTC_TST_TEST_PROCESS";
|
const char kTestProcess[] = "QTC_TST_TEST_PROCESS";
|
||||||
const char kForwardProcess[] = "QTC_TST_FORWARD_PROCESS";
|
const char kForwardProcess[] = "QTC_TST_FORWARD_PROCESS";
|
||||||
const char kForwardSubProcess[] = "QTC_TST_FORWARD_SUB_PROCESS";
|
const char kForwardSubProcess[] = "QTC_TST_FORWARD_SUB_PROCESS";
|
||||||
|
const char kBlockingProcess[] = "QTC_TST_BLOCKING_PROCESS";
|
||||||
|
|
||||||
// The variables above are meant to be different custom executables. Inside initTestCase()
|
// The variables above are meant to be different custom executables. Inside initTestCase()
|
||||||
// we are detecting if one of these variables is set and invoke directly a respective custom
|
// we are detecting if one of these variables is set and invoke directly a respective custom
|
||||||
@@ -147,6 +148,13 @@ static void testForwardSubProcessMain()
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void blockingProcessSubProcessMain()
|
||||||
|
{
|
||||||
|
std::cout << "Blocking process successfully executed." << std::endl;
|
||||||
|
while (true)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
class MacroMapExpander : public AbstractMacroExpander {
|
class MacroMapExpander : public AbstractMacroExpander {
|
||||||
public:
|
public:
|
||||||
virtual bool resolveMacro(const QString &name, QString *ret, QSet<AbstractMacroExpander*> &seen)
|
virtual bool resolveMacro(const QString &name, QString *ret, QSet<AbstractMacroExpander*> &seen)
|
||||||
@@ -198,6 +206,7 @@ private slots:
|
|||||||
void notRunningAfterStartingNonExistingProgram();
|
void notRunningAfterStartingNonExistingProgram();
|
||||||
void processChannelForwarding_data();
|
void processChannelForwarding_data();
|
||||||
void processChannelForwarding();
|
void processChannelForwarding();
|
||||||
|
void killBlockingProcess();
|
||||||
|
|
||||||
void cleanupTestCase();
|
void cleanupTestCase();
|
||||||
|
|
||||||
@@ -230,6 +239,8 @@ void tst_QtcProcess::initTestCase()
|
|||||||
testForwardSubProcessMain();
|
testForwardSubProcessMain();
|
||||||
else if (qEnvironmentVariableIsSet(kForwardProcess))
|
else if (qEnvironmentVariableIsSet(kForwardProcess))
|
||||||
testForwardProcessMain();
|
testForwardProcessMain();
|
||||||
|
if (qEnvironmentVariableIsSet(kBlockingProcess))
|
||||||
|
blockingProcessSubProcessMain();
|
||||||
|
|
||||||
homeStr = QLatin1String("@HOME@");
|
homeStr = QLatin1String("@HOME@");
|
||||||
home = QDir::homePath();
|
home = QDir::homePath();
|
||||||
@@ -1106,6 +1117,65 @@ void tst_QtcProcess::processChannelForwarding()
|
|||||||
QCOMPARE(error.contains(QByteArray(forwardedErrorData)), errorForwarded);
|
QCOMPARE(error.contains(QByteArray(forwardedErrorData)), errorForwarded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This handler is inspired by the one used in qtbase/tests/auto/corelib/io/qfile/tst_qfile.cpp
|
||||||
|
class MessageHandler {
|
||||||
|
public:
|
||||||
|
MessageHandler(QtMessageHandler messageHandler = handler)
|
||||||
|
{
|
||||||
|
ok = true;
|
||||||
|
oldMessageHandler = qInstallMessageHandler(messageHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
~MessageHandler()
|
||||||
|
{
|
||||||
|
qInstallMessageHandler(oldMessageHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool testPassed()
|
||||||
|
{
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static void handler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||||
|
{
|
||||||
|
if (msg.startsWith("QProcess: Destroyed while process")
|
||||||
|
&& msg.endsWith("is still running.")) {
|
||||||
|
ok = false;
|
||||||
|
}
|
||||||
|
// Defer to old message handler.
|
||||||
|
if (oldMessageHandler)
|
||||||
|
oldMessageHandler(type, context, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static QtMessageHandler oldMessageHandler;
|
||||||
|
static bool ok;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool MessageHandler::ok = true;
|
||||||
|
QtMessageHandler MessageHandler::oldMessageHandler = 0;
|
||||||
|
|
||||||
|
void tst_QtcProcess::killBlockingProcess()
|
||||||
|
{
|
||||||
|
Environment env = Environment::systemEnvironment();
|
||||||
|
env.set(kBlockingProcess, {});
|
||||||
|
QStringList args = QCoreApplication::arguments();
|
||||||
|
const QString binary = args.takeFirst();
|
||||||
|
const CommandLine command(FilePath::fromString(binary), args);
|
||||||
|
|
||||||
|
MessageHandler msgHandler;
|
||||||
|
{
|
||||||
|
QtcProcess process;
|
||||||
|
process.setCommand(command);
|
||||||
|
process.setEnvironment(env);
|
||||||
|
process.start();
|
||||||
|
QVERIFY(process.waitForStarted());
|
||||||
|
QVERIFY(!process.waitForFinished(1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
QVERIFY(msgHandler.testPassed());
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_MAIN(tst_QtcProcess)
|
QTEST_MAIN(tst_QtcProcess)
|
||||||
|
|
||||||
#include "tst_qtcprocess.moc"
|
#include "tst_qtcprocess.moc"
|
||||||
|
Reference in New Issue
Block a user