From 67d47d893bc582fbd06da8bd93a1c416fa314ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Vr=C3=A1til?= Date: Thu, 11 Dec 2014 11:07:04 +0100 Subject: Prototype of Async library --- async/autotests/CMakeLists.txt | 5 +++ async/autotests/asynctest.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 async/autotests/CMakeLists.txt create mode 100644 async/autotests/asynctest.cpp (limited to 'async/autotests') diff --git a/async/autotests/CMakeLists.txt b/async/autotests/CMakeLists.txt new file mode 100644 index 0000000..a2bedc8 --- /dev/null +++ b/async/autotests/CMakeLists.txt @@ -0,0 +1,5 @@ +include_directories(../src ${CMAKE_CURRENT_BINARY_DIR}) + +add_executable(asynctest asynctest.cpp) +qt5_use_modules(asynctest Test) +target_link_libraries(asynctest akonadi2async Qt5::Core Qt5::Test) \ No newline at end of file diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp new file mode 100644 index 0000000..f2a70a4 --- /dev/null +++ b/async/autotests/asynctest.cpp @@ -0,0 +1,98 @@ +/* + * 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 AsyncTest::testSyncPromises() +{ + auto baseJob = Async::start( + []() -> Async::Future { + auto f = Async::Future(42); + f.setFinished(); + return f; + }) + .then( + [](int v) -> Async::Future { + auto f = Async::Future("Result is " + QString::number(v)); + f.setFinished(); + return f; + }); + + auto job = baseJob.then( + [](const QString &v) -> Async::Future { + auto f = Async::Future(v.toUpper()); + f.setFinished(); + return f; + }); + + job.exec(); + Async::Future future = job.result(); + + QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); +} + +void AsyncTest::testAsyncPromises() +{ + auto job = Async::start( + []() -> Async::Future { + Async::Future future(-1); + QTimer *timer = new QTimer(); + QObject::connect(timer, &QTimer::timeout, + [&]() { + future.setValue(42); + future.setFinished(); + timer->deleteLater(); + }); + timer->setSingleShot(true); + timer->start(200); + return future; + }); + + job.exec(); + Async::Future future = job.result(); + QCOMPARE(future.value(), 42); +} + + +QTEST_MAIN(AsyncTest); + +#include "asynctest.moc" -- cgit v1.2.3