From 664396b0e550910cea50b7852066a04cc7fec3bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Vr=C3=A1til?= Date: Sun, 14 Dec 2014 12:59:22 +0100 Subject: Async: make the processing truly asynchronous Now calling exec() starts the first job and returns a pending Future immediately. Caller can then use Async::FutureWatcher to wait for the future to become finished, i.e. for all jobs to finish execution. --- async/autotests/asynctest.cpp | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'async/autotests') diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index 7aedfc4..000bb2a 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp @@ -37,6 +37,19 @@ public: ~AsyncTest() {} +private: + template + bool waitForFuture(const Async::Future &f) + { + QEventLoop eventLoop; + Async::FutureWatcher watcher; + connect(&watcher, &Async::FutureWatcher::futureReady, + &eventLoop, &QEventLoop::quit); + watcher.setFuture(f); + eventLoop.exec(); + return true; + } + private Q_SLOTS: void testSyncPromises(); void testAsyncPromises(); @@ -63,8 +76,7 @@ void AsyncTest::testSyncPromises() f.setFinished(); }); - job.exec(); - Async::Future future = job.result(); + Async::Future future = job.exec(); QVERIFY(future.isFinished()); QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); @@ -86,9 +98,9 @@ void AsyncTest::testAsyncPromises() timer->start(200); }); - job.exec(); - Async::Future future = job.result(); - QVERIFY(future.isFinished()); + Async::Future future = job.exec(); + + QVERIFY(waitForFuture(future)); QCOMPARE(future.value(), 42); } @@ -105,8 +117,8 @@ void AsyncTest::testSyncEach() future.setFinished(); }); - job.exec(); - Async::Future> future = job.result(); + Async::Future> future = job.exec(); + const QList expected({ 2, 3, 4, 5 }); QVERIFY(future.isFinished()); QCOMPARE(future.value(), expected); @@ -127,8 +139,8 @@ void AsyncTest::testSyncReduce() future.setFinished(); }); - job.exec(); - Async::Future future = job.result(); + Async::Future future = job.exec(); + QVERIFY(future.isFinished()); QCOMPARE(future.value(), 10); } -- cgit v1.2.3