Fix tst_QEventLoop::processEventsExcludeSocket test

The testcase always returns the expected result, independently of the
QEventLoop::ExcludeSocketNotifiers flag to processEvents.

In Qt4 the same test uses an intermediate QEventLoop and already runs
it before the QEventLoop::ExcludeSocketNotifiers:

  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);

This fixes and improves the test by connecting, processing and
checking the bytesWritten signal for the pending connection socket.

Change-Id: I1b1d2b7b83910c87ba3fe48e29ac9fd585ac62ad
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Jan-Marek Glogowski 2014-03-11 15:14:16 +01:00 committed by Raphael Kubo da Costa
parent cd75ca0dbd
commit a2f78ea0a1

View File

@ -40,6 +40,7 @@
#include <private/qeventloop_p.h> #include <private/qeventloop_p.h>
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX)
#include <private/qeventdispatcher_unix_p.h> #include <private/qeventdispatcher_unix_p.h>
#include <QtCore/private/qcore_unix_p.h>
#if defined(HAVE_GLIB) #if defined(HAVE_GLIB)
#include <private/qeventdispatcher_glib_p.h> #include <private/qeventdispatcher_glib_p.h>
#endif #endif
@ -172,7 +173,9 @@ private slots:
void execAfterExit(); void execAfterExit();
void wakeUp(); void wakeUp();
void quit(); void quit();
#if defined(Q_OS_UNIX)
void processEventsExcludeSocket(); void processEventsExcludeSocket();
#endif
void processEventsExcludeTimers(); void processEventsExcludeTimers();
void deliverInDefinedOrder(); void deliverInDefinedOrder();
@ -383,6 +386,7 @@ void tst_QEventLoop::customEvent(QEvent *e)
} }
} }
#if defined(Q_OS_UNIX)
class SocketEventsTester: public QObject class SocketEventsTester: public QObject
{ {
Q_OBJECT Q_OBJECT
@ -391,8 +395,10 @@ public:
{ {
socket = 0; socket = 0;
server = 0; server = 0;
dataArrived = false; dataSent = false;
dataReadable = false;
testResult = false; testResult = false;
dataArrived = false;
} }
~SocketEventsTester() ~SocketEventsTester()
{ {
@ -415,8 +421,10 @@ public:
QTcpSocket *socket; QTcpSocket *socket;
QTcpServer *server; QTcpServer *server;
bool dataArrived; bool dataSent;
bool dataReadable;
bool testResult; bool testResult;
bool dataArrived;
public slots: public slots:
void sendAck() void sendAck()
{ {
@ -428,12 +436,26 @@ public slots:
qint64 size = sizeof(data); qint64 size = sizeof(data);
QTcpSocket *serverSocket = server->nextPendingConnection(); QTcpSocket *serverSocket = server->nextPendingConnection();
QCoreApplication::processEvents();
serverSocket->write(data, size); serverSocket->write(data, size);
serverSocket->flush(); dataSent = serverSocket->waitForBytesWritten(-1);
QTest::qSleep(200); //allow the TCP/IP stack time to loopback the data, so our socket is ready to read
QCoreApplication::processEvents(QEventLoop::ExcludeSocketNotifiers); if (dataSent) {
testResult = dataArrived; fd_set fdread;
QCoreApplication::processEvents(); //check the deferred event is processed int fd = socket->socketDescriptor();
FD_ZERO(&fdread);
FD_SET(fd, &fdread);
dataReadable = (1 == qt_safe_select(fd + 1, &fdread, 0, 0, 0));
}
if (!dataReadable) {
testResult = dataArrived;
} else {
QCoreApplication::processEvents(QEventLoop::ExcludeSocketNotifiers);
testResult = dataArrived;
// to check if the deferred event is processed
QCoreApplication::processEvents();
}
serverSocket->close(); serverSocket->close();
QThread::currentThread()->exit(0); QThread::currentThread()->exit(0);
} }
@ -449,12 +471,16 @@ public:
SocketEventsTester *tester = new SocketEventsTester(); SocketEventsTester *tester = new SocketEventsTester();
if (tester->init()) if (tester->init())
exec(); exec();
dataSent = tester->dataSent;
dataReadable = tester->dataReadable;
testResult = tester->testResult; testResult = tester->testResult;
dataArrived = tester->dataArrived; dataArrived = tester->dataArrived;
delete tester; delete tester;
} }
bool testResult; bool dataSent;
bool dataArrived; bool dataReadable;
bool testResult;
bool dataArrived;
}; };
void tst_QEventLoop::processEventsExcludeSocket() void tst_QEventLoop::processEventsExcludeSocket()
@ -462,9 +488,17 @@ void tst_QEventLoop::processEventsExcludeSocket()
SocketTestThread thread; SocketTestThread thread;
thread.start(); thread.start();
QVERIFY(thread.wait()); QVERIFY(thread.wait());
QVERIFY(thread.dataSent);
QVERIFY(thread.dataReadable);
#if defined(HAVE_GLIB)
QAbstractEventDispatcher *eventDispatcher = QCoreApplication::eventDispatcher();
if (qobject_cast<QEventDispatcherGlib *>(eventDispatcher))
QEXPECT_FAIL("", "ExcludeSocketNotifiers is currently broken in the Glib dispatchers", Continue);
#endif
QVERIFY(!thread.testResult); QVERIFY(!thread.testResult);
QVERIFY(thread.dataArrived); QVERIFY(thread.dataArrived);
} }
#endif
class TimerReceiver : public QObject class TimerReceiver : public QObject
{ {