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>
This commit is contained in:
Thiago Macieira 2022-10-10 13:39:19 -07:00
parent 742584b0f2
commit a94731c2ad
2 changed files with 21 additions and 6 deletions

View File

@ -18,16 +18,21 @@ int main(int argc, char *argv[])
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
#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<char **>(ss_args));

View File

@ -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
}