From 0c6f3d69a947ec61397f0ca3793c1baf941a5c64 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 10 Oct 2022 13:39:19 -0700 Subject: [PATCH] tst_QTcpServer: use a random port number in addressReusable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just in case the same test is being run in parallel. We do that by creating a listening TCP server in the test process. This test is supposed to test the address reusability, so a clean close on a server that never accepted a connection should not cause reusability issues. Change-Id: I12a088d1ae424825abd3fffd171ccfb9fc5c09ee Reviewed-by: Qt CI Bot Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit a94731c2ad85f9dd40050a780f67c911bf12668e) Reviewed-by: Fabian Kosmale --- .../socket/qtcpserver/crashingServer/main.cpp | 9 +++++++-- .../socket/qtcpserver/tst_qtcpserver.cpp | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp b/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp index 1fe9f78a329..c14c429520e 100644 --- a/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp +++ b/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp @@ -26,16 +26,21 @@ int main(int argc, char *argv[]) #endif QCoreApplication app(argc, argv); + if (argc < 1) { + fprintf(stderr, "Need a port number\n"); + return 1; + } + int port = QByteArrayView(argv[1]).toInt(); QTcpServer server; - if (!server.listen(QHostAddress::LocalHost, 49199)) { + if (!server.listen(QHostAddress::LocalHost, port)) { fprintf(stderr, "Failed to listen: %s\n", server.errorString().toLatin1().constData()); if (server.serverError() == QTcpSocket::AddressInUseError) { // let's see if we can find the process that would be holding this // still open #ifdef Q_OS_LINUX static const char *ss_args[] = { - "ss", "-nap", "sport", "=", ":49199", nullptr + "ss", "-nap", "sport", "=", argv[1], nullptr }; dup2(STDERR_FILENO, STDOUT_FILENO); execvp(ss_args[0], const_cast(ss_args)); diff --git a/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp b/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp index e47c8f4654d..5c87a3f04b3 100644 --- a/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp +++ b/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp @@ -588,16 +588,25 @@ void tst_QTcpServer::addressReusable() QSKIP("No proxy support"); #endif // QT_NO_NETWORKPROXY } + + QTcpServer server; + QVERIFY(server.listen(QHostAddress::LocalHost, 0)); + quint16 serverPort = server.serverPort(); + qDebug() << "Got port" << serverPort; + server.close(); // cleanly close + + QTest::qSleep(10); + // The crashingServer process will crash once it gets a connection. QProcess process; QString processExe = crashingServerDir + "/crashingServer"; - process.start(processExe); + process.start(processExe, { QString::number(serverPort) }); QVERIFY2(process.waitForStarted(), qPrintable( QString::fromLatin1("Could not start %1: %2").arg(processExe, process.errorString()))); QVERIFY2(process.waitForReadyRead(5000), qPrintable(process.readAllStandardError())); QTcpSocket socket; - socket.connectToHost(QHostAddress::LocalHost, 49199); + socket.connectToHost(QHostAddress::LocalHost, serverPort); QVERIFY(socket.waitForConnected(5000)); QVERIFY(process.waitForFinished(30000)); @@ -605,8 +614,9 @@ void tst_QTcpServer::addressReusable() // Give the system some time. QTest::qSleep(10); - QTcpServer server; - QVERIFY(server.listen(QHostAddress::LocalHost, 49199)); + // listen again + QVERIFY2(server.listen(QHostAddress::LocalHost, serverPort), + qPrintable(server.errorString())); #endif }