/* * Copyright 2014 Daniel Vrátil * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include "../src/async.h" #include #include #include #include class AsyncTest : public QObject { Q_OBJECT public: AsyncTest() {} ~AsyncTest() {} private Q_SLOTS: void testSyncPromises(); void testAsyncPromises(); void testAsyncPromises2(); void testSyncEach(); void testSyncReduce(); }; void AsyncTest::testSyncPromises() { auto baseJob = Async::start( [](Async::Future &f) { f.setValue(42); f.setFinished(); }) .then( [](int v, Async::Future &f) { f.setValue("Result is " + QString::number(v)); f.setFinished(); }); auto job = baseJob.then( [](const QString &v, Async::Future &f) { f.setValue(v.toUpper()); f.setFinished(); }); Async::Future future = job.exec(); QVERIFY(future.isFinished()); QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); } void AsyncTest::testAsyncPromises() { auto job = Async::start( [](Async::Future &future) { QTimer *timer = new QTimer(); QObject::connect(timer, &QTimer::timeout, [&]() { future.setValue(42); future.setFinished(); }); QObject::connect(timer, &QTimer::timeout, timer, &QObject::deleteLater); timer->setSingleShot(true); timer->start(200); }); Async::Future future = job.exec(); future.waitForFinished(); QCOMPARE(future.value(), 42); } void AsyncTest::testAsyncPromises2() { bool done = false; auto job = Async::start( [](Async::Future &future) { QTimer *timer = new QTimer(); QObject::connect(timer, &QTimer::timeout, [&]() { future.setValue(42); future.setFinished(); }); QObject::connect(timer, &QTimer::timeout, timer, &QObject::deleteLater); timer->setSingleShot(true); timer->start(200); } ).then([&done](int result, Async::Future &future) { done = true; future.setValue(result); future.setFinished(); }); auto future = job.exec(); QTRY_VERIFY(done); QCOMPARE(future.value(), 42); } void AsyncTest::testSyncEach() { auto job = Async::start>( [](Async::Future> &future) { future.setValue(QList{ 1, 2, 3, 4 }); future.setFinished(); }) .each, int>( [](const int &v, Async::Future> &future) { future.setValue(QList{ v + 1 }); future.setFinished(); }); Async::Future> future = job.exec(); const QList expected({ 2, 3, 4, 5 }); QVERIFY(future.isFinished()); QCOMPARE(future.value(), expected); } void AsyncTest::testSyncReduce() { auto job = Async::start>( [](Async::Future> &future) { future.setValue(QList{ 1, 2, 3, 4 }); future.setFinished(); }) .reduce>( [](const QList &list, Async::Future &future) { int sum = 0; for (int i : list) sum += i; future.setValue(sum); future.setFinished(); }); Async::Future future = job.exec(); QVERIFY(future.isFinished()); QCOMPARE(future.value(), 10); } QTEST_MAIN(AsyncTest); #include "asynctest.moc"