summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorVitaly Fanaskov <vitaly.fanaskov@qt.io>2020-02-26 15:22:40 +0100
committerVitaly Fanaskov <vitaly.fanaskov@qt.io>2020-03-11 14:46:25 +0100
commitc977e74afd18afff8729070f631e6b7a3f2887f5 (patch)
tree08be91368aaee6391e1fcb71f67f90708f7b9e7a /tests
parent01bacdf7abb071198d843acdfb22ce1701766be8 (diff)
QtConcurrent::run: accept more then five function's arguments
[ChangeLog][Potentially Source-Incompatible Changes] QtConcurrent::run has the following signatures: run(Function &&f, Args &&...args) and run(QThreadPool *pool, Function &&f, Args &&...args). If f is a member pointer, the first argument of args should be an object for which that member is defined (or a reference, or a pointer to it). See the documentation for more details. Fixes: QTBUG-82383 Change-Id: I18f7fcfb2adbdd9f75b29c346bd3516304e32d31 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io> Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp178
-rw-r--r--tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp14
-rw-r--r--tests/auto/corelib/io/qurl/tst_qurl.cpp2
-rw-r--r--tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp16
4 files changed, 120 insertions, 90 deletions
diff --git a/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp b/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp
index a4eb2936b5..c0782d8483 100644
--- a/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp
+++ b/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp
@@ -50,6 +50,7 @@ private slots:
#endif
void functor();
void lambda();
+ void callableObjectWithState();
};
void light()
@@ -152,24 +153,24 @@ void tst_QtConcurrentRun::returnValue()
QCOMPARE(f.result(), 10);
A a;
- f = run(&a, &A::member0);
+ f = run(&A::member0, &a);
QCOMPARE(f.result(), 10);
- f = run(&pool, &a, &A::member0);
+ f = run(&pool, &A::member0, &a);
QCOMPARE(f.result(), 10);
- f = run(&a, &A::member1, 20);
+ f = run(&A::member1, &a, 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, &a, &A::member1, 20);
+ f = run(&pool, &A::member1, &a, 20);
QCOMPARE(f.result(), 20);
- f = run(a, &A::member0);
+ f = run(&A::member0, a);
QCOMPARE(f.result(), 10);
- f = run(&pool, a, &A::member0);
+ f = run(&pool, &A::member0, a);
QCOMPARE(f.result(), 10);
- f = run(a, &A::member1, 20);
+ f = run(&A::member1, a, 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, a, &A::member1, 20);
+ f = run(&pool, &A::member1, a, 20);
QCOMPARE(f.result(), 20);
f = run(a);
@@ -177,9 +178,9 @@ void tst_QtConcurrentRun::returnValue()
f = run(&pool, a);
QCOMPARE(f.result(), 10);
- f = run(&a);
+ f = run(a);
QCOMPARE(f.result(), 10);
- f = run(&pool, &a);
+ f = run(&pool, std::ref(a));
QCOMPARE(f.result(), 10);
f = run(a, 20);
@@ -187,30 +188,30 @@ void tst_QtConcurrentRun::returnValue()
f = run(&pool, a, 20);
QCOMPARE(f.result(), 20);
- f = run(&a, 20);
+ f = run(std::ref(a), 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, &a, 20);
+ f = run(&pool, std::ref(a), 20);
QCOMPARE(f.result(), 20);
const AConst aConst = AConst();
- f = run(&aConst, &AConst::member0);
+ f = run(&AConst::member0, &aConst);
QCOMPARE(f.result(), 10);
- f = run(&pool, &aConst, &AConst::member0);
+ f = run(&pool, &AConst::member0, &aConst);
QCOMPARE(f.result(), 10);
- f = run(&aConst, &AConst::member1, 20);
+ f = run(&AConst::member1, &aConst, 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, &aConst, &AConst::member1, 20);
+ f = run(&pool, &AConst::member1, &aConst, 20);
QCOMPARE(f.result(), 20);
- f = run(aConst, &AConst::member0);
+ f = run(&AConst::member0, aConst);
QCOMPARE(f.result(), 10);
- f = run(&pool, aConst, &AConst::member0);
+ f = run(&pool, &AConst::member0, aConst);
QCOMPARE(f.result(), 10);
- f = run(aConst, &AConst::member1, 20);
+ f = run(&AConst::member1, aConst, 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, aConst, &AConst::member1, 20);
+ f = run(&pool, &AConst::member1, aConst, 20);
QCOMPARE(f.result(), 20);
f = run(aConst);
@@ -218,9 +219,9 @@ void tst_QtConcurrentRun::returnValue()
f = run(&pool, aConst);
QCOMPARE(f.result(), 10);
- f = run(&aConst);
+ f = run(std::ref(a));
QCOMPARE(f.result(), 10);
- f = run(&pool, &aConst);
+ f = run(&pool, std::ref(a));
QCOMPARE(f.result(), 10);
f = run(aConst, 20);
@@ -228,30 +229,30 @@ void tst_QtConcurrentRun::returnValue()
f = run(&pool, aConst, 20);
QCOMPARE(f.result(), 20);
- f = run(&aConst, 20);
+ f = run(std::ref(aConst), 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, &aConst, 20);
+ f = run(&pool, std::ref(aConst), 20);
QCOMPARE(f.result(), 20);
ANoExcept aNoExcept;
- f = run(&aNoExcept, &ANoExcept::member0);
+ f = run(&ANoExcept::member0, &aNoExcept);
QCOMPARE(f.result(), 10);
- f = run(&pool, &aNoExcept, &ANoExcept::member0);
+ f = run(&pool, &ANoExcept::member0, &aNoExcept);
QCOMPARE(f.result(), 10);
- f = run(&aNoExcept, &ANoExcept::member1, 20);
+ f = run(&ANoExcept::member1, &aNoExcept, 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, &aNoExcept, &ANoExcept::member1, 20);
+ f = run(&pool, &ANoExcept::member1, &aNoExcept, 20);
QCOMPARE(f.result(), 20);
- f = run(aNoExcept, &ANoExcept::member0);
+ f = run(&ANoExcept::member0, aNoExcept);
QCOMPARE(f.result(), 10);
- f = run(&pool, aNoExcept, &ANoExcept::member0);
+ f = run(&pool, &ANoExcept::member0, aNoExcept);
QCOMPARE(f.result(), 10);
- f = run(aNoExcept, &ANoExcept::member1, 20);
+ f = run(&ANoExcept::member1, aNoExcept, 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, aNoExcept, &ANoExcept::member1, 20);
+ f = run(&pool, &ANoExcept::member1, aNoExcept, 20);
QCOMPARE(f.result(), 20);
f = run(aNoExcept);
@@ -259,9 +260,9 @@ void tst_QtConcurrentRun::returnValue()
f = run(&pool, aNoExcept);
QCOMPARE(f.result(), 10);
- f = run(&aNoExcept);
+ f = run(std::ref(aNoExcept));
QCOMPARE(f.result(), 10);
- f = run(&pool, &aNoExcept);
+ f = run(&pool, std::ref(aNoExcept));
QCOMPARE(f.result(), 10);
f = run(aNoExcept, 20);
@@ -269,30 +270,30 @@ void tst_QtConcurrentRun::returnValue()
f = run(&pool, aNoExcept, 20);
QCOMPARE(f.result(), 20);
- f = run(&aNoExcept, 20);
+ f = run(std::ref(aNoExcept), 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, &aNoExcept, 20);
+ f = run(&pool, std::ref(aNoExcept), 20);
QCOMPARE(f.result(), 20);
const AConstNoExcept aConstNoExcept = AConstNoExcept();
- f = run(&aConstNoExcept, &AConstNoExcept::member0);
+ f = run(&AConstNoExcept::member0, &aConstNoExcept);
QCOMPARE(f.result(), 10);
- f = run(&pool, &aConstNoExcept, &AConstNoExcept::member0);
+ f = run(&pool, &AConstNoExcept::member0, &aConstNoExcept);
QCOMPARE(f.result(), 10);
- f = run(&aConstNoExcept, &AConstNoExcept::member1, 20);
+ f = run(&AConstNoExcept::member1, &aConstNoExcept, 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, &aConstNoExcept, &AConstNoExcept::member1, 20);
+ f = run(&pool, &AConstNoExcept::member1, &aConstNoExcept, 20);
QCOMPARE(f.result(), 20);
- f = run(aConstNoExcept, &AConstNoExcept::member0);
+ f = run(&AConstNoExcept::member0, aConstNoExcept);
QCOMPARE(f.result(), 10);
- f = run(&pool, aConstNoExcept, &AConstNoExcept::member0);
+ f = run(&pool, &AConstNoExcept::member0, aConstNoExcept);
QCOMPARE(f.result(), 10);
- f = run(aConstNoExcept, &AConstNoExcept::member1, 20);
+ f = run(&AConstNoExcept::member1, aConstNoExcept, 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, aConstNoExcept, &AConstNoExcept::member1, 20);
+ f = run(&pool, &AConstNoExcept::member1, aConstNoExcept, 20);
QCOMPARE(f.result(), 20);
f = run(aConstNoExcept);
@@ -300,9 +301,9 @@ void tst_QtConcurrentRun::returnValue()
f = run(&pool, aConstNoExcept);
QCOMPARE(f.result(), 10);
- f = run(&aConstNoExcept);
+ f = run(std::ref(aConstNoExcept));
QCOMPARE(f.result(), 10);
- f = run(&pool, &aConstNoExcept);
+ f = run(&pool, std::ref(aConstNoExcept));
QCOMPARE(f.result(), 10);
f = run(aConstNoExcept, 20);
@@ -310,9 +311,9 @@ void tst_QtConcurrentRun::returnValue()
f = run(&pool, aConstNoExcept, 20);
QCOMPARE(f.result(), 20);
- f = run(&aConstNoExcept, 20);
+ f = run(std::ref(aConstNoExcept), 20);
QCOMPARE(f.result(), 20);
- f = run(&pool, &aConstNoExcept, 20);
+ f = run(&pool, std::ref(aConstNoExcept), 20);
QCOMPARE(f.result(), 20);
}
@@ -341,25 +342,25 @@ void tst_QtConcurrentRun::functionObject()
TestClass c;
f = run(c);
- f = run(&c);
+ f = run(std::ref(c));
f = run(c, 10);
- f = run(&c, 10);
+ f = run(std::ref(c), 10);
f = run(&pool, c);
- f = run(&pool, &c);
+ f = run(&pool, std::ref(c));
f = run(&pool, c, 10);
- f = run(&pool, &c, 10);
+ f = run(&pool, std::ref(c), 10);
const TestConstClass cc = TestConstClass();
f = run(cc);
- f = run(&cc);
+ f = run(std::ref(c));
f = run(cc, 10);
- f = run(&cc, 10);
+ f = run(std::ref(c), 10);
f = run(&pool, cc);
- f = run(&pool, &cc);
+ f = run(&pool, std::ref(c));
f = run(&pool, cc, 10);
- f = run(&pool, &cc, 10);
+ f = run(&pool, std::ref(c), 10);
}
@@ -369,26 +370,26 @@ void tst_QtConcurrentRun::memberFunctions()
TestClass c;
- run(c, &TestClass::foo).waitForFinished();
- run(&c, &TestClass::foo).waitForFinished();
- run(c, &TestClass::fooInt, 10).waitForFinished();
- run(&c, &TestClass::fooInt, 10).waitForFinished();
+ run(&TestClass::foo, c).waitForFinished();
+ run(&TestClass::foo, &c).waitForFinished();
+ run(&TestClass::fooInt, c, 10).waitForFinished();
+ run(&TestClass::fooInt, &c, 10).waitForFinished();
- run(&pool, c, &TestClass::foo).waitForFinished();
- run(&pool, &c, &TestClass::foo).waitForFinished();
- run(&pool, c, &TestClass::fooInt, 10).waitForFinished();
- run(&pool, &c, &TestClass::fooInt, 10).waitForFinished();
+ run(&pool, &TestClass::foo, c).waitForFinished();
+ run(&pool, &TestClass::foo, &c).waitForFinished();
+ run(&pool, &TestClass::fooInt, c, 10).waitForFinished();
+ run(&pool, &TestClass::fooInt, &c, 10).waitForFinished();
const TestConstClass cc = TestConstClass();
- run(cc, &TestConstClass::foo).waitForFinished();
- run(&cc, &TestConstClass::foo).waitForFinished();
- run(cc, &TestConstClass::fooInt, 10).waitForFinished();
- run(&cc, &TestConstClass::fooInt, 10).waitForFinished();
+ run(&TestConstClass::foo, cc).waitForFinished();
+ run(&TestConstClass::foo, &cc).waitForFinished();
+ run(&TestConstClass::fooInt, cc, 10).waitForFinished();
+ run(&TestConstClass::fooInt, &cc, 10).waitForFinished();
- run(&pool, cc, &TestConstClass::foo).waitForFinished();
- run(&pool, &cc, &TestConstClass::foo).waitForFinished();
- run(&pool, cc, &TestConstClass::fooInt, 10).waitForFinished();
- run(&pool, &cc, &TestConstClass::fooInt, 10).waitForFinished();
+ run(&pool, &TestConstClass::foo, cc).waitForFinished();
+ run(&pool, &TestConstClass::foo, &cc).waitForFinished();
+ run(&pool, &TestConstClass::fooInt, cc, 10).waitForFinished();
+ run(&pool, &TestConstClass::fooInt, &cc, 10).waitForFinished();
}
@@ -437,8 +438,8 @@ void tst_QtConcurrentRun::implicitConvertibleTypes()
run(stringConstRefFunction, QLatin1String("Foo")).waitForFinished();
run(&pool, stringConstRefFunction, QLatin1String("Foo")).waitForFinished();
QString string;
- run(stringRefFunction, string).waitForFinished();
- run(&pool, stringRefFunction, string).waitForFinished();
+ run(stringRefFunction, std::ref(string)).waitForFinished();
+ run(&pool, stringRefFunction, std::ref(string)).waitForFinished();
}
void fn() { }
@@ -732,5 +733,34 @@ void tst_QtConcurrentRun::lambda()
}
}
+struct CallableWithState
+{
+ void setNewState(int newState) { state = newState; }
+ int operator()(int newState) { return (state = newState); }
+
+ static constexpr int defaultState() { return 42; }
+ int state = defaultState();
+};
+
+void tst_QtConcurrentRun::callableObjectWithState()
+{
+ CallableWithState o;
+
+ // Run method setNewState explicitly
+ run(&CallableWithState::setNewState, &o, CallableWithState::defaultState() + 1).waitForFinished();
+ QCOMPARE(o.state, CallableWithState::defaultState() + 1);
+
+ // Run operator()(int) explicitly
+ run(std::ref(o), CallableWithState::defaultState() + 2).waitForFinished();
+ QCOMPARE(o.state, CallableWithState::defaultState() + 2);
+
+ // Run on a copy of object (original object remains unchanged)
+ run(o, CallableWithState::defaultState() + 3).waitForFinished();
+ QCOMPARE(o.state, CallableWithState::defaultState() + 2);
+
+ // Explicitly run on a temporary object
+ QCOMPARE(run(CallableWithState(), 15).result(), 15);
+}
+
QTEST_MAIN(tst_QtConcurrentRun)
#include "tst_qtconcurrentrun.moc"
diff --git a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
index 4969e417f4..1d79d0dd1f 100644
--- a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
+++ b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
@@ -163,13 +163,13 @@ void tst_QLockFile::lockOutOtherThread()
QVERIFY(lockFile.lock());
// Other thread can't acquire lock
- QFuture<QLockFile::LockError> ret = QtConcurrent::run<QLockFile::LockError>(tryLockFromThread, fileName);
+ auto ret = QtConcurrent::run(tryLockFromThread, fileName);
QCOMPARE(ret.result(), QLockFile::LockFailedError);
lockFile.unlock();
// Now other thread can acquire lock
- QFuture<QLockFile::LockError> ret2 = QtConcurrent::run<QLockFile::LockError>(tryLockFromThread, fileName);
+ auto ret2 = QtConcurrent::run(tryLockFromThread, fileName);
QCOMPARE(ret2.result(), QLockFile::NoError);
}
@@ -186,13 +186,13 @@ static QLockFile::LockError lockFromThread(const QString &fileName)
void tst_QLockFile::raceWithOtherThread()
{
const QString fileName = dir.path() + "/raceWithOtherThread";
- QFuture<QLockFile::LockError> ret = QtConcurrent::run<QLockFile::LockError>(lockFromThread, fileName);
- QFuture<QLockFile::LockError> ret2 = QtConcurrent::run<QLockFile::LockError>(lockFromThread, fileName);
+ auto ret = QtConcurrent::run(lockFromThread, fileName);
+ auto ret2 = QtConcurrent::run(lockFromThread, fileName);
QCOMPARE(ret.result(), QLockFile::NoError);
QCOMPARE(ret2.result(), QLockFile::NoError);
}
-static bool lockFromThread(const QString &fileName, int sleepMs, QSemaphore *semThreadReady, QSemaphore *semMainThreadDone)
+static bool lockFromThreadAndWait(const QString &fileName, int sleepMs, QSemaphore *semThreadReady, QSemaphore *semMainThreadDone)
{
QLockFile lockFile(fileName);
if (!lockFile.lock()) {
@@ -233,7 +233,7 @@ void tst_QLockFile::waitForLock()
QLockFile lockFile(fileName);
QSemaphore semThreadReady, semMainThreadDone;
// Lock file from a thread
- QFuture<bool> ret = QtConcurrent::run<bool>(lockFromThread, fileName, threadSleepMs, &semThreadReady, &semMainThreadDone);
+ auto ret = QtConcurrent::run(lockFromThreadAndWait, fileName, threadSleepMs, &semThreadReady, &semMainThreadDone);
semThreadReady.acquire();
if (releaseEarly) // let the thread release the lock after threadSleepMs
@@ -410,7 +410,7 @@ void tst_QLockFile::staleLockRace()
QThreadPool::globalInstance()->setMaxThreadCount(10);
QFutureSynchronizer<QString> synchronizer;
for (int i = 0; i < 8; ++i)
- synchronizer.addFuture(QtConcurrent::run<QString>(tryStaleLockFromThread, fileName));
+ synchronizer.addFuture(QtConcurrent::run(tryStaleLockFromThread, fileName));
synchronizer.waitForFinished();
foreach (const QFuture<QString> &future, synchronizer.futures())
QVERIFY2(future.result().isEmpty(), qPrintable(future.result()));
diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp
index 8d046d5499..31d1de4234 100644
--- a/tests/auto/corelib/io/qurl/tst_qurl.cpp
+++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp
@@ -4067,7 +4067,7 @@ void tst_QUrl::testThreading()
QThreadPool::globalInstance()->setMaxThreadCount(100);
QFutureSynchronizer<void> sync;
for (int i = 0; i < 100; ++i)
- sync.addFuture(QtConcurrent::run(this, &tst_QUrl::testThreadingHelper));
+ sync.addFuture(QtConcurrent::run(&tst_QUrl::testThreadingHelper, this));
sync.waitForFinished();
delete s_urlStorage;
}
diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp
index 7e3642752e..3c6da506e2 100644
--- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp
+++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp
@@ -852,14 +852,14 @@ void tst_QMimeDatabase::fromThreads()
QThreadPool tp;
tp.setMaxThreadCount(20);
// Note that data-based tests cannot be used here (QTest::fetchData asserts).
- QtConcurrent::run(&tp, this, &tst_QMimeDatabase::mimeTypeForName);
- QtConcurrent::run(&tp, this, &tst_QMimeDatabase::aliases);
- QtConcurrent::run(&tp, this, &tst_QMimeDatabase::allMimeTypes);
- QtConcurrent::run(&tp, this, &tst_QMimeDatabase::icons);
- QtConcurrent::run(&tp, this, &tst_QMimeDatabase::inheritance);
- QtConcurrent::run(&tp, this, &tst_QMimeDatabase::knownSuffix);
- QtConcurrent::run(&tp, this, &tst_QMimeDatabase::mimeTypeForFileWithContent);
- QtConcurrent::run(&tp, this, &tst_QMimeDatabase::allMimeTypes); // a second time
+ QtConcurrent::run(&tp, &tst_QMimeDatabase::mimeTypeForName, this);
+ QtConcurrent::run(&tp, &tst_QMimeDatabase::aliases, this);
+ QtConcurrent::run(&tp, &tst_QMimeDatabase::allMimeTypes, this);
+ QtConcurrent::run(&tp, &tst_QMimeDatabase::icons, this);
+ QtConcurrent::run(&tp, &tst_QMimeDatabase::inheritance, this);
+ QtConcurrent::run(&tp, &tst_QMimeDatabase::knownSuffix, this);
+ QtConcurrent::run(&tp, &tst_QMimeDatabase::mimeTypeForFileWithContent, this);
+ QtConcurrent::run(&tp, &tst_QMimeDatabase::allMimeTypes, this); // a second time
QVERIFY(tp.waitForDone(60000));
}