From 4111d8e8e789c815ae37eb9903b042124e169078 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Mon, 16 Nov 2020 19:23:53 +0100 Subject: [PATCH] tst_QTcpSocket::connectToHostError - handle possible timeouts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... instead of failing the test. On Ubuntu 20.04 when calling 'connect' with 0.0.0.1 we get EINPROGRESS and nothing else, since our own internal timer has 30 s. timeout, the event loop in the test stops before this and no socket error detected yet. Handle such situation without failing a test. Fixes: QTBUG-88042 Change-Id: Id6add27fcf9bbbe5fbf83a193652edf08fbad8d6 Reviewed-by: MÃ¥rten Nordheim --- .../auto/network/socket/qtcpsocket/BLACKLIST | 1 - .../socket/qtcpsocket/tst_qtcpsocket.cpp | 39 +++++++++++++++---- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/tests/auto/network/socket/qtcpsocket/BLACKLIST b/tests/auto/network/socket/qtcpsocket/BLACKLIST index 95e3e63977..ac243c0203 100644 --- a/tests/auto/network/socket/qtcpsocket/BLACKLIST +++ b/tests/auto/network/socket/qtcpsocket/BLACKLIST @@ -2,7 +2,6 @@ windows [connectToHostError] windows-10 gcc developer-build -ubuntu-20.04 # QTBUG-66247 [delayedClose:WithSocks5Proxy] windows-10 gcc developer-build diff --git a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp index 0608fa8e63..4ebdaa6851 100644 --- a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp @@ -83,6 +83,10 @@ #include +#ifdef Q_OS_LINUX +#include "private/qnativesocketengine_p.h" +#endif // Q_OS_LINUX + #include "private/qhostinfo_p.h" #include "../../../network-settings.h" @@ -2090,31 +2094,50 @@ void tst_QTcpSocket::nestedEventLoopInErrorSlot() void tst_QTcpSocket::connectToHostError_data() { QTest::addColumn("host"); - QTest::addColumn("port"); + QTest::addColumn("port"); QTest::addColumn("expectedError"); - QTest::newRow("localhost no service") << QStringLiteral("localhost") << 31415 << QAbstractSocket::ConnectionRefusedError; - QTest::newRow("unreachable") << QStringLiteral("0.0.0.1") << 65000 << QAbstractSocket::NetworkError; + QTest::newRow("localhost no service") << QStringLiteral("localhost") << quint16(31415) << QAbstractSocket::ConnectionRefusedError; + QTest::newRow("unreachable") << QStringLiteral("0.0.0.1") << quint16(65000) << QAbstractSocket::NetworkError; } void tst_QTcpSocket::connectToHostError() { + // We are aware of at least one OS in our CI, that would fail + // the test due to timeout - it's Ubuntu 20.04 and 'connect' + // to 0.0.0.1 there return EINPROGRESS, with no other error + // ever received, so only our own internal 30 s. timer can + // detect a connection timeout. + std::unique_ptr socket(newSocket()); QAbstractSocket::SocketError error = QAbstractSocket::UnknownSocketError; - QFETCH(QString, host); - QFETCH(int, port); + QFETCH(const QString, host); + QFETCH(const quint16, port); QFETCH(QAbstractSocket::SocketError, expectedError); - connect(socket.get(), &QAbstractSocket::errorOccurred, [&](QAbstractSocket::SocketError socketError){ + QTestEventLoop eventLoop; + connect(socket.get(), &QAbstractSocket::errorOccurred, socket.get(), + [&](QAbstractSocket::SocketError socketError) { error = socketError; + QTimer::singleShot(0, &eventLoop, [&]{eventLoop.exitLoop();}); }); - socket->connectToHost(host, port); // no service running here, one suspects - QTRY_COMPARE_WITH_TIMEOUT(socket->state(), QTcpSocket::UnconnectedState, 7000); + + socket->connectToHost(host, port); + eventLoop.enterLoopMSecs(7000); + if (eventLoop.timeout() && port == 65000) { + // Let's at least verify it's not in connected state: + QVERIFY(socket->state() != QAbstractSocket::ConnectedState); + QSKIP("Connection to unreachable host timed out, skipping the rest of the test"); + } + + QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); + if (error != expectedError && error == QAbstractSocket::ConnectionRefusedError) QEXPECT_FAIL("unreachable", "CI firewall interfers with this test", Continue); + QCOMPARE(error, expectedError); }