summaryrefslogtreecommitdiffstats
path: root/tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp')
-rw-r--r--tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp91
1 files changed, 56 insertions, 35 deletions
diff --git a/tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp b/tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp
index f4e6f99660..d570b0f974 100644
--- a/tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp
+++ b/tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp
@@ -1,34 +1,12 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2020 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <qtconcurrenttask.h>
-#include <QtTest/QtTest>
+#include <QTest>
+#include <QSemaphore>
+
+#include <random>
class tst_QtConcurrentTask : public QObject
{
@@ -41,9 +19,11 @@ private Q_SLOTS:
void taskWithLambda();
void taskWithArguments();
void useCustomThreadPool();
+ void setPriority_data();
void setPriority();
void adjustAllSettings();
void ignoreFutureResult();
+ void withPromise();
};
using namespace QtConcurrent;
@@ -52,13 +32,14 @@ void tst_QtConcurrentTask::taskWithFreeFunction()
{
QVariant value(42);
- auto result = task(&qvariant_cast<int>)
+ auto result = task([](const QVariant &var){ return qvariant_cast<int>(var); })
.withArguments(value)
.spawn()
.result();
QCOMPARE(result, 42);
}
+
void tst_QtConcurrentTask::taskWithClassMethod()
{
QString result("foobar");
@@ -67,6 +48,7 @@ void tst_QtConcurrentTask::taskWithClassMethod()
QCOMPARE(result, "foo");
}
+
void tst_QtConcurrentTask::taskWithCallableObject()
{
QCOMPARE(task(std::plus<int>())
@@ -100,8 +82,18 @@ void tst_QtConcurrentTask::useCustomThreadPool()
QCOMPARE(result, 42);
}
+void tst_QtConcurrentTask::setPriority_data()
+{
+ QTest::addColumn<bool>("runWithPromise");
+
+ QTest::addRow("without promise") << false;
+ QTest::addRow("with promise") << true;
+}
+
void tst_QtConcurrentTask::setPriority()
{
+ QFETCH(bool, runWithPromise);
+
QThreadPool pool;
pool.setMaxThreadCount(1);
@@ -115,15 +107,25 @@ void tst_QtConcurrentTask::setPriority()
const int tasksCount = 10;
QList<int> priorities(tasksCount);
std::iota(priorities.begin(), priorities.end(), 1);
- auto seed = std::chrono::system_clock::now().time_since_epoch().count();
+ auto seed = std::random_device {}();
std::shuffle(priorities.begin(), priorities.end(), std::default_random_engine(seed));
+ qDebug() << "Generated priorities list" << priorities << "using seed" << seed;
+
QList<int> actual;
- for (int priority : priorities)
- futureResults << task([priority, &actual] { actual << priority; })
- .onThreadPool(pool)
- .withPriority(priority)
- .spawn();
+ for (int priority : priorities) {
+ if (runWithPromise) {
+ futureResults << task([priority, &actual] (QPromise<void> &) { actual << priority; })
+ .onThreadPool(pool)
+ .withPriority(priority)
+ .spawn();
+ } else {
+ futureResults << task([priority, &actual] { actual << priority; })
+ .onThreadPool(pool)
+ .withPriority(priority)
+ .spawn();
+ }
+ }
sem.release();
pool.waitForDone();
@@ -156,6 +158,7 @@ void tst_QtConcurrentTask::adjustAllSettings()
QCOMPARE(result, QList<int>({ 1, 2, 3 }));
}
+
void tst_QtConcurrentTask::ignoreFutureResult()
{
QThreadPool pool;
@@ -171,5 +174,23 @@ void tst_QtConcurrentTask::ignoreFutureResult()
QCOMPARE(value, 10);
}
+void incrementWithPromise(QPromise<int> &promise, int i)
+{
+ promise.addResult(i + 1);
+}
+
+void return0WithPromise(QPromise<int> &promise)
+{
+ promise.addResult(0);
+}
+
+void tst_QtConcurrentTask::withPromise()
+{
+ QCOMPARE(task(&return0WithPromise).spawn().result(), 0);
+ QCOMPARE(task(&return0WithPromise).withPriority(7).spawn().result(), 0);
+ QCOMPARE(task(&incrementWithPromise).withArguments(1).spawn().result(), 2);
+ QCOMPARE(task(&incrementWithPromise).withArguments(1).withPriority(7).spawn().result(), 2);
+}
+
QTEST_MAIN(tst_QtConcurrentTask)
#include "tst_qtconcurrenttask.moc"