tests/auto/network: Avoid unconditional qWait()s

Replace with QSignalSpy or QTRY_COMPARE when possible.

Task-number: QTBUG-63992
Change-Id: I18dc8837301424855487a12ee62451a5aeb21bf0
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Kari Oikarinen 2018-03-29 11:50:58 +03:00
parent 09d4faa008
commit c580644fe9
5 changed files with 19 additions and 33 deletions

View File

@ -2078,8 +2078,6 @@ void tst_QFtp::doneSignal()
if (QTestEventLoop::instance().timeout()) if (QTestEventLoop::instance().timeout())
QFAIL("Network operation timed out"); QFAIL("Network operation timed out");
QTest::qWait(200);
QCOMPARE(spy.count(), 1); QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().first().toBool(), false); QCOMPARE(spy.first().first().toBool(), false);
} }

View File

@ -7816,13 +7816,14 @@ void tst_QNetworkReply::closeDuringDownload()
QFETCH(QUrl, url); QFETCH(QUrl, url);
QNetworkRequest request(url); QNetworkRequest request(url);
QNetworkReply* reply = manager.get(request); QNetworkReply* reply = manager.get(request);
connect(reply, SIGNAL(readyRead()), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy readyReadSpy(reply, &QNetworkReply::readyRead);
QTestEventLoop::instance().enterLoop(10); QVERIFY(readyReadSpy.wait(10000));
QVERIFY(!QTestEventLoop::instance().timeout()); QSignalSpy destroySpy(reply, &QObject::destroyed);
connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()));
reply->close(); reply->close();
reply->deleteLater(); reply->deleteLater();
QTest::qWait(1000); //cancelling ftp takes some time, this avoids a warning caused by test's cleanup() destroying the connection cache before the abort is finished // Wait for destruction to avoid a warning caused by test's cleanup()
// destroying the connection cache before the abort is finished
QVERIFY(destroySpy.wait());
} }
void tst_QNetworkReply::ftpAuthentication_data() void tst_QNetworkReply::ftpAuthentication_data()

View File

@ -1225,12 +1225,13 @@ public:
socket.write("testing\n"); socket.write("testing\n");
exec(); exec();
} }
signals:
void bytesWrittenReceived();
public slots: public slots:
void bytesWritten(qint64) { void bytesWritten(qint64) {
emit bytesWrittenReceived();
exit(); exit();
} }
private:
}; };
/* /*
@ -1248,11 +1249,12 @@ void tst_QLocalSocket::bytesWrittenSignal()
QLocalServer server; QLocalServer server;
QVERIFY(server.listen("qlocalsocket_readyread")); QVERIFY(server.listen("qlocalsocket_readyread"));
WriteThread writeThread; WriteThread writeThread;
QSignalSpy receivedSpy(&writeThread, &WriteThread::bytesWrittenReceived);
writeThread.start(); writeThread.start();
bool timedOut = false; bool timedOut = false;
QVERIFY(server.waitForNewConnection(3000, &timedOut)); QVERIFY(server.waitForNewConnection(3000, &timedOut));
QVERIFY(!timedOut); QVERIFY(!timedOut);
QTest::qWait(2000); QVERIFY(receivedSpy.wait(2000));
QVERIFY(writeThread.wait(2000)); QVERIFY(writeThread.wait(2000));
} }

View File

@ -2048,11 +2048,7 @@ void tst_QTcpSocket::connectToLocalHostNoService()
// port with no service listening. // port with no service listening.
QTcpSocket *socket = newSocket(); QTcpSocket *socket = newSocket();
socket->connectToHost("localhost", 31415); // no service running here, one suspects socket->connectToHost("localhost", 31415); // no service running here, one suspects
QTRY_COMPARE(socket->state(), QTcpSocket::UnconnectedState);
while(socket->state() == QTcpSocket::HostLookupState || socket->state() == QTcpSocket::ConnectingState) {
QTest::qWait(100);
}
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
delete socket; delete socket;
} }
#endif #endif

View File

@ -2908,10 +2908,7 @@ void tst_QSslSocket::qtbug18498_peek2()
QScopedPointer<QSslSocket> server(listener.socket); QScopedPointer<QSslSocket> server(listener.socket);
QVERIFY(server->write("HELLO\r\n", 7)); QVERIFY(server->write("HELLO\r\n", 7));
QElapsedTimer stopwatch; QTRY_COMPARE(client->bytesAvailable(), 7);
stopwatch.start();
while (client->bytesAvailable() < 7 && stopwatch.elapsed() < 5000)
QTest::qWait(100);
char c; char c;
QCOMPARE(client->peek(&c,1), 1); QCOMPARE(client->peek(&c,1), 1);
QCOMPARE(c, 'H'); QCOMPARE(c, 'H');
@ -2930,8 +2927,7 @@ void tst_QSslSocket::qtbug18498_peek2()
bigblock.fill('#', QIODEVICE_BUFFERSIZE + 1024); bigblock.fill('#', QIODEVICE_BUFFERSIZE + 1024);
QVERIFY(client->write(QByteArray("head"))); QVERIFY(client->write(QByteArray("head")));
QVERIFY(client->write(bigblock)); QVERIFY(client->write(bigblock));
while (server->bytesAvailable() < bigblock.length() + 4 && stopwatch.elapsed() < 5000) QTRY_COMPARE(server->bytesAvailable(), bigblock.length() + 4);
QTest::qWait(100);
QCOMPARE(server->read(4), QByteArray("head")); QCOMPARE(server->read(4), QByteArray("head"));
QCOMPARE(server->peek(bigblock.length()), bigblock); QCOMPARE(server->peek(bigblock.length()), bigblock);
b.reserve(bigblock.length()); b.reserve(bigblock.length());
@ -2947,10 +2943,7 @@ void tst_QSslSocket::qtbug18498_peek2()
QCOMPARE(server->readAll(), bigblock); QCOMPARE(server->readAll(), bigblock);
QVERIFY(client->write("STARTTLS\r\n")); QVERIFY(client->write("STARTTLS\r\n"));
stopwatch.start(); QTRY_COMPARE(server->bytesAvailable(), 10);
// ### Qt5 use QTRY_VERIFY
while (server->bytesAvailable() < 10 && stopwatch.elapsed() < 5000)
QTest::qWait(100);
QCOMPARE(server->peek(&c,1), 1); QCOMPARE(server->peek(&c,1), 1);
QCOMPARE(c, 'S'); QCOMPARE(c, 'S');
b = server->peek(3); b = server->peek(3);
@ -2983,9 +2976,7 @@ void tst_QSslSocket::qtbug18498_peek2()
client->startClientEncryption(); client->startClientEncryption();
QVERIFY(server->write("hello\r\n", 7)); QVERIFY(server->write("hello\r\n", 7));
stopwatch.start(); QTRY_COMPARE(client->bytesAvailable(), 7);
while (client->bytesAvailable() < 7 && stopwatch.elapsed() < 5000)
QTest::qWait(100);
QVERIFY(server->mode() == QSslSocket::SslServerMode && client->mode() == QSslSocket::SslClientMode); QVERIFY(server->mode() == QSslSocket::SslServerMode && client->mode() == QSslSocket::SslClientMode);
QCOMPARE(client->peek(&c,1), 1); QCOMPARE(client->peek(&c,1), 1);
QCOMPARE(c, 'h'); QCOMPARE(c, 'h');
@ -2996,9 +2987,7 @@ void tst_QSslSocket::qtbug18498_peek2()
QCOMPARE(client->readAll(), QByteArray("ello\r\n")); QCOMPARE(client->readAll(), QByteArray("ello\r\n"));
QVERIFY(client->write("goodbye\r\n")); QVERIFY(client->write("goodbye\r\n"));
stopwatch.start(); QTRY_COMPARE(server->bytesAvailable(), 9);
while (server->bytesAvailable() < 9 && stopwatch.elapsed() < 5000)
QTest::qWait(100);
QCOMPARE(server->peek(&c,1), 1); QCOMPARE(server->peek(&c,1), 1);
QCOMPARE(c, 'g'); QCOMPARE(c, 'g');
QCOMPARE(server->readAll(), QByteArray("goodbye\r\n")); QCOMPARE(server->readAll(), QByteArray("goodbye\r\n"));