From 2b012938ac0adaa173705c931e12f40184036183 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 27 Dec 2015 10:50:18 +0100 Subject: Threaded query runner implementation All database access is now implemented in threads, to avoid blocking the main thread. The resource communication still resides in the main thread to keep the coordination simple. With it comes a test that ensures we don't block the main thread for too long. --- tests/CMakeLists.txt | 2 + tests/modelinteractivitytest.cpp | 101 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 tests/modelinteractivitytest.cpp (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5d64511..1e0f6b5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -50,11 +50,13 @@ auto_tests ( querytest databasepopulationandfacadequerybenchmark dummyresourcewritebenchmark + modelinteractivitytest ) target_link_libraries(dummyresourcetest akonadi2_resource_dummy) target_link_libraries(dummyresourcebenchmark akonadi2_resource_dummy) target_link_libraries(dummyresourcewritebenchmark akonadi2_resource_dummy) target_link_libraries(querytest akonadi2_resource_dummy) +target_link_libraries(modelinteractivitytest akonadi2_resource_dummy) if (BUILD_MAILDIR) auto_tests ( diff --git a/tests/modelinteractivitytest.cpp b/tests/modelinteractivitytest.cpp new file mode 100644 index 0000000..52db932 --- /dev/null +++ b/tests/modelinteractivitytest.cpp @@ -0,0 +1,101 @@ +#include + +#include +#include + +#include "dummyresource/resourcefactory.h" +#include "clientapi.h" +#include "commands.h" +#include "resourceconfig.h" +#include "log.h" +#include "modelresult.h" + +static int blockingTime; + +class TimeMeasuringApplication : public QCoreApplication +{ + QElapsedTimer t; +public: + TimeMeasuringApplication(int& argc, char ** argv) : QCoreApplication(argc, argv) { } + virtual ~TimeMeasuringApplication() { } + + virtual bool notify(QObject* receiver, QEvent* event) + { + t.start(); + const bool ret = QCoreApplication::notify(receiver, event); + if(t.elapsed() > 1) + std::cout << QString("processing event type %1 for object %2 took %3ms") + .arg((int)event->type()) + .arg(""/* receiver->objectName().toLocal8Bit().data()*/) + .arg((int)t.elapsed()) + .toStdString() << std::endl; + blockingTime += t.elapsed(); + return ret; + } +}; + +/** + * Ensure that queries don't block the system for an extended period of time. + * + * This is done by ensuring that the event loop is never blocked. + */ +class ModelinteractivityTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase() + { + Akonadi2::Log::setDebugOutputLevel(Akonadi2::Log::Warning); + DummyResource::removeFromDisk("org.kde.dummy.instance1"); + ResourceConfig::addResource("org.kde.dummy.instance1", "org.kde.dummy"); + } + + void cleanup() + { + Akonadi2::Store::shutdown(QByteArray("org.kde.dummy.instance1")).exec().waitForFinished(); + DummyResource::removeFromDisk("org.kde.dummy.instance1"); + Akonadi2::Store::start(QByteArray("org.kde.dummy.instance1")).exec().waitForFinished(); + } + + void init() + { + } + + void testSingle() + { + //Setup + { + Akonadi2::ApplicationDomain::Mail mail("org.kde.dummy.instance1"); + for (int i = 0; i < 1000; i++) { + Akonadi2::Store::create(mail).exec().waitForFinished(); + } + } + + Akonadi2::Query query; + query.resources << "org.kde.dummy.instance1"; + query.syncOnDemand = false; + query.processAll = true; + query.liveQuery = true; + + Akonadi2::Store::synchronize(query).exec().waitForFinished(); + + //Test + QTime time; + time.start(); + auto model = Akonadi2::Store::loadModel(query); + blockingTime += time.elapsed(); + QTRY_VERIFY(model->data(QModelIndex(), Akonadi2::Store::ChildrenFetchedRole).toBool()); + //Never block longer than 10 ms + QVERIFY2(blockingTime < 10, QString("Total blocking time: %1").arg(blockingTime).toLatin1().data()); + } +}; + +int main(int argc, char *argv[]) +{ + blockingTime = 0; + TimeMeasuringApplication app(argc, argv); + ModelinteractivityTest tc; + return QTest::qExec(&tc, argc, argv); +} + +#include "modelinteractivitytest.moc" -- cgit v1.2.3