1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT
#include "testobject.h"
#include "qcorofuture.h"
#include <QString>
#include <QException>
#include <QtConcurrentRun>
#if QT_VERSION_MAJOR > 6
#include <QPromise>
#endif
#include <thread>
class TestException : public QException {
public:
explicit TestException(const QString &msg)
: mMsg(msg)
{}
const char *what() const noexcept override { return qUtf8Printable(mMsg); }
TestException *clone() const override {
return new TestException(mMsg);
}
void raise() const override {
throw *this;
}
private:
QString mMsg;
};
class MoveOnly
{
public:
explicit MoveOnly(int value)
: value(value)
{}
MoveOnly(const MoveOnly &) = delete;
MoveOnly(MoveOnly &&) = default;
MoveOnly &operator=(const MoveOnly &) = delete;
MoveOnly &operator=(MoveOnly &&) = default;
~MoveOnly() = default;
int value = 0;
};
class QCoroFutureTest : public QCoro::TestObject<QCoroFutureTest> {
Q_OBJECT
private:
QCoro::Task<> testTriggers_coro(QCoro::TestContext) {
auto future = QtConcurrent::run([] { std::this_thread::sleep_for(100ms); });
co_await future;
QCORO_VERIFY(future.isFinished());
}
QCoro::Task<> testQCoroWrapperTriggers_coro(QCoro::TestContext) {
auto future = QtConcurrent::run([] { std::this_thread::sleep_for(100ms); });
co_await qCoro(future).waitForFinished();
QCORO_VERIFY(future.isFinished());
}
void testThenQCoroWrapperTriggers_coro(TestLoop &el) {
auto future = QtConcurrent::run([] { std::this_thread::sleep_for(100ms); });
bool called = false;
qCoro(future).waitForFinished().then([&]() {
called = true;
el.quit();
});
el.exec();
QVERIFY(called);
}
QCoro::Task<> testReturnsResult_coro(QCoro::TestContext) {
const QString result = co_await QtConcurrent::run([] {
std::this_thread::sleep_for(100ms);
return QStringLiteral("42");
});
QCORO_COMPARE(result, QStringLiteral("42"));
}
void testThenReturnsResult_coro(TestLoop &el) {
const auto future = QtConcurrent::run([] {
std::this_thread::sleep_for(100ms);
return QStringLiteral("42");
});
bool called = false;
qCoro(future).waitForFinished().then([&](const QString &result) {
called = true;
el.quit();
QCOMPARE(result, QStringLiteral("42"));
});
el.exec();
QVERIFY(called);
}
QCoro::Task<> testDoesntBlockEventLoop_coro(QCoro::TestContext) {
QCoro::EventLoopChecker eventLoopResponsive;
co_await QtConcurrent::run([] { std::this_thread::sleep_for(500ms); });
QCORO_VERIFY(eventLoopResponsive);
}
QCoro::Task<> testDoesntCoAwaitFinishedFuture_coro(QCoro::TestContext test) {
auto future = QtConcurrent::run([] { std::this_thread::sleep_for(100ms); });
co_await future;
QCORO_VERIFY(future.isFinished());
test.setShouldNotSuspend();
co_await future;
}
void testThenDoesntCoAwaitFinishedFuture_coro(TestLoop &el) {
auto future = QtConcurrent::run([] { std::this_thread::sleep_for(1ms); });
QTest::qWait((100ms).count());
QVERIFY(future.isFinished());
bool called = false;
qCoro(future).waitForFinished().then([&]() {
called = true;
el.quit();
});
el.exec();
QVERIFY(called);
}
QCoro::Task<> testDoesntCoAwaitCanceledFuture_coro(QCoro::TestContext test) {
test.setShouldNotSuspend();
QFuture<void> future;
co_await future;
}
void testThenDoesntCoAwaitCanceledFuture_coro(TestLoop &el) {
QFuture<void> future;
bool called = false;
qCoro(future).waitForFinished().then([&]() {
called = true;
el.quit();
});
el.exec();
QVERIFY(called);
}
QCoro::Task<> testPropagateQExceptionFromVoidConcurrent_coro(QCoro::TestContext) {
auto future = QtConcurrent::run([]() {
std::this_thread::sleep_for(100ms);
throw TestException(QStringLiteral("Ooops"));
});
QCORO_VERIFY_EXCEPTION_THROWN(co_await future, TestException);
}
QCoro::Task<> testPropagateQExceptionFromNonvoidConcurrent_coro(QCoro::TestContext) {
bool throwException = true;
auto future = QtConcurrent::run([throwException]() -> int {
std::this_thread::sleep_for(100ms);
if (throwException) { // Workaround MSVC reporting the "return" stmt as unreachablet
throw TestException(QStringLiteral("Ooops"));
}
return 42;
});
QCORO_VERIFY_EXCEPTION_THROWN(co_await future, TestException);
}
#if QT_VERSION_MAJOR >= 6
QCoro::Task<> testPropagateQExceptionFromVoidPromise_coro(QCoro::TestContext) {
QPromise<void> promise;
QTimer::singleShot(100ms, this, [&promise]() {
promise.start();
promise.setException(TestException(QStringLiteral("Booom")));
promise.finish();
});
QCORO_VERIFY_EXCEPTION_THROWN(co_await promise.future(), TestException);
}
QCoro::Task<> testPropagateQExceptionFromNonvoidPromise_coro(QCoro::TestContext) {
QPromise<int> promise;
QTimer::singleShot(100ms, this, [&promise]() {
promise.start();
promise.setException(TestException(QStringLiteral("Booom")));
promise.finish();
});
QCORO_VERIFY_EXCEPTION_THROWN(co_await promise.future(), TestException);
}
QCoro::Task<> testPropagateStdExceptionFromVoidPromise_coro(QCoro::TestContext) {
QPromise<void> promise;
QTimer::singleShot(100ms, this, [&promise]() {
promise.start();
promise.setException(std::make_exception_ptr(std::runtime_error("Booom")));
promise.finish();
});
QCORO_VERIFY_EXCEPTION_THROWN(co_await promise.future(), std::runtime_error);
}
QCoro::Task<> testPropagateStdExceptionFromNonvoidPromise_coro(QCoro::TestContext) {
QPromise<void> promise;
QTimer::singleShot(100ms, this, [&promise]() {
promise.start();
promise.setException(std::make_exception_ptr(std::runtime_error("Booom")));
promise.finish();
});
QCORO_VERIFY_EXCEPTION_THROWN(co_await promise.future(), std::runtime_error);
}
QCoro::Task<> testTakeResult_coro(QCoro::TestContext) {
auto future = QtConcurrent::run([]() -> MoveOnly {
std::this_thread::sleep_for(10ms);
return MoveOnly(42);
});
MoveOnly result = co_await qCoro(future).takeResult();
QCORO_COMPARE(result.value, 42);
QPromise<MoveOnly> promise;
QTimer::singleShot(10ms, this, [&promise]() {
promise.start();
promise.addResult(MoveOnly(84));
promise.finish();
});
QCORO_COMPARE((co_await qCoro(promise.future()).takeResult()).value, 84);
}
void testThenTakeResult_coro(TestLoop &el) {
auto future = QtConcurrent::run([]() -> MoveOnly {
std::this_thread::sleep_for(10ms);
return MoveOnly(42);
});
bool called = false;
qCoro(future).takeResult().then([&](MoveOnly result) {
called = true;
QCOMPARE(result.value, 42);
el.quit();
});
el.exec();
QVERIFY(called);
}
#endif
// QPromise cancelling running future on destruction has been introduced in
// Qt 6.3.
#if QT_VERSION >= QT_VERSION_CHECK(6, 3, 1)
QCoro::Task<> testUnfinishedPromiseDestroyed_coro(QCoro::TestContext) {
const auto future = [this]() {
auto promise = std::make_shared<QPromise<int>>();
auto future = promise->future();
QTimer::singleShot(400ms, this, [p = promise]() {
p->addResult(42);
});
return future;
}();
co_await future;
}
#endif
private Q_SLOTS:
addTest(Triggers)
addCoroAndThenTests(ReturnsResult)
addTest(DoesntBlockEventLoop)
addCoroAndThenTests(DoesntCoAwaitFinishedFuture)
addCoroAndThenTests(DoesntCoAwaitCanceledFuture)
addCoroAndThenTests(QCoroWrapperTriggers)
addTest(PropagateQExceptionFromVoidConcurrent)
addTest(PropagateQExceptionFromNonvoidConcurrent)
#if QT_VERSION_MAJOR >= 6
addTest(PropagateQExceptionFromVoidPromise)
addTest(PropagateQExceptionFromNonvoidPromise)
addTest(PropagateStdExceptionFromVoidPromise)
addTest(PropagateStdExceptionFromNonvoidPromise)
addCoroAndThenTests(TakeResult)
#endif
#if QT_VERSION >= QT_VERSION_CHECK(6, 3, 1)
addTest(UnfinishedPromiseDestroyed)
#endif
};
QTEST_GUILESS_MAIN(QCoroFutureTest)
#include "qfuture.moc"
|