diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-12-04 17:20:29 +0100 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-12-11 09:36:45 +0100 |
commit | 335acffe1d955b5fe4535dc1d99ec2c3dbe1b978 (patch) | |
tree | 4648a3814a956bbe5d0dc8c39bf4f120ce30c591 /tests/auto/corelib | |
parent | b283ce1e836ab08e602a11ea255ee3d8e537902e (diff) |
Add support of invoking QFuture continuations in a given context
Added overloads of .then()/.onFailed()/.onCanceled() which take a
pointer of a context object, and invoke the continuations in the
object's thread.
Task-number: QTBUG-86794
Change-Id: I0f3cbb0500695673fc4087af5d4b96b416e3e1ce
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r-- | tests/auto/corelib/thread/qfuture/tst_qfuture.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp index c7d552cb30..3616500af8 100644 --- a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp @@ -138,6 +138,7 @@ private slots: void onFailedForMoveOnlyTypes(); #endif void onCanceled(); + void continuationsWithContext(); #if 0 // TODO: enable when QFuture::takeResults() is enabled void takeResults(); @@ -2820,6 +2821,85 @@ void tst_QFuture::onCanceled() #endif // QT_NO_EXCEPTIONS } +void tst_QFuture::continuationsWithContext() +{ + QThread thread; + thread.start(); + + auto context = new QObject(); + context->moveToThread(&thread); + + auto tstThread = QThread::currentThread(); + + // .then() + { + auto future = QtFuture::makeReadyFuture(0) + .then([&](int val) { + if (QThread::currentThread() != tstThread) + return 0; + return val + 1; + }) + .then(context, + [&](int val) { + if (QThread::currentThread() != &thread) + return 0; + return val + 1; + }) + .then([&](int val) { + if (QThread::currentThread() != &thread) + return 0; + return val + 1; + }); + QCOMPARE(future.result(), 3); + } + + // .onCanceled + { + auto future = createCanceledFuture<int>() + .onCanceled(context, + [&] { + if (QThread::currentThread() != &thread) + return 0; + return 1; + }) + .then([&](int val) { + if (QThread::currentThread() != &thread) + return 0; + return val + 1; + }); + QCOMPARE(future.result(), 2); + } + +#ifndef QT_NO_EXCEPTIONS + // .onFaled() + { + auto future = QtFuture::makeReadyFuture() + .then([&] { + if (QThread::currentThread() != tstThread) + return 0; + throw std::runtime_error("error"); + }) + .onFailed(context, + [&] { + if (QThread::currentThread() != &thread) + return 0; + return 1; + }) + .then([&](int val) { + if (QThread::currentThread() != &thread) + return 0; + return val + 1; + }); + QCOMPARE(future.result(), 2); + } +#endif // QT_NO_EXCEPTIONS + + context->deleteLater(); + + thread.quit(); + thread.wait(); +} + void tst_QFuture::testSingleResult(const UniquePtr &p) { QVERIFY(p.get() != nullptr); |