diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-24 10:41:11 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-24 10:41:11 +0200 |
commit | 12539f35e385c9250cd67e387c67dbaff4de34f3 (patch) | |
tree | 5223ebcdc7e94827862f2c9c76a3f6123c8abd1e /tests | |
parent | 2998d9d3d5dfc825904b53393e9ae12e7cd5b72b (diff) | |
download | sink-12539f35e385c9250cd67e387c67dbaff4de34f3.tar.gz sink-12539f35e385c9250cd67e387c67dbaff4de34f3.zip |
Keep thread alive until the end of the query, and cleanup the resultSet.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/clientapitest.cpp | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/tests/clientapitest.cpp b/tests/clientapitest.cpp index 7bcd7b0..057495e 100644 --- a/tests/clientapitest.cpp +++ b/tests/clientapitest.cpp | |||
@@ -24,10 +24,11 @@ class DummyResourceFacade : public Akonadi2::StoreFacade<Akonadi2::ApplicationDo | |||
24 | { | 24 | { |
25 | public: | 25 | public: |
26 | ~DummyResourceFacade(){}; | 26 | ~DummyResourceFacade(){}; |
27 | virtual Async::Job<void> create(const Akonadi2::ApplicationDomain::Event &domainObject){ return Async::null<void>(); }; | 27 | Async::Job<void> create(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE { return Async::null<void>(); }; |
28 | virtual Async::Job<void> modify(const Akonadi2::ApplicationDomain::Event &domainObject){ return Async::null<void>(); }; | 28 | Async::Job<void> modify(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE { return Async::null<void>(); }; |
29 | virtual Async::Job<void> remove(const Akonadi2::ApplicationDomain::Event &domainObject){ return Async::null<void>(); }; | 29 | Async::Job<void> remove(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE { return Async::null<void>(); }; |
30 | virtual Async::Job<qint64> load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) | 30 | |
31 | Async::Job<qint64> load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) | ||
31 | { | 32 | { |
32 | return Async::start<qint64>([this, resultCallback](Async::Future<qint64> &future) { | 33 | return Async::start<qint64>([this, resultCallback](Async::Future<qint64> &future) { |
33 | qDebug() << "load called"; | 34 | qDebug() << "load called"; |
@@ -39,14 +40,23 @@ public: | |||
39 | }); | 40 | }); |
40 | } | 41 | } |
41 | 42 | ||
42 | Async::Job<void> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider) | 43 | Async::Job<void> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider) Q_DECL_OVERRIDE |
43 | { | 44 | { |
44 | auto runner = QSharedPointer<QueryRunner>::create(query); | 45 | auto runner = QSharedPointer<QueryRunner>::create(query); |
45 | //The runner only lives as long as the resultProvider | 46 | //The runner only lives as long as the resultProvider |
46 | resultProvider->setQueryRunner(runner); | 47 | resultProvider->setQueryRunner(runner); |
47 | runner->setQuery([this, resultProvider, query](qint64 oldRevision, qint64 newRevision) -> Async::Job<qint64> { | 48 | QWeakPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > weakResultProvider = resultProvider; |
49 | capturedResultProvider = resultProvider; | ||
50 | runner->setQuery([this, weakResultProvider, query](qint64 oldRevision, qint64 newRevision) -> Async::Job<qint64> { | ||
48 | qDebug() << "Creating query for revisions: " << oldRevision << newRevision; | 51 | qDebug() << "Creating query for revisions: " << oldRevision << newRevision; |
49 | return Async::start<qint64>([this, resultProvider, query](Async::Future<qint64> &future) { | 52 | return Async::start<qint64>([this, weakResultProvider, query](Async::Future<qint64> &future) { |
53 | auto resultProvider = weakResultProvider.toStrongRef(); | ||
54 | if (!resultProvider) { | ||
55 | Warning() << "Tried executing query after result provider is already gone"; | ||
56 | future.setError(0, QString()); | ||
57 | future.setFinished(); | ||
58 | return; | ||
59 | } | ||
50 | //TODO only emit changes and don't replace everything | 60 | //TODO only emit changes and don't replace everything |
51 | resultProvider->clear(); | 61 | resultProvider->clear(); |
52 | //rerun query | 62 | //rerun query |
@@ -84,6 +94,7 @@ public: | |||
84 | 94 | ||
85 | QList<Akonadi2::ApplicationDomain::Event::Ptr> results; | 95 | QList<Akonadi2::ApplicationDomain::Event::Ptr> results; |
86 | QSharedPointer<RevisionNotifier> notifier; | 96 | QSharedPointer<RevisionNotifier> notifier; |
97 | QWeakPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > capturedResultProvider; | ||
87 | }; | 98 | }; |
88 | 99 | ||
89 | class ClientAPITest : public QObject | 100 | class ClientAPITest : public QObject |
@@ -123,8 +134,8 @@ private Q_SLOTS: | |||
123 | facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); | 134 | facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); |
124 | 135 | ||
125 | Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource", | 136 | Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource", |
126 | [&facade](bool &externallManage){ | 137 | [&facade](bool &externallyManaged){ |
127 | externallManage = true; | 138 | externallyManaged = true; |
128 | return &facade; | 139 | return &facade; |
129 | } | 140 | } |
130 | ); | 141 | ); |
@@ -145,6 +156,31 @@ private Q_SLOTS: | |||
145 | QTRY_COMPARE(result.size(), 2); | 156 | QTRY_COMPARE(result.size(), 2); |
146 | } | 157 | } |
147 | 158 | ||
159 | void testQueryLifetime() | ||
160 | { | ||
161 | DummyResourceFacade facade; | ||
162 | facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); | ||
163 | |||
164 | Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource", | ||
165 | [&facade](bool &externallyManaged){ | ||
166 | externallyManaged = true; | ||
167 | return &facade; | ||
168 | } | ||
169 | ); | ||
170 | |||
171 | Akonadi2::Query query; | ||
172 | query.resources << "dummyresource"; | ||
173 | query.liveQuery = true; | ||
174 | |||
175 | { | ||
176 | async::SyncListResult<Akonadi2::ApplicationDomain::Event::Ptr> result(Akonadi2::Store::load<Akonadi2::ApplicationDomain::Event>(query)); | ||
177 | result.exec(); | ||
178 | QCOMPARE(result.size(), 1); | ||
179 | } | ||
180 | //It's running in a separate thread, so we have to wait for a moment. | ||
181 | QTRY_VERIFY(!facade.capturedResultProvider); | ||
182 | } | ||
183 | |||
148 | }; | 184 | }; |
149 | 185 | ||
150 | QTEST_MAIN(ClientAPITest) | 186 | QTEST_MAIN(ClientAPITest) |