summaryrefslogtreecommitdiffstats
path: root/tests/auto/concurrent
diff options
context:
space:
mode:
authorVitaly Fanaskov <vitaly.fanaskov@qt.io>2020-03-11 18:07:03 +0100
committerVitaly Fanaskov <vitaly.fanaskov@qt.io>2020-03-29 20:44:32 +0100
commit5a0d4f3313157d2fe48e9d159968bed6883eb5f8 (patch)
treed7366ac16fdbe83ff4e8006577f9987404ae2742 /tests/auto/concurrent
parentd975ad4ed728553b765c61f38c1e0df899187cf5 (diff)
QtConcurrent: add fluent interface to configure a task before run
Task-number: QTBUG-82950 Change-Id: I449da938b6b501a7646b3425edde5c880d6ca87e Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Mikhail Svetkin <mikhail.svetkin@gmail.com>
Diffstat (limited to 'tests/auto/concurrent')
-rw-r--r--tests/auto/concurrent/CMakeLists.txt1
-rw-r--r--tests/auto/concurrent/concurrent.pro3
-rw-r--r--tests/auto/concurrent/qtconcurrenttask/CMakeLists.txt6
-rw-r--r--tests/auto/concurrent/qtconcurrenttask/qtconcurrenttask.pro4
-rw-r--r--tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp160
5 files changed, 173 insertions, 1 deletions
diff --git a/tests/auto/concurrent/CMakeLists.txt b/tests/auto/concurrent/CMakeLists.txt
index ae1e256bbd..2ef8cfd128 100644
--- a/tests/auto/concurrent/CMakeLists.txt
+++ b/tests/auto/concurrent/CMakeLists.txt
@@ -6,3 +6,4 @@ add_subdirectory(qtconcurrentmap)
add_subdirectory(qtconcurrentmedian)
add_subdirectory(qtconcurrentrun)
add_subdirectory(qtconcurrentthreadengine)
+add_subdirectory(qtconcurrenttask)
diff --git a/tests/auto/concurrent/concurrent.pro b/tests/auto/concurrent/concurrent.pro
index e67c51aae3..ad3231ff90 100644
--- a/tests/auto/concurrent/concurrent.pro
+++ b/tests/auto/concurrent/concurrent.pro
@@ -5,5 +5,6 @@ SUBDIRS=\
qtconcurrentmap \
qtconcurrentmedian \
qtconcurrentrun \
- qtconcurrentthreadengine
+ qtconcurrentthreadengine \
+ qtconcurrenttask
diff --git a/tests/auto/concurrent/qtconcurrenttask/CMakeLists.txt b/tests/auto/concurrent/qtconcurrenttask/CMakeLists.txt
new file mode 100644
index 0000000000..d55a0536cf
--- /dev/null
+++ b/tests/auto/concurrent/qtconcurrenttask/CMakeLists.txt
@@ -0,0 +1,6 @@
+add_qt_test(tst_qtconcurrenttask
+ SOURCES
+ tst_qtconcurrenttask.cpp
+ PUBLIC_LIBRARIES
+ Qt::Concurrent
+)
diff --git a/tests/auto/concurrent/qtconcurrenttask/qtconcurrenttask.pro b/tests/auto/concurrent/qtconcurrenttask/qtconcurrenttask.pro
new file mode 100644
index 0000000000..4404efa0b3
--- /dev/null
+++ b/tests/auto/concurrent/qtconcurrenttask/qtconcurrenttask.pro
@@ -0,0 +1,4 @@
+CONFIG += testcase
+TARGET = tst_qtconcurrenttask
+QT = core testlib concurrent
+SOURCES = tst_qtconcurrenttask.cpp
diff --git a/tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp b/tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp
new file mode 100644
index 0000000000..0d08efa463
--- /dev/null
+++ b/tests/auto/concurrent/qtconcurrenttask/tst_qtconcurrenttask.cpp
@@ -0,0 +1,160 @@
+/****************************************************************************
+**
+** 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$
+**
+****************************************************************************/
+
+#include <qtconcurrenttask.h>
+
+#include <QtTest/QtTest>
+
+class tst_QtConcurrentTask : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void taskWithFreeFunction();
+ void taskWithClassMethod();
+ void taskWithCallableObject();
+ void taskWithLambda();
+ void taskWithArguments();
+ void useCustomThreadPool();
+ void setPriority();
+ void adjustAllSettings();
+};
+
+using namespace QtConcurrent;
+
+void tst_QtConcurrentTask::taskWithFreeFunction()
+{
+ QVariant value(42);
+
+ auto result = task(&qvariant_cast<int>)
+ .withArguments(value)
+ .spawn()
+ .result();
+
+ QCOMPARE(result, 42);
+}
+void tst_QtConcurrentTask::taskWithClassMethod()
+{
+ QString result("foobar");
+
+ task(&QString::chop).withArguments(&result, 3).spawn().waitForFinished();
+
+ QCOMPARE(result, "foo");
+}
+void tst_QtConcurrentTask::taskWithCallableObject()
+{
+ QCOMPARE(task(std::plus<int>())
+ .withArguments(40, 2)
+ .spawn()
+ .result(),
+ 42);
+}
+
+void tst_QtConcurrentTask::taskWithLambda()
+{
+ QCOMPARE(task([]{ return 42; }).spawn().result(), 42);
+}
+
+void tst_QtConcurrentTask::taskWithArguments()
+{
+ auto result = task([](int arg1, int arg2){ return arg1 + arg2; })
+ .withArguments(40, 2)
+ .spawn()
+ .result();
+ QCOMPARE(result, 42);
+}
+
+void tst_QtConcurrentTask::useCustomThreadPool()
+{
+ QThreadPool pool;
+
+ int result = 0;
+ task([&]{ result = 42; }).onThreadPool(pool).spawn().waitForFinished();
+
+ QCOMPARE(result, 42);
+}
+
+void tst_QtConcurrentTask::setPriority()
+{
+ QThreadPool pool;
+ pool.setMaxThreadCount(1);
+
+ QSemaphore sem;
+
+ QVector<QFuture<void>> futureResults;
+ futureResults << task([&]{ sem.acquire(); })
+ .onThreadPool(pool)
+ .spawn();
+
+ const int tasksCount = 10;
+ QVector<int> priorities(tasksCount);
+ std::iota(priorities.begin(), priorities.end(), 1);
+ auto seed = std::chrono::system_clock::now().time_since_epoch().count();
+ std::shuffle(priorities.begin(), priorities.end(), std::default_random_engine(seed));
+
+ QVector<int> actual;
+ for (int priority : priorities)
+ futureResults << task([priority, &actual] { actual << priority; })
+ .onThreadPool(pool)
+ .withPriority(priority)
+ .spawn();
+
+ sem.release();
+ pool.waitForDone();
+
+ for (const auto &f : futureResults)
+ QVERIFY(f.isFinished());
+
+ QVector<int> expected(priorities);
+ std::sort(expected.begin(), expected.end(), std::greater<>());
+
+ QCOMPARE(actual, expected);
+}
+
+void tst_QtConcurrentTask::adjustAllSettings()
+{
+ QThreadPool pool;
+ pool.setMaxThreadCount(1);
+
+ const int priority = 10;
+
+ QVector<int> result;
+ auto append = [&](auto &&...args){ (result << ... << args); };
+
+ task(std::move(append))
+ .withArguments(1, 2, 3)
+ .onThreadPool(pool)
+ .withPriority(priority)
+ .spawn()
+ .waitForFinished();
+
+ QCOMPARE(result, QVector<int>({1, 2, 3}));
+}
+
+QTEST_MAIN(tst_QtConcurrentTask)
+#include "tst_qtconcurrenttask.moc"