diff options
Diffstat (limited to 'tests/auto/corelib/thread')
5 files changed, 349 insertions, 35 deletions
diff --git a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp index a546cad225..58bebe19ac 100644 --- a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp @@ -36,6 +36,7 @@ #include <qresultstore.h> #include <qthreadpool.h> #include <qexception.h> +#include <qrandom.h> #include <private/qfutureinterface_p.h> // COM interface macro. @@ -1205,8 +1206,6 @@ void tst_QFuture::pause() Interface.reportFinished(); } -const int resultCount = 1000; - class ResultObject : public QObject { Q_OBJECT @@ -1458,7 +1457,7 @@ void tst_QFuture::nonGlobalThreadPool() void run() Q_DECL_OVERRIDE { - const int ms = 100 + (qrand() % 100 - 100/2); + const int ms = 100 + (QRandomGenerator::global()->bounded(100) - 100/2); QThread::msleep(ms); reportResult(Answer); reportFinished(); diff --git a/tests/auto/corelib/thread/qsemaphore/tst_qsemaphore.cpp b/tests/auto/corelib/thread/qsemaphore/tst_qsemaphore.cpp index ce093f769c..a67ecc2471 100644 --- a/tests/auto/corelib/thread/qsemaphore/tst_qsemaphore.cpp +++ b/tests/auto/corelib/thread/qsemaphore/tst_qsemaphore.cpp @@ -44,6 +44,7 @@ private slots: void tryAcquireWithTimeoutForever_data(); void tryAcquireWithTimeoutForever(); void producerConsumer(); + void raii(); }; static QSemaphore *semaphore = 0; @@ -480,5 +481,54 @@ void tst_QSemaphore::producerConsumer() consumer.wait(); } +void tst_QSemaphore::raii() +{ + QSemaphore sem; + + QCOMPARE(sem.available(), 0); + + // basic operation: + { + QSemaphoreReleaser r0; + const QSemaphoreReleaser r1(sem); + const QSemaphoreReleaser r2(sem, 2); + + QCOMPARE(r0.semaphore(), nullptr); + QCOMPARE(r1.semaphore(), &sem); + QCOMPARE(r2.semaphore(), &sem); + } + + QCOMPARE(sem.available(), 3); + + // cancel: + { + const QSemaphoreReleaser r1(sem); + QSemaphoreReleaser r2(sem, 2); + + QCOMPARE(r2.cancel(), &sem); + QCOMPARE(r2.semaphore(), nullptr); + } + + QCOMPARE(sem.available(), 4); + + // move-assignment: + { + const QSemaphoreReleaser r1(sem); + QSemaphoreReleaser r2(sem, 2); + + QCOMPARE(sem.available(), 4); + + r2 = QSemaphoreReleaser(); + + QCOMPARE(sem.available(), 6); + + r2 = QSemaphoreReleaser(sem, 42); + + QCOMPARE(sem.available(), 6); + } + + QCOMPARE(sem.available(), 49); +} + QTEST_MAIN(tst_QSemaphore) #include "tst_qsemaphore.moc" diff --git a/tests/auto/corelib/thread/qthread/qthread.pro b/tests/auto/corelib/thread/qthread/qthread.pro index e0ef506d2c..37552f1fca 100644 --- a/tests/auto/corelib/thread/qthread/qthread.pro +++ b/tests/auto/corelib/thread/qthread/qthread.pro @@ -2,6 +2,8 @@ CONFIG += testcase TARGET = tst_qthread QT = core testlib SOURCES = tst_qthread.cpp +qtConfig(c++14):CONFIG += c++14 +qtConfig(c++1z):CONFIG += c++1z INCLUDEPATH += ../../../../shared/ HEADERS += ../../../../shared/emulationdetector.h diff --git a/tests/auto/corelib/thread/qthread/tst_qthread.cpp b/tests/auto/corelib/thread/qthread/tst_qthread.cpp index 5c8522a313..0405896ca7 100644 --- a/tests/auto/corelib/thread/qthread/tst_qthread.cpp +++ b/tests/auto/corelib/thread/qthread/tst_qthread.cpp @@ -47,6 +47,10 @@ #endif #endif +#ifndef QT_NO_EXCEPTIONS +#include <exception> +#endif + #include "emulationdetector.h" class tst_QThread : public QObject @@ -100,6 +104,8 @@ private slots: void stressTest(); void quitLock(); + + void create(); }; enum { one_minute = 60 * 1000, five_minutes = 5 * one_minute }; @@ -1330,6 +1336,259 @@ void tst_QThread::quitLock() QVERIFY(exitThreadCalled); } +void tst_QThread::create() +{ +#if !QT_CONFIG(cxx11_future) + QSKIP("This test requires QThread::create"); +#else + { + const auto &function = [](){}; + QScopedPointer<QThread> thread(QThread::create(function)); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + } + + { + // no side effects before starting + int i = 0; + const auto &function = [&i]() { i = 42; }; + QScopedPointer<QThread> thread(QThread::create(function)); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + QCOMPARE(i, 0); + thread->start(); + QVERIFY(thread->wait()); + QCOMPARE(i, 42); + } + + { + // control thread progress + QSemaphore semaphore1; + QSemaphore semaphore2; + + const auto &function = [&semaphore1, &semaphore2]() -> void + { + semaphore1.acquire(); + semaphore2.release(); + }; + + QScopedPointer<QThread> thread(QThread::create(function)); + + QVERIFY(thread); + thread->start(); + QTRY_VERIFY(thread->isRunning()); + semaphore1.release(); + semaphore2.acquire(); + QVERIFY(thread->wait()); + QVERIFY(!thread->isRunning()); + } + + { + // ignore return values + const auto &function = []() { return 42; }; + QScopedPointer<QThread> thread(QThread::create(function)); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + } + + { + // return value of create + QScopedPointer<QThread> thread; + QSemaphore s; + const auto &function = [&thread, &s]() -> void + { + s.acquire(); + QCOMPARE(thread.data(), QThread::currentThread()); + }; + + thread.reset(QThread::create(function)); + QVERIFY(thread); + thread->start(); + QTRY_VERIFY(thread->isRunning()); + s.release(); + QVERIFY(thread->wait()); + } + + { + // move-only parameters + struct MoveOnlyValue { + explicit MoveOnlyValue(int v) : v(v) {} + ~MoveOnlyValue() = default; + MoveOnlyValue(const MoveOnlyValue &) = delete; + MoveOnlyValue(MoveOnlyValue &&) = default; + MoveOnlyValue &operator=(const MoveOnlyValue &) = delete; + MoveOnlyValue &operator=(MoveOnlyValue &&) = default; + int v; + }; + + struct MoveOnlyFunctor { + explicit MoveOnlyFunctor(int *i) : i(i) {} + ~MoveOnlyFunctor() = default; + MoveOnlyFunctor(const MoveOnlyFunctor &) = delete; + MoveOnlyFunctor(MoveOnlyFunctor &&) = default; + MoveOnlyFunctor &operator=(const MoveOnlyFunctor &) = delete; + MoveOnlyFunctor &operator=(MoveOnlyFunctor &&) = default; + int operator()() { return (*i = 42); } + int *i; + }; + + { + int i = 0; + MoveOnlyFunctor f(&i); + QScopedPointer<QThread> thread(QThread::create(std::move(f))); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + QCOMPARE(i, 42); + } + +#if defined(__cpp_init_captures) && __cpp_init_captures >= 201304 + { + int i = 0; + MoveOnlyValue mo(123); + auto moveOnlyFunction = [&i, mo = std::move(mo)]() { i = mo.v; }; + QScopedPointer<QThread> thread(QThread::create(std::move(moveOnlyFunction))); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + QCOMPARE(i, 123); + } +#endif // __cpp_init_captures + +#ifdef QTHREAD_HAS_VARIADIC_CREATE + { + int i = 0; + const auto &function = [&i](MoveOnlyValue &&mo) { i = mo.v; }; + QScopedPointer<QThread> thread(QThread::create(function, MoveOnlyValue(123))); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + QCOMPARE(i, 123); + } + + { + int i = 0; + const auto &function = [&i](MoveOnlyValue &&mo) { i = mo.v; }; + MoveOnlyValue mo(-1); + QScopedPointer<QThread> thread(QThread::create(function, std::move(mo))); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + QCOMPARE(i, -1); + } +#endif // QTHREAD_HAS_VARIADIC_CREATE + } + +#ifdef QTHREAD_HAS_VARIADIC_CREATE + { + // simple parameter passing + int i = 0; + const auto &function = [&i](int j, int k) { i = j * k; }; + QScopedPointer<QThread> thread(QThread::create(function, 3, 4)); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + QCOMPARE(i, 0); + thread->start(); + QVERIFY(thread->wait()); + QCOMPARE(i, 12); + } + + { + // ignore return values (with parameters) + const auto &function = [](double d) { return d * 2.0; }; + QScopedPointer<QThread> thread(QThread::create(function, 3.14)); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + } + + { + // handling of pointers to member functions, std::ref, etc. + struct S { + S() : v(0) {} + void doSomething() { ++v; } + int v; + }; + + S object; + + QCOMPARE(object.v, 0); + + QScopedPointer<QThread> thread; + thread.reset(QThread::create(&S::doSomething, object)); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + + QCOMPARE(object.v, 0); // a copy was passed, this should still be 0 + + thread.reset(QThread::create(&S::doSomething, std::ref(object))); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + + QCOMPARE(object.v, 1); + + thread.reset(QThread::create(&S::doSomething, &object)); + QVERIFY(thread); + QVERIFY(!thread->isRunning()); + thread->start(); + QVERIFY(thread->wait()); + + QCOMPARE(object.v, 2); + } + + { + // std::ref into ordinary reference + int i = 42; + const auto &function = [](int &i) { i *= 2; }; + QScopedPointer<QThread> thread(QThread::create(function, std::ref(i))); + QVERIFY(thread); + thread->start(); + QVERIFY(thread->wait()); + QCOMPARE(i, 84); + } + +#ifndef QT_NO_EXCEPTIONS + { + // exceptions when copying/decaying the arguments are thrown at build side and won't terminate + class ThreadException : public std::exception + { + }; + + struct ThrowWhenCopying + { + ThrowWhenCopying() = default; + ThrowWhenCopying(const ThrowWhenCopying &) + { + throw ThreadException(); + } + ~ThrowWhenCopying() = default; + ThrowWhenCopying &operator=(const ThrowWhenCopying &) = default; + }; + + const auto &function = [](const ThrowWhenCopying &){}; + QScopedPointer<QThread> thread; + ThrowWhenCopying t; + QVERIFY_EXCEPTION_THROWN(thread.reset(QThread::create(function, t)), ThreadException); + QVERIFY(!thread); + } +#endif // QT_NO_EXCEPTIONS +#endif // QTHREAD_HAS_VARIADIC_CREATE +#endif // QT_CONFIG(cxx11_future) +} + class StopableJob : public QObject { Q_OBJECT diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp index 0eaf8a4b77..1092216fb7 100644 --- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp +++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp @@ -92,6 +92,7 @@ private slots: void tryTake(); void waitForDoneTimeout(); void destroyingWaitsForTasksToFinish(); + void stackSize(); void stressTest(); void takeAllAndIncreaseMaxThreadCount(); void waitForDoneAfterTake(); @@ -963,21 +964,6 @@ void tst_QThreadPool::cancel() QSemaphore sem(0); QSemaphore startedThreads(0); - class SemaphoreReleaser - { - QSemaphore &sem; - int n; - Q_DISABLE_COPY(SemaphoreReleaser) - public: - explicit SemaphoreReleaser(QSemaphore &sem, int n) - : sem(sem), n(n) {} - - ~SemaphoreReleaser() - { - sem.release(n); - } - }; - class BlockingRunnable : public QRunnable { public: @@ -1016,7 +1002,7 @@ void tst_QThreadPool::cancel() // ensure that the QThreadPool doesn't deadlock if any of the checks fail // and cause an early return: - const SemaphoreReleaser semReleaser(sem, runs); + const QSemaphoreReleaser semReleaser(sem, runs); count.store(0); QAtomicInt dtorCounter = 0; @@ -1050,21 +1036,6 @@ void tst_QThreadPool::tryTake() QSemaphore sem(0); QSemaphore startedThreads(0); - class SemaphoreReleaser - { - QSemaphore &sem; - int n; - Q_DISABLE_COPY(SemaphoreReleaser) - public: - explicit SemaphoreReleaser(QSemaphore &sem, int n) - : sem(sem), n(n) {} - - ~SemaphoreReleaser() - { - sem.release(n); - } - }; - class BlockingRunnable : public QRunnable { public: @@ -1103,7 +1074,7 @@ void tst_QThreadPool::tryTake() // ensure that the QThreadPool doesn't deadlock if any of the checks fail // and cause an early return: - const SemaphoreReleaser semReleaser(sem, Runs); + const QSemaphoreReleaser semReleaser(sem, Runs); count.store(0); QAtomicInt dtorCounter = 0; @@ -1168,6 +1139,39 @@ void tst_QThreadPool::destroyingWaitsForTasksToFinish() } } +// Verify that QThreadPool::stackSize is used when creating +// new threads. Note that this tests the Qt property only +// since QThread::stackSize() does not reflect the actual +// stack size used by the native thread. +void tst_QThreadPool::stackSize() +{ + uint targetStackSize = 512 * 1024; + uint threadStackSize = 1; // impossible value + + class StackSizeChecker : public QRunnable + { + public: + uint *stackSize; + + StackSizeChecker(uint *stackSize) + :stackSize(stackSize) + { + + } + + void run() + { + *stackSize = QThread::currentThread()->stackSize(); + } + }; + + QThreadPool threadPool; + threadPool.setStackSize(targetStackSize); + threadPool.start(new StackSizeChecker(&threadStackSize)); + QVERIFY(threadPool.waitForDone(30000)); // 30s timeout + QCOMPARE(threadStackSize, targetStackSize); +} + void tst_QThreadPool::stressTest() { class Task : public QRunnable |