Extend QFuture continuations tests

Create tests to check that the continuations attached to ready futures
are immediately executed, and that the proper handlers are selected.

These checks were missing from the overall test set, which was detected
while working on the linked issue.

Task-number: QTBUG-112958
Change-Id: Iae97e4b9dfb1e016869693a5162f72e027ca7f5e
Reviewed-by: Arno Rehn <a.rehn@menlosystems.com>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Ivan Solovev 2023-05-08 18:55:17 +02:00
parent b3b6dd49ba
commit 86c044176f

View File

@ -209,6 +209,7 @@ private slots:
void rejectPendingResultOverwrite();
void createReadyFutures();
void continuationsAfterReadyFutures();
void getFutureInterface();
void convertQMetaType();
@ -4261,6 +4262,108 @@ QT_WARNING_POP
}
}
void tst_QFuture::continuationsAfterReadyFutures()
{
// continuations without a context
{
QFuture<int> f = QtFuture::makeReadyValueFuture(42)
.then([](int val) {
return val + 10;
})
.onCanceled([]() {
return -1;
});
QCOMPARE(f.result(), 52);
}
{
auto rangeF = QtFuture::makeReadyRangeFuture({1, 2, 3});
QFuture<int> f = rangeF
.then([vals = rangeF.results()](auto) {
return vals.last();
})
.onCanceled([]() {
return -1;
});
QCOMPARE(f.result(), 3);
}
{
QFuture<int> f = QtFuture::makeReadyVoidFuture()
.then([]() {
return 1;
})
.onCanceled([]() {
return -1;
});
QCOMPARE(f.result(), 1);
}
#ifndef QT_NO_EXCEPTIONS
{
QException e;
QFuture<int> f = QtFuture::makeExceptionalFuture<int>(e)
.then([](int) {
return 1;
})
.onCanceled([]() {
return -1;
})
.onFailed([](const QException &) {
return -2;
});
QCOMPARE(f.result(), -2);
}
#endif
// continuations with a context
QObject context;
{
QFuture<int> f = QtFuture::makeReadyValueFuture(42)
.then(&context, [](int val) {
return val + 10;
})
.onCanceled([]() {
return -1;
});
QCOMPARE(f.result(), 52);
}
{
auto rangeF = QtFuture::makeReadyRangeFuture({1, 2, 3});
QFuture<int> f = rangeF
.then(&context, [vals = rangeF.results()](auto) {
return vals.last();
})
.onCanceled([]() {
return -1;
});
QCOMPARE(f.result(), 3);
}
{
QFuture<int> f = QtFuture::makeReadyVoidFuture()
.then(&context, []() {
return 1;
})
.onCanceled([]() {
return -1;
});
QCOMPARE(f.result(), 1);
}
#ifndef QT_NO_EXCEPTIONS
{
QException e;
QFuture<int> f = QtFuture::makeExceptionalFuture<int>(e)
.then(&context, [](int) {
return 1;
})
.onCanceled([]() {
return -1;
})
.onFailed([](const QException &) {
return -2;
});
QCOMPARE(f.result(), -2);
}
#endif
}
void tst_QFuture::getFutureInterface()
{
const int val = 42;