Speed up tst_qcoreapplication

It was emitting signals from a thread for 15 seconds.
Doing this 10 times should be enough, and way faster.
Also a race made it sometimes wait 15 seconds while nothing was happening,
and then it would still succeed; the new code prevents this from happening.

Change-Id: Ib36785dd8090047c760ddcca44fc805efaef1bd8
Merge-request: 4
Reviewed-by: Olivier Goffart <olivier.goffart@nokia.com>
Reviewed-on: http://codereview.qt.nokia.com/1989
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
This commit is contained in:
David Faure 2011-07-21 16:56:55 +02:00 committed by Qt by Nokia
parent cfbdb4cc8a
commit 74a6fe79d9

View File

@ -361,26 +361,25 @@ class DeliverInDefinedOrderObject : public QObject
QPointer<QThread> thread; QPointer<QThread> thread;
int count; int count;
int startCount;
int loopLevel;
public: public:
DeliverInDefinedOrderObject(QObject *parent) DeliverInDefinedOrderObject(QObject *parent)
: QObject(parent), thread(0), count(0) : QObject(parent), thread(0), count(0), startCount(0), loopLevel(0)
{ } { }
~DeliverInDefinedOrderObject()
{ signals:
if (!thread.isNull()) void done();
thread->wait();
}
public slots: public slots:
void start() void startThread()
{ {
QVERIFY(!thread); QVERIFY(!thread);
thread = new DeliverInDefinedOrderThread(); thread = new DeliverInDefinedOrderThread();
connect(thread, SIGNAL(progress(int)), this, SLOT(threadProgress(int))); connect(thread, SIGNAL(progress(int)), this, SLOT(threadProgress(int)));
connect(thread, SIGNAL(finished()), this, SLOT(threadFinished())); connect(thread, SIGNAL(finished()), this, SLOT(threadFinished()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(thread, SIGNAL(destroyed()), this, SLOT(threadDestroyed()));
connect(thread, SIGNAL(destroyed()), this, SLOT(start()));
thread->start(); thread->start();
QCoreApplication::postEvent(this, new QEvent(QEvent::MaxUser), -1); QCoreApplication::postEvent(this, new QEvent(QEvent::MaxUser), -1);
@ -398,21 +397,34 @@ public slots:
{ {
QVERIFY(count == 7); QVERIFY(count == 7);
count = 0; count = 0;
thread->deleteLater();
QCoreApplication::postEvent(this, new QEvent(QEvent::MaxUser), -1); QCoreApplication::postEvent(this, new QEvent(QEvent::MaxUser), -1);
} }
void threadDestroyed()
{
if (++startCount < 20)
startThread();
else
emit done();
}
public: public:
bool event(QEvent *event) bool event(QEvent *event)
{ {
switch (event->type()) { switch (event->type()) {
case QEvent::User: case QEvent::User:
{ {
(void) QEventLoop().exec(); ++loopLevel;
break; if (loopLevel == 2) {
// Ready. Starts a thread that emits (queued) signals, which should be handled in order
startThread();
} }
case QEvent::User + 1: QCoreApplication::postEvent(this, new QEvent(QEvent::MaxUser), -1);
(void) QEventLoop().exec();
break; break;
}
default: default:
break; break;
} }
@ -430,11 +442,8 @@ void tst_QCoreApplication::deliverInDefinedOrder()
// causes sendPostedEvents() to recurse twice // causes sendPostedEvents() to recurse twice
QCoreApplication::postEvent(&obj, new QEvent(QEvent::User)); QCoreApplication::postEvent(&obj, new QEvent(QEvent::User));
QCoreApplication::postEvent(&obj, new QEvent(QEvent::User)); QCoreApplication::postEvent(&obj, new QEvent(QEvent::User));
// starts a thread that emits (queued) signals, which should be handled in order
obj.start();
// run for 15 seconds QObject::connect(&obj, SIGNAL(done()), &app, SLOT(quit()));
QTimer::singleShot(15000, &app, SLOT(quit()));
app.exec(); app.exec();
} }
#endif // QT_NO_QTHREAD #endif // QT_NO_QTHREAD
@ -524,7 +533,7 @@ void tst_QCoreApplication::processEventsAlwaysSendsPostedEvents()
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QCOMPARE(object.counter, i); QCOMPARE(object.counter, i);
++i; ++i;
} while (t.elapsed() < 3000); } while (t.elapsed() < 1000);
} }
void tst_QCoreApplication::reexec() void tst_QCoreApplication::reexec()