summaryrefslogtreecommitdiffstats
path: root/tests/clientapitest.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-07-28 16:50:39 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-07-28 16:50:39 +0200
commite22776a57bd12621358ad7cd98dac3261f2a70db (patch)
treec6fa0cef56931b042c3837826219353c31429422 /tests/clientapitest.cpp
parenta547d10f3d36e7c820d913d22798ca051c6e3df3 (diff)
downloadsink-e22776a57bd12621358ad7cd98dac3261f2a70db.tar.gz
sink-e22776a57bd12621358ad7cd98dac3261f2a70db.zip
Test life queries in the generic facade
Diffstat (limited to 'tests/clientapitest.cpp')
-rw-r--r--tests/clientapitest.cpp102
1 files changed, 7 insertions, 95 deletions
diff --git a/tests/clientapitest.cpp b/tests/clientapitest.cpp
index 9b8c27e..27139c2 100644
--- a/tests/clientapitest.cpp
+++ b/tests/clientapitest.cpp
@@ -7,20 +7,6 @@
7#include "synclistresult.h" 7#include "synclistresult.h"
8#include "resourceconfig.h" 8#include "resourceconfig.h"
9 9
10class RevisionNotifier : public QObject
11{
12 Q_OBJECT
13public:
14 RevisionNotifier() : QObject() {};
15 void notify(qint64 revision)
16 {
17 emit revisionChanged(revision);
18 }
19
20Q_SIGNALS:
21 void revisionChanged(qint64);
22};
23
24class DummyResourceFacade : public Akonadi2::StoreFacade<Akonadi2::ApplicationDomain::Event> 10class DummyResourceFacade : public Akonadi2::StoreFacade<Akonadi2::ApplicationDomain::Event>
25{ 11{
26public: 12public:
@@ -28,70 +14,17 @@ public:
28 KAsync::Job<void> create(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE { return KAsync::null<void>(); }; 14 KAsync::Job<void> create(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE { return KAsync::null<void>(); };
29 KAsync::Job<void> modify(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE { return KAsync::null<void>(); }; 15 KAsync::Job<void> modify(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE { return KAsync::null<void>(); };
30 KAsync::Job<void> remove(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE { return KAsync::null<void>(); }; 16 KAsync::Job<void> remove(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE { return KAsync::null<void>(); };
31 17 KAsync::Job<void> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider) Q_DECL_OVERRIDE
32 KAsync::Job<qint64> load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback)
33 { 18 {
34 return KAsync::start<qint64>([this, resultCallback](KAsync::Future<qint64> &future) {
35 qDebug() << "load called";
36 for(const auto &result : results) {
37 resultCallback(result);
38 }
39 future.setValue(0);
40 future.setFinished();
41 });
42 }
43
44 KAsync::Job<void> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider) Q_DECL_OVERRIDE
45 {
46 auto runner = QSharedPointer<QueryRunner>::create(query);
47 //The runner only lives as long as the resultProvider
48 resultProvider->setQueryRunner(runner);
49 QWeakPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > weakResultProvider = resultProvider;
50 capturedResultProvider = resultProvider; 19 capturedResultProvider = resultProvider;
51 runner->setQuery([this, weakResultProvider, query](qint64 oldRevision, qint64 newRevision) -> KAsync::Job<qint64> { 20 return KAsync::start<void>([this, resultProvider, query]() {
52 qDebug() << "Creating query for revisions: " << oldRevision << newRevision; 21 for (const auto &res : results) {
53 return KAsync::start<qint64>([this, weakResultProvider, query](KAsync::Future<qint64> &future) { 22 resultProvider->add(res);
54 auto resultProvider = weakResultProvider.toStrongRef(); 23 }
55 if (!resultProvider) {
56 Warning() << "Tried executing query after result provider is already gone";
57 future.setError(0, QString());
58 future.setFinished();
59 return;
60 }
61 //TODO only emit changes and don't replace everything
62 resultProvider->clear();
63 //rerun query
64 std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> addCallback = std::bind(&Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr>::add, resultProvider, std::placeholders::_1);
65 load(query, addCallback).then<void, qint64>([resultProvider, &future, query](qint64 queriedRevision) {
66 future.setValue(queriedRevision);
67 future.setFinished();
68 }).exec();
69 });
70 });
71
72 //Ensure the notification is emitted in the right thread
73 //Otherwise we get crashes as we call revisionChanged from the test.
74 if (!notifier) {
75 notifier.reset(new RevisionNotifier);
76 }
77
78 //TODO somehow disconnect as resultNotifier is destroyed. Otherwise we keep the runner alive forever.
79 if (query.liveQuery) {
80 QObject::connect(notifier.data(), &RevisionNotifier::revisionChanged, [runner](qint64 newRevision) {
81 runner->revisionChanged(newRevision);
82 });
83 }
84
85 return KAsync::start<void>([runner](KAsync::Future<void> &future) {
86 runner->run().then<void>([&future]() {
87 //TODO if not live query, destroy runner.
88 future.setFinished();
89 }).exec();
90 }); 24 });
91 } 25 }
92 26
93 QList<Akonadi2::ApplicationDomain::Event::Ptr> results; 27 QList<Akonadi2::ApplicationDomain::Event::Ptr> results;
94 QSharedPointer<RevisionNotifier> notifier;
95 QWeakPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > capturedResultProvider; 28 QWeakPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > capturedResultProvider;
96}; 29};
97 30
@@ -117,7 +50,6 @@ private Q_SLOTS:
117 ResourceConfig::clear(); 50 ResourceConfig::clear();
118 } 51 }
119 52
120
121 void testLoad() 53 void testLoad()
122 { 54 {
123 auto facade = registerDummyFacade(); 55 auto facade = registerDummyFacade();
@@ -133,27 +65,7 @@ private Q_SLOTS:
133 QCOMPARE(result.size(), 1); 65 QCOMPARE(result.size(), 1);
134 } 66 }
135 67
136 void testLiveQuery() 68 //The query provider is supposed to delete itself
137 {
138 auto facade = registerDummyFacade();
139 facade->results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>());
140 ResourceConfig::addResource("dummyresource.instance1", "dummyresource");
141
142 Akonadi2::Query query;
143 query.resources << "dummyresource.instance1";
144 query.liveQuery = true;
145
146 async::SyncListResult<Akonadi2::ApplicationDomain::Event::Ptr> result(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query));
147 result.exec();
148 QCOMPARE(result.size(), 1);
149
150 //Enter a second result
151 facade->results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id2", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>());
152 QVERIFY(facade->notifier);
153 facade->notifier->revisionChanged(2);
154 QTRY_COMPARE(result.size(), 2);
155 }
156
157 void testQueryLifetime() 69 void testQueryLifetime()
158 { 70 {
159 auto facade = registerDummyFacade(); 71 auto facade = registerDummyFacade();
@@ -169,7 +81,7 @@ private Q_SLOTS:
169 result.exec(); 81 result.exec();
170 QCOMPARE(result.size(), 1); 82 QCOMPARE(result.size(), 1);
171 } 83 }
172 //It's running in a separate thread, so we have to wait for a moment. 84 //It's running in a separate thread, so we have to wait for a moment until the query provider deletes itself.
173 QTRY_VERIFY(!facade->capturedResultProvider); 85 QTRY_VERIFY(!facade->capturedResultProvider);
174 } 86 }
175 87