Fix flaky test timing out sometimes while waiting for data

The test used to hang on waitForRead(), sometimes, which underneath
involve a poll()+read() syscall pair.
When this happened, the IMAP data came together with the proxy data on a
previous poll()+read() call and the proxy code had already consumed it.

We now wait for data only if data is not already available.

Fixes: QTBUG-96345
Change-Id: I084f5d1268a5091ea614fcec91c8d356dcb90d9f
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit c7e0a1a966c143485eb32211aaabf62cd8dbf6ca)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Dimitrios Apostolou 2021-09-17 12:32:14 +02:00 committed by Qt Cherry-pick Bot
parent c492bda966
commit 03224fa6de

View File

@ -312,12 +312,15 @@ void tst_QSocks5SocketEngine::simpleConnectToIMAP()
QCOMPARE(socketDevice.state(), QAbstractSocket::ConnectedState);
QCOMPARE(socketDevice.peerAddress(), QtNetworkSettings::imapServerIp());
// Wait for the greeting
QVERIFY2(socketDevice.waitForRead(), qPrintable("Socket error:" + socketDevice.errorString()));
// Wait for the greeting, if it hasn't arrived yet
qint64 available = socketDevice.bytesAvailable();
if (available == 0) {
QVERIFY2(socketDevice.waitForRead(), qPrintable("Socket error:" + socketDevice.errorString()));
available = socketDevice.bytesAvailable();
}
QVERIFY(available > 0);
// Read the greeting
qint64 available = socketDevice.bytesAvailable();
QVERIFY(available > 0);
QByteArray array;
array.resize(available);
QVERIFY(socketDevice.read(array.data(), array.size()) == available);