From eb0032687fd6a107bcd3dcff719a1ca761c87971 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 12 Mar 2014 12:38:32 +0100 Subject: [PATCH] Stabilize tst_qnetworkreply on Windows. Do not close connection in slot bytesWritten() since that can cause clients to fail with "Connection Closed". Instead, use deleteLater() to close properly and prevent leaking the sockets. FAIL! : tst_QNetworkReply::qtbug28035browserDoesNotLoadQtProjectOrgCorrectly() 'waitForFinish(reply) == Success' returned FALSE. ( QUrl( "http://localhost:58240" ) failed: # 2 "Connection closed" ) ..\tst_qnetworkreply.cpp(7067) : failure location Task-number: QTBUG-37449 Change-Id: Ib92cb62fae523370b2fb45e1ccfa217559732bc8 Reviewed-by: Peter Hartmann Reviewed-by: Joerg Bornemann --- .../qnetworkreply/tst_qnetworkreply.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index c883b77ea86..c448e893754 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -530,7 +530,7 @@ class MiniHttpServer: public QTcpServer { Q_OBJECT public: - QTcpSocket *client; // always the last one that was received + QPointer client; // always the last one that was received QByteArray dataToTransmit; QByteArray receivedData; QSemaphore ready; @@ -541,7 +541,7 @@ public: int totalConnections; MiniHttpServer(const QByteArray &data, bool ssl = false, QThread *thread = 0, bool useipv6 = false) - : client(0), dataToTransmit(data), doClose(true), doSsl(ssl), ipv6(useipv6), + : dataToTransmit(data), doClose(true), doSsl(ssl), ipv6(useipv6), multiple(false), totalConnections(0) { if (useipv6) { @@ -591,6 +591,7 @@ protected: } virtual void reply() { + Q_ASSERT(!client.isNull()); // we need to emulate the bytesWrittenSlot call if the data is empty. if (dataToTransmit.size() == 0) QMetaObject::invokeMethod(this, "bytesWrittenSlot", Qt::QueuedConnection); @@ -600,10 +601,11 @@ protected: private: void connectSocketSignals() { + Q_ASSERT(!client.isNull()); //qDebug() << "connectSocketSignals" << client; - connect(client, SIGNAL(readyRead()), this, SLOT(readyReadSlot())); - connect(client, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWrittenSlot())); - connect(client, SIGNAL(error(QAbstractSocket::SocketError)), + connect(client.data(), SIGNAL(readyRead()), this, SLOT(readyReadSlot())); + connect(client.data(), SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWrittenSlot())); + connect(client.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError))); } @@ -611,17 +613,20 @@ private slots: #ifndef QT_NO_SSL void slotSslErrors(const QList& errors) { + Q_ASSERT(!client.isNull()); qDebug() << "slotSslErrors" << client->errorString() << errors; } #endif void slotError(QAbstractSocket::SocketError err) { + Q_ASSERT(!client.isNull()); qDebug() << "slotError" << err << client->errorString(); } public slots: void readyReadSlot() { + Q_ASSERT(!client.isNull()); receivedData += client->readAll(); int doubleEndlPos = receivedData.indexOf("\r\n\r\n"); @@ -635,9 +640,11 @@ public slots: } void bytesWrittenSlot() { + Q_ASSERT(!client.isNull()); + // Disconnect and delete in next cycle (else Windows clients will fail with RemoteHostClosedError). if (doClose && client->bytesToWrite() == 0) { - client->disconnectFromHost(); disconnect(client, 0, this, 0); + client->deleteLater(); } }