// 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); }
void LockTestClient::startTests() { QTcpSocket socket; socket.connectToHost ( "localhost", 55555); char control; #define GetNextCommand() if(socket.waitForReadyRead (-1) )\ {\ if(socket.bytesAvailable() > 1)\ qDebug()<<"Something is wrong here";\ socket.getChar(&control);\ if(control == 'a')\ {\ socket.disconnectFromHost();\ return;\ }\ if(control != 'n')\ { \ qDebug()<<"Wrong control command";\ }\ } if(socket.waitForConnected (-1)) { QFile file("lock.file"); if(!file.open(QIODevice::ReadWrite)) { qDebug()<<"Could not open lockfile"; return; } if(1) { GetNextCommand(); //Trying to readlock the same region QxtFileLock lock(&file,0x10,20,QxtFileLock::ReadLock); if(lock.lock()) socket.putChar('s'); //s for success f for fail else socket.putChar('f'); socket.waitForBytesWritten(-1); } if(1) { GetNextCommand(); //Trying to lock the same region with different locks QxtFileLock lock(&file,0x10,20,QxtFileLock::WriteLock); if(!lock.lock()) socket.putChar('s'); //s for success f for fail else socket.putChar('f'); socket.waitForBytesWritten(-1); } if(1) { GetNextCommand(); //Trying to writelock the same region QxtFileLock lock(&file,0x10,20,QxtFileLock::WriteLock); if(!lock.lock()) socket.putChar('s'); //s for success f for fail else socket.putChar('f'); socket.waitForBytesWritten(-1); } if(1) { GetNextCommand(); //Trying to writelock different regions QxtFileLock lock(&file,0x10+21,20,QxtFileLock::WriteLock); if(lock.lock()) socket.putChar('s'); //s for success f for fail else socket.putChar('f'); socket.waitForBytesWritten(-1); } } }