From a6959d8414da1efbe90e7dbc364bffb6658bba74 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 7 Aug 2015 18:41:46 +0200 Subject: Example for parallel processing of jobs --- tests/dummyresourcebenchmark.cpp | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'tests/dummyresourcebenchmark.cpp') diff --git a/tests/dummyresourcebenchmark.cpp b/tests/dummyresourcebenchmark.cpp index 4c649a9..ed448f0 100644 --- a/tests/dummyresourcebenchmark.cpp +++ b/tests/dummyresourcebenchmark.cpp @@ -45,18 +45,50 @@ private Q_SLOTS: removeFromDisk("org.kde.dummy.instance1.index.uid"); } + static KAsync::Job waitForCompletion(QList > &futures) + { + auto context = new QObject; + return KAsync::start([futures, context](KAsync::Future &future) { + const auto total = futures.size(); + auto count = QSharedPointer::create(); + int i = 0; + for (KAsync::Future subFuture : futures) { + i++; + if (subFuture.isFinished()) { + *count += 1; + continue; + } + //FIXME bind lifetime all watcher to future (repectively the main job + auto watcher = QSharedPointer >::create(); + QObject::connect(watcher.data(), &KAsync::FutureWatcher::futureReady, + [count, total, &future](){ + *count += 1; + if (*count == total) { + future.setFinished(); + } + }); + watcher->setFuture(subFuture); + context->setProperty(QString("future%1").arg(i).toLatin1().data(), QVariant::fromValue(watcher)); + } + }).then([context]() { + delete context; + }); + } + void testWriteToFacadeAndQueryByUid() { QTime time; time.start(); int num = 10000; + QList > waitCondition; for (int i = 0; i < num; i++) { Akonadi2::ApplicationDomain::Event event; event.setProperty("uid", "testuid"); QCOMPARE(event.getProperty("uid").toByteArray(), QByteArray("testuid")); event.setProperty("summary", "summaryValue"); - Akonadi2::Store::create(event, "org.kde.dummy.instance1"); + waitCondition << Akonadi2::Store::create(event, "org.kde.dummy.instance1").exec(); } + waitForCompletion(waitCondition).exec().waitForFinished(); auto appendTime = time.elapsed(); //Ensure everything is processed -- cgit v1.2.3