diff options
author | Stephen Kelly <stephen.kelly@kdab.com> | 2011-10-26 13:29:51 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-31 23:22:15 +0100 |
commit | a512e210ac5b032c5fc2edf1ddf72e5a414485fd (patch) | |
tree | 0e9615f317fe5c24bd002155b205a01d5716a1d3 /tests/auto/corelib/kernel/qeventloop | |
parent | 36a590c90ddab6fa438b7b54b05d89e8f9698a52 (diff) |
Add the event loop quitlock feature to QtCore.
A feature of a ref-counted quit (managed by a quit-lock class)
is added to both QEventLoop and QCoreApplication.
This allows, for example, an event loop to quit() when there is
no more work for it to do.
quitOnLastWindowClosed is implemented in terms of the refcount in
QCoreApplication so that jobs can be completed before the
application quits.
Change-Id: I14c8f4e7ee12bbf81a6e5849290d4c8ff37fa110
Reviewed-by: David Faure <faure@kde.org>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'tests/auto/corelib/kernel/qeventloop')
-rw-r--r-- | tests/auto/corelib/kernel/qeventloop/qeventloop.pro | 2 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp | 82 |
2 files changed, 83 insertions, 1 deletions
diff --git a/tests/auto/corelib/kernel/qeventloop/qeventloop.pro b/tests/auto/corelib/kernel/qeventloop/qeventloop.pro index d21a7d64f6..adfc810788 100644 --- a/tests/auto/corelib/kernel/qeventloop/qeventloop.pro +++ b/tests/auto/corelib/kernel/qeventloop/qeventloop.pro @@ -1,6 +1,6 @@ CONFIG += testcase TARGET = tst_qeventloop -QT = core network testlib +QT = core network testlib core-private SOURCES = tst_qeventloop.cpp win32:!wince*:LIBS += -luser32 diff --git a/tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp b/tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp index 897cba1f17..e2144134d9 100644 --- a/tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp +++ b/tests/auto/corelib/kernel/qeventloop/tst_qeventloop.cpp @@ -45,6 +45,7 @@ #include <qcoreapplication.h> #include <qcoreevent.h> #include <qeventloop.h> +#include <private/qeventloop_p.h> #include <qmutex.h> #include <qthread.h> #include <qtimer.h> @@ -194,6 +195,8 @@ private slots: // keep this test last: void nestedLoops(); + void testQuitLock(); + protected: void customEvent(QEvent *e); }; @@ -640,6 +643,85 @@ void tst_QEventLoop::deliverInDefinedOrder() } +class JobObject : public QObject +{ + Q_OBJECT +public: + + explicit JobObject(QEventLoop *loop, QObject *parent = 0) + : QObject(parent), loop(loop), locker(loop) + { + } + + explicit JobObject(QObject *parent = 0) + : QObject(parent) + { + } + +public slots: + void start(int timeout = 200) + { + QTimer::singleShot(timeout, this, SLOT(timeout())); + } + +private slots: + void timeout() + { + emit done(); + deleteLater(); + } + +signals: + void done(); + +private: + QEventLoop *loop; + QEventLoopLocker locker; +}; + +void tst_QEventLoop::testQuitLock() +{ + QEventLoop eventLoop; + + QTimer timer; + timer.setInterval(100); + QSignalSpy timerSpy(&timer, SIGNAL(timeout())); + timer.start(); + + QEventLoopPrivate* privateClass = static_cast<QEventLoopPrivate*>(QObjectPrivate::get(&eventLoop)); + + QCOMPARE(privateClass->quitLockRef.load(), 0); + + JobObject *job1 = new JobObject(&eventLoop, this); + job1->start(500); + + QCOMPARE(privateClass->quitLockRef.load(), 1); + + eventLoop.exec(); + + QCOMPARE(privateClass->quitLockRef.load(), 0); + + // The job takes long enough that the timer times out several times. + QVERIFY(timerSpy.count() > 3); + timerSpy.clear(); + + job1 = new JobObject(&eventLoop, this); + job1->start(200); + + JobObject *previousJob = job1; + for (int i = 0; i < 9; ++i) { + JobObject *subJob = new JobObject(&eventLoop, this); + connect(previousJob, SIGNAL(done()), subJob, SLOT(start())); + previousJob = subJob; + } + + eventLoop.exec(); + + qDebug() << timerSpy.count(); + // The timer times out more if it has more subjobs to do. + // We run 10 jobs in sequence here of about 200ms each. + QVERIFY(timerSpy.count() > 17); +} QTEST_MAIN(tst_QEventLoop) #include "tst_qeventloop.moc" |