Move the socket test to an appropriate file

Actually, it tests the buffered QTcpSocket. Place it in
tst_qtcpsocket.cpp instead of tst_qabstractsocket.cpp.

Change-Id: I3055c4773d0de74c238be4f11b2d1c07ddad4485
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Alex Trotsenko 2015-02-21 11:41:01 +02:00
parent dbd6d158e3
commit dbfd3c0952
2 changed files with 46 additions and 53 deletions

View File

@ -37,11 +37,6 @@
#include <qcoreapplication.h>
#include <qdebug.h>
#include <qabstractsocket.h>
#include <qtcpserver.h>
#include <qtcpsocket.h>
#ifndef QT_NO_SSL
#include <qsslsocket.h>
#endif
class tst_QAbstractSocket : public QObject
{
@ -52,9 +47,7 @@ public:
virtual ~tst_QAbstractSocket();
private slots:
void initTestCase();
void getSetCheck();
void serverDisconnectWithBuffered();
};
tst_QAbstractSocket::tst_QAbstractSocket()
@ -73,11 +66,6 @@ public:
void setPeerPort(quint16 port) { QAbstractSocket::setPeerPort(port); }
};
void tst_QAbstractSocket::initTestCase()
{
qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState");
}
// Testing get/set functions
void tst_QAbstractSocket::getSetCheck()
{
@ -106,46 +94,5 @@ void tst_QAbstractSocket::getSetCheck()
QCOMPARE(quint16(0xffff), obj1.peerPort());
}
// Test buffered socket being properly closed on remote disconnect
void tst_QAbstractSocket::serverDisconnectWithBuffered()
{
QTcpServer tcpServer;
#ifndef QT_NO_SSL
QSslSocket testSocket;
#else
QTcpSocket testSocket;
#endif
QVERIFY(tcpServer.listen(QHostAddress::LocalHost));
testSocket.connectToHost(tcpServer.serverAddress(), tcpServer.serverPort());
// Accept connection on server side
QVERIFY(tcpServer.waitForNewConnection(5000));
QTcpSocket *newConnection = tcpServer.nextPendingConnection();
// Send one char and drop link
QVERIFY(newConnection != NULL);
QVERIFY(newConnection->putChar(0));
QVERIFY(newConnection->flush());
delete newConnection;
QVERIFY(testSocket.waitForConnected(5000)); // ready for write
QVERIFY(testSocket.state() == QAbstractSocket::ConnectedState);
QSignalSpy spyStateChanged(&testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
QSignalSpy spyDisconnected(&testSocket, SIGNAL(disconnected()));
QVERIFY(testSocket.waitForReadyRead(5000)); // have one char already in internal buffer
char buf[128];
QCOMPARE(testSocket.read(buf, sizeof(buf)), Q_INT64_C(1));
if (testSocket.state() != QAbstractSocket::UnconnectedState) {
QVERIFY(testSocket.waitForDisconnected(5000));
QVERIFY(testSocket.state() == QAbstractSocket::UnconnectedState);
}
// Test signal emitting
QVERIFY(spyDisconnected.count() == 1);
QVERIFY(spyStateChanged.count() > 0);
QVERIFY(qvariant_cast<QAbstractSocket::SocketState>(spyStateChanged.last().first())
== QAbstractSocket::UnconnectedState);
}
QTEST_MAIN(tst_QAbstractSocket)
#include "tst_qabstractsocket.moc"

View File

@ -197,6 +197,7 @@ private slots:
void setSocketOption();
void clientSendDataOnDelayedDisconnect();
void serverDisconnectWithBuffered();
protected slots:
void nonBlockingIMAP_hostFound();
@ -2847,5 +2848,50 @@ void tst_QTcpSocket::clientSendDataOnDelayedDisconnect()
delete socket;
}
// Test buffered socket being properly closed on remote disconnect
void tst_QTcpSocket::serverDisconnectWithBuffered()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy)
return;
qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState");
QTcpServer tcpServer;
QTcpSocket *socket = newSocket();
QVERIFY(tcpServer.listen(QHostAddress::LocalHost));
socket->connectToHost(tcpServer.serverAddress(), tcpServer.serverPort());
// Accept connection on server side
QVERIFY(tcpServer.waitForNewConnection(5000));
QTcpSocket *newConnection = tcpServer.nextPendingConnection();
// Send one char and drop link
QVERIFY(newConnection != NULL);
QVERIFY(newConnection->putChar(0));
QVERIFY(newConnection->flush());
delete newConnection;
QVERIFY(socket->waitForConnected(5000)); // ready for write
QVERIFY(socket->state() == QAbstractSocket::ConnectedState);
QSignalSpy spyStateChanged(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
QSignalSpy spyDisconnected(socket, SIGNAL(disconnected()));
QVERIFY(socket->waitForReadyRead(5000)); // have one char already in internal buffer
char buf[128];
QCOMPARE(socket->read(buf, sizeof(buf)), Q_INT64_C(1));
if (socket->state() != QAbstractSocket::UnconnectedState) {
QVERIFY(socket->waitForDisconnected(5000));
QVERIFY(socket->state() == QAbstractSocket::UnconnectedState);
}
// Test signal emitting
QVERIFY(spyDisconnected.count() == 1);
QVERIFY(spyStateChanged.count() > 0);
QVERIFY(qvariant_cast<QAbstractSocket::SocketState>(spyStateChanged.last().first())
== QAbstractSocket::UnconnectedState);
delete socket;
}
QTEST_MAIN(tst_QTcpSocket)
#include "tst_qtcpsocket.moc"