tst_QTcpServer: use a random port number in addressReusable

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 <qt_ci_bot@qt-project.org>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit a94731c2ad85f9dd40050a780f67c911bf12668e)
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Thiago Macieira 2022-10-10 13:39:19 -07:00
parent 8760c0c2f6
commit 0c6f3d69a9
2 changed files with 21 additions and 6 deletions

View File

@ -26,16 +26,21 @@ int main(int argc, char *argv[])
#endif #endif
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
if (argc < 1) {
fprintf(stderr, "Need a port number\n");
return 1;
}
int port = QByteArrayView(argv[1]).toInt();
QTcpServer server; 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()); fprintf(stderr, "Failed to listen: %s\n", server.errorString().toLatin1().constData());
if (server.serverError() == QTcpSocket::AddressInUseError) { if (server.serverError() == QTcpSocket::AddressInUseError) {
// let's see if we can find the process that would be holding this // let's see if we can find the process that would be holding this
// still open // still open
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
static const char *ss_args[] = { static const char *ss_args[] = {
"ss", "-nap", "sport", "=", ":49199", nullptr "ss", "-nap", "sport", "=", argv[1], nullptr
}; };
dup2(STDERR_FILENO, STDOUT_FILENO); dup2(STDERR_FILENO, STDOUT_FILENO);
execvp(ss_args[0], const_cast<char **>(ss_args)); execvp(ss_args[0], const_cast<char **>(ss_args));

View File

@ -588,16 +588,25 @@ void tst_QTcpServer::addressReusable()
QSKIP("No proxy support"); QSKIP("No proxy support");
#endif // QT_NO_NETWORKPROXY #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. // The crashingServer process will crash once it gets a connection.
QProcess process; QProcess process;
QString processExe = crashingServerDir + "/crashingServer"; QString processExe = crashingServerDir + "/crashingServer";
process.start(processExe); process.start(processExe, { QString::number(serverPort) });
QVERIFY2(process.waitForStarted(), qPrintable( QVERIFY2(process.waitForStarted(), qPrintable(
QString::fromLatin1("Could not start %1: %2").arg(processExe, process.errorString()))); QString::fromLatin1("Could not start %1: %2").arg(processExe, process.errorString())));
QVERIFY2(process.waitForReadyRead(5000), qPrintable(process.readAllStandardError())); QVERIFY2(process.waitForReadyRead(5000), qPrintable(process.readAllStandardError()));
QTcpSocket socket; QTcpSocket socket;
socket.connectToHost(QHostAddress::LocalHost, 49199); socket.connectToHost(QHostAddress::LocalHost, serverPort);
QVERIFY(socket.waitForConnected(5000)); QVERIFY(socket.waitForConnected(5000));
QVERIFY(process.waitForFinished(30000)); QVERIFY(process.waitForFinished(30000));
@ -605,8 +614,9 @@ void tst_QTcpServer::addressReusable()
// Give the system some time. // Give the system some time.
QTest::qSleep(10); QTest::qSleep(10);
QTcpServer server; // listen again
QVERIFY(server.listen(QHostAddress::LocalHost, 49199)); QVERIFY2(server.listen(QHostAddress::LocalHost, serverPort),
qPrintable(server.errorString()));
#endif #endif
} }