summaryrefslogtreecommitdiffstats
path: root/tests/modelinteractivitytest.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-12-27 10:50:18 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-12-27 10:52:56 +0100
commit2b012938ac0adaa173705c931e12f40184036183 (patch)
treeed1f65aa5c1435ef1a4dba6829d306bd1dfbf453 /tests/modelinteractivitytest.cpp
parent5eb17e7eab0cbbed0f7b7df84d745f228446703d (diff)
downloadsink-2b012938ac0adaa173705c931e12f40184036183.tar.gz
sink-2b012938ac0adaa173705c931e12f40184036183.zip
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.
Diffstat (limited to 'tests/modelinteractivitytest.cpp')
-rw-r--r--tests/modelinteractivitytest.cpp101
1 files changed, 101 insertions, 0 deletions
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 @@
1#include <QtTest>
2
3#include <QString>
4#include <iostream>
5
6#include "dummyresource/resourcefactory.h"
7#include "clientapi.h"
8#include "commands.h"
9#include "resourceconfig.h"
10#include "log.h"
11#include "modelresult.h"
12
13static int blockingTime;
14
15class TimeMeasuringApplication : public QCoreApplication
16{
17 QElapsedTimer t;
18public:
19 TimeMeasuringApplication(int& argc, char ** argv) : QCoreApplication(argc, argv) { }
20 virtual ~TimeMeasuringApplication() { }
21
22 virtual bool notify(QObject* receiver, QEvent* event)
23 {
24 t.start();
25 const bool ret = QCoreApplication::notify(receiver, event);
26 if(t.elapsed() > 1)
27 std::cout << QString("processing event type %1 for object %2 took %3ms")
28 .arg((int)event->type())
29 .arg(""/* receiver->objectName().toLocal8Bit().data()*/)
30 .arg((int)t.elapsed())
31 .toStdString() << std::endl;
32 blockingTime += t.elapsed();
33 return ret;
34 }
35};
36
37/**
38 * Ensure that queries don't block the system for an extended period of time.
39 *
40 * This is done by ensuring that the event loop is never blocked.
41 */
42class ModelinteractivityTest : public QObject
43{
44 Q_OBJECT
45private Q_SLOTS:
46 void initTestCase()
47 {
48 Akonadi2::Log::setDebugOutputLevel(Akonadi2::Log::Warning);
49 DummyResource::removeFromDisk("org.kde.dummy.instance1");
50 ResourceConfig::addResource("org.kde.dummy.instance1", "org.kde.dummy");
51 }
52
53 void cleanup()
54 {
55 Akonadi2::Store::shutdown(QByteArray("org.kde.dummy.instance1")).exec().waitForFinished();
56 DummyResource::removeFromDisk("org.kde.dummy.instance1");
57 Akonadi2::Store::start(QByteArray("org.kde.dummy.instance1")).exec().waitForFinished();
58 }
59
60 void init()
61 {
62 }
63
64 void testSingle()
65 {
66 //Setup
67 {
68 Akonadi2::ApplicationDomain::Mail mail("org.kde.dummy.instance1");
69 for (int i = 0; i < 1000; i++) {
70 Akonadi2::Store::create<Akonadi2::ApplicationDomain::Mail>(mail).exec().waitForFinished();
71 }
72 }
73
74 Akonadi2::Query query;
75 query.resources << "org.kde.dummy.instance1";
76 query.syncOnDemand = false;
77 query.processAll = true;
78 query.liveQuery = true;
79
80 Akonadi2::Store::synchronize(query).exec().waitForFinished();
81
82 //Test
83 QTime time;
84 time.start();
85 auto model = Akonadi2::Store::loadModel<Akonadi2::ApplicationDomain::Mail>(query);
86 blockingTime += time.elapsed();
87 QTRY_VERIFY(model->data(QModelIndex(), Akonadi2::Store::ChildrenFetchedRole).toBool());
88 //Never block longer than 10 ms
89 QVERIFY2(blockingTime < 10, QString("Total blocking time: %1").arg(blockingTime).toLatin1().data());
90 }
91};
92
93int main(int argc, char *argv[])
94{
95 blockingTime = 0;
96 TimeMeasuringApplication app(argc, argv);
97 ModelinteractivityTest tc;
98 return QTest::qExec(&tc, argc, argv);
99}
100
101#include "modelinteractivitytest.moc"