diff options
Diffstat (limited to 'tests/auto/qsocketnotifier/tst_qsocketnotifier.cpp')
-rw-r--r-- | tests/auto/qsocketnotifier/tst_qsocketnotifier.cpp | 92 |
1 files changed, 83 insertions, 9 deletions
diff --git a/tests/auto/qsocketnotifier/tst_qsocketnotifier.cpp b/tests/auto/qsocketnotifier/tst_qsocketnotifier.cpp index 7f6d932..d4ae159 100644 --- a/tests/auto/qsocketnotifier/tst_qsocketnotifier.cpp +++ b/tests/auto/qsocketnotifier/tst_qsocketnotifier.cpp @@ -57,6 +57,7 @@ public: private slots: void unexpectedDisconnection(); + void mixingWithTimers(); }; tst_QSocketNotifier::tst_QSocketNotifier() @@ -69,9 +70,10 @@ tst_QSocketNotifier::~tst_QSocketNotifier() class UnexpectedDisconnectTester : public QObject { Q_OBJECT + int sequence; + public: QNativeSocketEngine *readEnd1, *readEnd2; - int sequence; UnexpectedDisconnectTester(QNativeSocketEngine *s1, QNativeSocketEngine *s2) : readEnd1(s1), readEnd2(s2), sequence(0) @@ -84,18 +86,25 @@ public: connect(notifier2, SIGNAL(activated(int)), SLOT(handleActivated())); } + const int getSequence() { + return sequence; + } + + void incSequence() { + ++sequence; + } + public slots: void handleActivated() { char data1[1], data2[1]; - ++sequence; - if (sequence == 1) { + incSequence(); + if (getSequence() == 1) { // read from both ends (void) readEnd1->read(data1, sizeof(data1)); (void) readEnd2->read(data2, sizeof(data2)); emit finished(); - } else if (sequence == 2) { - // we should never get here + } else if (getSequence() == 2) { QCOMPARE(readEnd2->read(data2, sizeof(data2)), qint64(-2)); QVERIFY(readEnd2->isValid()); } @@ -155,9 +164,12 @@ void tst_QSocketNotifier::unexpectedDisconnection() writeEnd2->flush(); UnexpectedDisconnectTester tester(&readEnd1, &readEnd2); - QEventLoop eventLoop; - connect(&tester, SIGNAL(finished()), &eventLoop, SLOT(quit())); - eventLoop.exec(); + + do { + // we have to wait until sequence value changes + // as any event can make us jump out processing + QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents); + } while(tester.getSequence() <= 0); QVERIFY(readEnd1.state() == QAbstractSocket::ConnectedState); QVERIFY(readEnd2.state() == QAbstractSocket::ConnectedState); @@ -165,7 +177,7 @@ void tst_QSocketNotifier::unexpectedDisconnection() qWarning("### Windows returns 1 activation, Unix returns 2."); QCOMPARE(tester.sequence, 1); #else - QCOMPARE(tester.sequence, 2); + QCOMPARE(tester.getSequence(), 2); #endif readEnd1.close(); @@ -175,5 +187,67 @@ void tst_QSocketNotifier::unexpectedDisconnection() server.close(); } +class MixingWithTimersHelper : public QObject +{ + Q_OBJECT + +public: + MixingWithTimersHelper(QTimer *timer, QTcpServer *server); + + bool timerActivated; + bool socketActivated; + +private slots: + void timerFired(); + void socketFired(); +}; + +MixingWithTimersHelper::MixingWithTimersHelper(QTimer *timer, QTcpServer *server) +{ + timerActivated = false; + socketActivated = false; + + connect(timer, SIGNAL(timeout()), SLOT(timerFired())); + connect(server, SIGNAL(newConnection()), SLOT(socketFired())); +} + +void MixingWithTimersHelper::timerFired() +{ + timerActivated = true; +} + +void MixingWithTimersHelper::socketFired() +{ + socketActivated = true; +} + +void tst_QSocketNotifier::mixingWithTimers() +{ + QTimer timer; + timer.setInterval(0); + timer.start(); + + QTcpServer server; + QVERIFY(server.listen(QHostAddress::LocalHost, 0)); + + MixingWithTimersHelper helper(&timer, &server); + + QCoreApplication::processEvents(); + + QCOMPARE(helper.timerActivated, true); + QCOMPARE(helper.socketActivated, false); + + helper.timerActivated = false; + helper.socketActivated = false; + + QTcpSocket socket; + socket.connectToHost(server.serverAddress(), server.serverPort()); + + QCoreApplication::processEvents(); + + QCOMPARE(helper.timerActivated, true); + QCOMPARE(helper.socketActivated, true); +} + QTEST_MAIN(tst_QSocketNotifier) #include <tst_qsocketnotifier.moc> |