From c668fd940d0c610324254d5aa5aab6e0769f78a6 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Wed, 11 Mar 2020 10:38:20 +0100 Subject: [PATCH] Fix 'out of process' autotests We are, arguably, not testing QProcess and its ability to start or finish, we test QUdpSocket. If, for some reason (as we discovered on some specific machines recently) the process does not start or does not produce any output (canReadLine), we QSKIP instead of failing. Also, all those QCOMPARE will bypass the part there we stop processes - so must be RAII-protected. Fixes: QTBUG-82717 Change-Id: Idfb0d4a483d753f336b3827875eeaf51c79270e2 Reviewed-by: Volker Hilsheimer --- .../socket/qudpsocket/tst_qudpsocket.cpp | 82 ++++++++++++------- 1 file changed, 54 insertions(+), 28 deletions(-) diff --git a/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp b/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp index 0f419e9de4..bed8a8b129 100644 --- a/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp +++ b/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp @@ -1090,12 +1090,21 @@ void tst_QUdpSocket::outOfProcessConnectedClientServerTest() QProcess serverProcess; serverProcess.start(QLatin1String("clientserver/clientserver server 1 1"), QIODevice::ReadWrite | QIODevice::Text); - QVERIFY2(serverProcess.waitForStarted(3000), - qPrintable("Failed to start subprocess: " + serverProcess.errorString())); + + const auto serverProcessCleaner = qScopeGuard([&serverProcess] { + serverProcess.kill(); + serverProcess.waitForFinished(); + }); + + if (!serverProcess.waitForStarted(3000)) + QSKIP("Failed to start server as a subprocess"); // Wait until the server has started and reports success. - while (!serverProcess.canReadLine()) - QVERIFY(serverProcess.waitForReadyRead(3000)); + while (!serverProcess.canReadLine()) { + if (!serverProcess.waitForReadyRead(3000)) + QSKIP("No output from the server process, bailing out"); + } + QByteArray serverGreeting = serverProcess.readLine(); QVERIFY(serverGreeting != QByteArray("XXX\n")); int serverPort = serverGreeting.trimmed().toInt(); @@ -1105,12 +1114,21 @@ void tst_QUdpSocket::outOfProcessConnectedClientServerTest() clientProcess.start(QString::fromLatin1("clientserver/clientserver connectedclient %1 %2") .arg(QLatin1String("127.0.0.1")).arg(serverPort), QIODevice::ReadWrite | QIODevice::Text); - QVERIFY2(clientProcess.waitForStarted(3000), - qPrintable("Failed to start subprocess: " + clientProcess.errorString())); - // Wait until the server has started and reports success. - while (!clientProcess.canReadLine()) - QVERIFY(clientProcess.waitForReadyRead(3000)); + const auto clientProcessCleaner = qScopeGuard([&clientProcess] { + clientProcess.kill(); + clientProcess.waitForFinished(); + }); + + if (!clientProcess.waitForStarted(3000)) + QSKIP("Client process did not start"); + + // Wait until the client has started and reports success. + while (!clientProcess.canReadLine()) { + if (!clientProcess.waitForReadyRead(3000)) + QSKIP("No output from the client process, bailing out"); + } + QByteArray clientGreeting = clientProcess.readLine(); QCOMPARE(clientGreeting, QByteArray("ok\n")); @@ -1135,11 +1153,6 @@ void tst_QUdpSocket::outOfProcessConnectedClientServerTest() QCOMPARE(serverData.at(i * 3 + 2).trimmed().mid(8).toInt(), sdata.mid(4).trimmed().toInt() * 2); } - - clientProcess.kill(); - QVERIFY(clientProcess.waitForFinished()); - serverProcess.kill(); - QVERIFY(serverProcess.waitForFinished()); #endif } @@ -1151,12 +1164,21 @@ void tst_QUdpSocket::outOfProcessUnconnectedClientServerTest() QProcess serverProcess; serverProcess.start(QLatin1String("clientserver/clientserver server 1 1"), QIODevice::ReadWrite | QIODevice::Text); - QVERIFY2(serverProcess.waitForStarted(3000), - qPrintable("Failed to start subprocess: " + serverProcess.errorString())); + + const auto serverProcessCleaner = qScopeGuard([&serverProcess] { + serverProcess.kill(); + serverProcess.waitForFinished(); + }); + + if (!serverProcess.waitForStarted(3000)) + QSKIP("Failed to start the server subprocess"); // Wait until the server has started and reports success. - while (!serverProcess.canReadLine()) - QVERIFY(serverProcess.waitForReadyRead(3000)); + while (!serverProcess.canReadLine()) { + if (!serverProcess.waitForReadyRead(3000)) + QSKIP("No output from the server, probably, it is not running"); + } + QByteArray serverGreeting = serverProcess.readLine(); QVERIFY(serverGreeting != QByteArray("XXX\n")); int serverPort = serverGreeting.trimmed().toInt(); @@ -1166,12 +1188,21 @@ void tst_QUdpSocket::outOfProcessUnconnectedClientServerTest() clientProcess.start(QString::fromLatin1("clientserver/clientserver unconnectedclient %1 %2") .arg(QLatin1String("127.0.0.1")).arg(serverPort), QIODevice::ReadWrite | QIODevice::Text); - QVERIFY2(clientProcess.waitForStarted(3000), - qPrintable("Failed to start subprocess: " + clientProcess.errorString())); - // Wait until the server has started and reports success. - while (!clientProcess.canReadLine()) - QVERIFY(clientProcess.waitForReadyRead(3000)); + const auto clientProcessCleaner = qScopeGuard([&clientProcess] { + clientProcess.kill(); + clientProcess.waitForFinished(); + }); + + if (!clientProcess.waitForStarted(3000)) + QSKIP("Failed to start the client's subprocess"); + + // Wait until the client has started and reports success. + while (!clientProcess.canReadLine()) { + if (!clientProcess.waitForReadyRead(3000)) + QSKIP("The client subprocess produced not output, exiting."); + } + QByteArray clientGreeting = clientProcess.readLine(); QCOMPARE(clientGreeting, QByteArray("ok\n")); @@ -1197,11 +1228,6 @@ void tst_QUdpSocket::outOfProcessUnconnectedClientServerTest() QCOMPARE(serverData.at(i * 3 + 2).trimmed().mid(8).toInt(), sdata.mid(4).trimmed().toInt() * 2); } - - clientProcess.kill(); - QVERIFY(clientProcess.waitForFinished()); - serverProcess.kill(); - QVERIFY(serverProcess.waitForFinished()); #endif }