diff options
author | Qt Continuous Integration System <qt-info@nokia.com> | 2010-11-25 03:31:56 +0100 |
---|---|---|
committer | Qt Continuous Integration System <qt-info@nokia.com> | 2010-11-25 03:31:56 +0100 |
commit | e868e896054061f35b38dd3e7b688d932be40a55 (patch) | |
tree | 75eb8d69df9bd07f93dd1a75dd7d014486b11bd6 /tests/auto/qthread | |
parent | 7f2877acd0183092c31b74ed0de8d54fb3c49caf (diff) | |
parent | 25c9b6ed488b1446cbdd38186992957264596314 (diff) |
Merge branch 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-2 into master-integration
* 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-2:
QThread: fix a race condition when destroying or restarting thread from finished()
QThreadStorage: fix crash if thread local destructor reset himself
Add WA_MacNoShadow widget attribute
Doc: Fixing typo
Fix compilation on symbian
Fix a namespace error and some warnings found by clang
optimize ligatureHelper by using qBinaryFind instead of the for loop
QFileSystemWatcher: Do not require QApplication in the destructor.
Do not define METHOD if QT_NO_KEYWORD is defined.
QThreadPrivate::finish should not keep mutex locked when calling signals
Make QThreadStorage supports value type and not only pointers.
QThreadStorage: fix memory leak if thread storage are added while destroying
Compile fix.
Fix some warnings on Mac
Diffstat (limited to 'tests/auto/qthread')
-rw-r--r-- | tests/auto/qthread/tst_qthread.cpp | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/tests/auto/qthread/tst_qthread.cpp b/tests/auto/qthread/tst_qthread.cpp index f290a2b0bd..01f080fcff 100644 --- a/tests/auto/qthread/tst_qthread.cpp +++ b/tests/auto/qthread/tst_qthread.cpp @@ -107,6 +107,10 @@ private slots: void QTBUG13810_exitAndStart(); void connectThreadFinishedSignalToObjectDeleteLaterSlot(); + void wait2(); + void wait3_slowDestructor(); + void destroyFinishRace(); + void startFinishRace(); void stressTest(); }; @@ -976,6 +980,7 @@ void tst_QThread::QTBUG13810_exitAndStart() QCOMPARE(sync1.m_prop, 89); } + void tst_QThread::connectThreadFinishedSignalToObjectDeleteLaterSlot() { QThread thread; @@ -990,5 +995,123 @@ void tst_QThread::connectThreadFinishedSignalToObjectDeleteLaterSlot() QVERIFY(p.isNull()); } +class Waiting_Thread : public QThread +{ +public: + enum { WaitTime = 800 }; + QMutex mutex; + QWaitCondition cond1; + QWaitCondition cond2; + + void run() + { + QMutexLocker locker(&mutex); + cond1.wait(&mutex); + cond2.wait(&mutex, WaitTime); + } +}; + +void tst_QThread::wait2() +{ + QElapsedTimer timer; + Waiting_Thread thread; + thread.start(); + timer.start(); + QVERIFY(!thread.wait(Waiting_Thread::WaitTime)); + qint64 elapsed = timer.elapsed(); + + QVERIFY(elapsed >= Waiting_Thread::WaitTime); + //QVERIFY(elapsed < Waiting_Thread::WaitTime * 1.4); + + timer.start(); + thread.cond1.wakeOne(); + QVERIFY(thread.wait(/*Waiting_Thread::WaitTime * 1.4*/)); + elapsed = timer.elapsed(); + QVERIFY(elapsed >= Waiting_Thread::WaitTime); + //QVERIFY(elapsed < Waiting_Thread::WaitTime * 1.4); +} + + +class SlowSlotObject : public QObject { + Q_OBJECT +public: + QMutex mutex; + QWaitCondition cond; +public slots: + void slowSlot() { + QMutexLocker locker(&mutex); + cond.wait(&mutex); + } +}; + +void tst_QThread::wait3_slowDestructor() +{ + SlowSlotObject slow; + QThread thread; + QObject::connect(&thread, SIGNAL(finished()), &slow, SLOT(slowSlot()), Qt::DirectConnection); + + enum { WaitTime = 1800 }; + QElapsedTimer timer; + + thread.start(); + thread.quit(); + //the quit function will cause the thread to finish and enter the slowSlot that is blocking + + timer.start(); + QVERIFY(!thread.wait(Waiting_Thread::WaitTime)); + qint64 elapsed = timer.elapsed(); + + QVERIFY(elapsed >= Waiting_Thread::WaitTime); + //QVERIFY(elapsed < Waiting_Thread::WaitTime * 1.4); + + slow.cond.wakeOne(); + //now the thread should finish quickly + QVERIFY(thread.wait(one_minute)); +} + +void tst_QThread::destroyFinishRace() +{ + class Thread : public QThread { void run() {} }; + for (int i = 0; i < 15; i++) { + Thread *thr = new Thread; + connect(thr, SIGNAL(finished()), thr, SLOT(deleteLater())); + QWeakPointer<QThread> weak(static_cast<QThread*>(thr)); + thr->start(); + while (weak) { + qApp->processEvents(); + qApp->processEvents(); + qApp->processEvents(); + qApp->processEvents(); + } + } +} + +void tst_QThread::startFinishRace() +{ + class Thread : public QThread { + public: + Thread() : i (50) {} + void run() { + i--; + if (!i) disconnect(this, SIGNAL(finished()), 0, 0); + } + int i; + }; + for (int i = 0; i < 15; i++) { + Thread thr; + connect(&thr, SIGNAL(finished()), &thr, SLOT(start())); + thr.start(); + while (!thr.isFinished() || thr.i != 0) { + qApp->processEvents(); + qApp->processEvents(); + qApp->processEvents(); + qApp->processEvents(); + } + QCOMPARE(thr.i, 0); + } +} + + + QTEST_MAIN(tst_QThread) #include "tst_qthread.moc" |