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 <phartmann@blackberry.com>
Reviewed-by: Joerg Bornemann <joerg.bornemann@digia.com>
This commit is contained in:
Friedemann Kleint 2014-03-12 12:38:32 +01:00 committed by The Qt Project
parent e5785d6322
commit eb0032687f

View File

@ -530,7 +530,7 @@ class MiniHttpServer: public QTcpServer
{
Q_OBJECT
public:
QTcpSocket *client; // always the last one that was received
QPointer<QTcpSocket> 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<QSslError>& 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();
}
}