Kaydet (Commit) 145f2e97 authored tarafından Luboš Luňák's avatar Luboš Luňák

avoid several 200ms delays in Qt tests

Since the tests are run during LO startup, this is not such a good idea.
Also redo the socket notifier test which seems a bit of an overkill
when a mere pipe will do (and I'm not sure TCP sockets would have
the data available the moment it's written to).

Change-Id: I6a436b286d20ceecf859f9028af98da03c2561b7
üst ef2ad2fb
......@@ -23,48 +23,50 @@
#include <qcoreapplication.h>
#include <qeventloop.h>
#include <qtimer.h>
namespace
{
const QEvent::Type eventType = QEvent::User;
class Test
class TestExcludePostedEvents
: public QObject
{
Q_OBJECT
public:
Test();
TestExcludePostedEvents();
virtual bool event( QEvent* e );
bool processed;
};
Test::Test()
TestExcludePostedEvents::TestExcludePostedEvents()
: processed( false )
{
}
bool Test::event( QEvent* e )
bool TestExcludePostedEvents::event( QEvent* e )
{
if( e->type() == eventType )
processed = true;
return QObject::event( e );
}
}
#define QVERIFY(a) \
if (!a) return 1;
static int tst_excludePostedEvents()
{
Test test;
TestExcludePostedEvents test;
QCoreApplication::postEvent( &test, new QEvent( eventType ));
QEventLoop loop;
QTimer::singleShot(200, &loop, SLOT(quit()));
loop.processEvents(QEventLoop::ExcludeUserInputEvents
| QEventLoop::ExcludeSocketNotifiers
// | QEventLoop::WaitForMoreEvents
| QEventLoop::X11ExcludeTimers);
QVERIFY( !test.processed );
QTimer::singleShot(200, &loop, SLOT(quit()));
loop.exec();
loop.processEvents();
QVERIFY( test.processed );
return 0;
}
......@@ -23,109 +23,62 @@
#include <qcoreapplication.h>
#include <qeventloop.h>
#include <qthread.h>
#include <qtimer.h>
#include <QtNetwork/qtcpserver.h>
#include <QtNetwork/qtcpsocket.h>
#include <qsocketnotifier.h>
#include <unistd.h>
// This is also used by a configure check.
#ifndef SAL_OVERRIDE
#define SAL_OVERRIDE
#endif
namespace
{
class SocketEventsTester: public QObject
class TestExcludeSocketNotifiers
: public QObject
{
Q_OBJECT
public:
SocketEventsTester()
{
socket = 0;
server = 0;
dataSent = false;
testResult = false;
dataArrived = false;
}
~SocketEventsTester()
{
delete socket;
delete server;
}
bool init()
{
bool ret = false;
server = new QTcpServer();
socket = new QTcpSocket();
connect(server, SIGNAL(newConnection()), this, SLOT(sendHello()));
connect(socket, SIGNAL(readyRead()), this, SLOT(sendAck()), Qt::DirectConnection);
if((ret = server->listen(QHostAddress::LocalHost, 0))) {
socket->connectToHost(server->serverAddress(), server->serverPort());
socket->waitForConnected();
}
return ret;
}
public:
TestExcludeSocketNotifiers( const int* pipes );
~TestExcludeSocketNotifiers();
bool received;
public slots:
void slotReceived();
private:
const int* pipes;
};
QTcpSocket *socket;
QTcpServer *server;
bool dataSent;
bool testResult;
bool dataArrived;
public slots:
void sendAck()
{
dataArrived = true;
}
void sendHello()
{
char data[10] ="HELLO";
qint64 size = sizeof(data);
TestExcludeSocketNotifiers::TestExcludeSocketNotifiers( const int* pipes )
: received( false )
, pipes( pipes )
{
}
QTcpSocket *serverSocket = server->nextPendingConnection();
serverSocket->write(data, size);
dataSent = serverSocket->waitForBytesWritten(-1);
QEventLoop loop;
//allow the TCP/IP stack time to loopback the data, so our socket is ready to read
QTimer::singleShot(200, &loop, SLOT(quit()));
loop.exec(QEventLoop::ExcludeSocketNotifiers);
testResult = dataArrived;
//check the deferred event is processed
QTimer::singleShot(200, &loop, SLOT(quit()));
loop.exec();
serverSocket->close();
QThread::currentThread()->exit(0);
}
};
TestExcludeSocketNotifiers::~TestExcludeSocketNotifiers()
{
close( pipes[ 0 ] );
close( pipes[ 1 ] );
}
class SocketTestThread : public QThread
void TestExcludeSocketNotifiers::slotReceived()
{
Q_OBJECT
public:
SocketTestThread():QThread(0),testResult(false){};
virtual void run() SAL_OVERRIDE
{
SocketEventsTester *tester = new SocketEventsTester();
if (tester->init())
exec();
dataSent = tester->dataSent;
testResult = tester->testResult;
dataArrived = tester->dataArrived;
delete tester;
}
bool dataSent;
bool testResult;
bool dataArrived;
};
received = true;
}
}
#define QVERIFY(a) \
if (!a) return 1;
static int tst_processEventsExcludeSocket()
{
SocketTestThread thread;
thread.start();
QVERIFY(thread.wait());
QVERIFY(thread.dataSent);
QVERIFY(!thread.testResult);
QVERIFY(thread.dataArrived);
int pipes[ 2 ];
if( pipe( pipes ) < 0 )
return 1;
TestExcludeSocketNotifiers test( pipes );
QSocketNotifier notifier( pipes[ 0 ], QSocketNotifier::Read );
QObject::connect( &notifier, SIGNAL( activated( int )), &test, SLOT( slotReceived()));
char dummy = 'a';
write( pipes[ 1 ], &dummy, 1 );
QEventLoop loop;
loop.processEvents( QEventLoop::ExcludeSocketNotifiers );
QVERIFY( !test.received );
loop.processEvents();
QVERIFY( test.received );
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment