File: itemretrievertest.cpp

package info (click to toggle)
akonadi 4%3A18.08.3-7~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,536 kB
  • sloc: cpp: 98,501; xml: 2,560; sh: 40; makefile: 24
file content (390 lines) | stat: -rw-r--r-- 13,553 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
    Copyright (c) 2013 Volker Krause <vkrause@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include <QObject>
#include <QTest>
#include <QTimer>
#include <QMutex>

#include "storage/itemretriever.h"
#include "storage/itemretrievaljob.h"
#include "storage/itemretrievalmanager.h"
#include "storage/itemretrievalrequest.h"

#include "fakeakonadiserver.h"
#include "dbinitializer.h"

#include <aktest.h>

using namespace Akonadi::Server;

struct JobResult
{
    qint64 pimItemId;
    QByteArray partname;
    QByteArray partdata;
    QString error;
};

class FakeItemRetrievalJob : public AbstractItemRetrievalJob
{
    Q_OBJECT
public:
    FakeItemRetrievalJob(ItemRetrievalRequest *req, DbInitializer &dbInitializer,
                         const QVector<JobResult> &results, QObject *parent)
        : AbstractItemRetrievalJob(req, parent)
        , mDbInitializer(dbInitializer)
        , mResults(results)
    {
    }

    void start() override
    {
        Q_FOREACH (const JobResult &res, mResults) {
            if (res.error.isEmpty()) {
                // This is analogous to what STORE/MERGE does
                const PimItem item = PimItem::retrieveById(res.pimItemId);
                const auto parts = item.parts();
                // Try to find the part by name
                auto it = std::find_if(parts.begin(), parts.end(),
                                       [res](const Part &part) {
                                           return part.partType().name().toLatin1() == res.partname;
                                       });
                if (it == parts.end()) {
                    // Does not exist, create it
                    mDbInitializer.createPart(res.pimItemId, "PLD:" + res.partname, res.partdata);
                } else {
                    // Exist, update it
                    Part part(*it);
                    part.setData(res.partdata);
                    part.setDatasize(res.partdata.size());
                    part.update();
                }
            } else {
                mError = res.error;
                break;
            }
        }

        QTimer::singleShot(0, this, [this]() {
            Q_EMIT requestCompleted(m_request, mError);
        });
    }

    void kill() override
    {
        // TODO?
    }

private:
    DbInitializer &mDbInitializer;
    QVector<JobResult> mResults;
    QString mError;
};

class FakeItemRetrievalJobFactory : public AbstractItemRetrievalJobFactory
{
public:
    FakeItemRetrievalJobFactory(DbInitializer &initializer)
        : mJobsCount(0)
        , mDbInitializer(initializer)
    {
    }

    void addJobResult(qint64 itemId, const QByteArray &partname, const QByteArray &partdata)
    {
        mJobResults.insert(itemId, JobResult{ itemId, partname, partdata, QString() });
    }

    void addJobResult(qint64 itemId, const QString &error)
    {
        mJobResults.insert(itemId, JobResult{ itemId, QByteArray(), QByteArray(), error });
    }

    AbstractItemRetrievalJob *retrievalJob(ItemRetrievalRequest *request, QObject *parent) override
    {
        QVector<JobResult> results;
        Q_FOREACH (auto id, request->ids) {
            auto it = mJobResults.constFind(id);
            while (it != mJobResults.constEnd() && it.key() == id) {
                if (request->parts.contains(it->partname)) {
                    results << *it;
                }
                ++it;
            }
        }

        ++mJobsCount;
        return new FakeItemRetrievalJob(request, mDbInitializer, results, parent);
    }

    int jobsCount() const
    {
        return mJobsCount;
    }

private:
    int mJobsCount;
    DbInitializer &mDbInitializer;
    QMultiHash<qint64, JobResult> mJobResults;
};

using RequestedParts = QVector<QByteArray /* FQ name */>;

class ClientThread : public QThread
{
public:
    ClientThread(Entity::Id itemId, const RequestedParts &requestedParts)
        : m_itemId(itemId), m_requestedParts(requestedParts)
    {}

    void run() override
    {
        // ItemRetriever should...
        ItemRetriever retriever;
        retriever.setItem(m_itemId);
        retriever.setRetrieveParts(m_requestedParts);
        QSignalSpy spy(&retriever, &ItemRetriever::itemsRetrieved);

        const bool success = retriever.exec();

        QMutexLocker lock(&m_mutex);
        m_results.success = success;
        m_results.signalsCount = spy.count();
        if (m_results.signalsCount > 0) {
            m_results.emittedItems = spy.at(0).at(0).value<QList<qint64>>();
        }
    }

    struct Results
    {
        bool success;
        int signalsCount;
        QList<qint64> emittedItems;
    };
    Results results() const {
        QMutexLocker lock(&m_mutex);
        return m_results;
    }

private:
    const Entity::Id m_itemId;
    const RequestedParts m_requestedParts;

    mutable QMutex m_mutex; // protects results below
    Results m_results;
};

class ItemRetrieverTest : public QObject
{
    Q_OBJECT


    using ExistingParts = QVector<QPair<QByteArray /* name */, QByteArray /* data */>>;
    using AvailableParts = QVector<QPair<QByteArray /* name */, QByteArray /* data */>>;

public:
    ItemRetrieverTest()
        : QObject()
    {
        try {
            FakeAkonadiServer::instance()->setPopulateDb(false);
            FakeAkonadiServer::instance()->disableItemRetrievalManager();
            FakeAkonadiServer::instance()->init();
        } catch (const FakeAkonadiServerException &e) {
            qWarning() << "Server exception: " << e.what();
            qFatal("Fake Akonadi Server failed to start up, aborting test");
        }
    }

    ~ItemRetrieverTest()
    {
        FakeAkonadiServer::instance()->quit();
    }

private Q_SLOTS:
    void testFullPayload()
    {
        ItemRetriever r1(nullptr);
        r1.setRetrieveFullPayload(true);
        QCOMPARE(r1.retrieveParts().size(), 1);
        QCOMPARE(r1.retrieveParts().at(0), { "PLD:RFC822" });
        r1.setRetrieveParts({ "PLD:FOO" });
        QCOMPARE(r1.retrieveParts().size(), 2);
    }

    void testRetrieval_data()
    {
        QTest::addColumn<ExistingParts>("existingParts");
        QTest::addColumn<AvailableParts>("availableParts");
        QTest::addColumn<RequestedParts>("requestedParts");
        QTest::addColumn<int>("expectedRetrievalJobs");
        QTest::addColumn<int>("expectedSignals");
        QTest::addColumn<int>("expectedParts");

        QTest::newRow("should retrieve missing payload part")
            << ExistingParts()
            << AvailableParts{ { "RFC822", "somedata" } }
            << RequestedParts{ "PLD:RFC822" }
            << 1 << 1 << 1;

        QTest::newRow("should retrieve multiple missing payload parts")
            << ExistingParts()
            << AvailableParts{ { "RFC822", "somedata" }, { "HEAD", "head" } }
            << RequestedParts{ "PLD:HEAD", "PLD:RFC822" }
            << 1 << 1 << 2;

        QTest::newRow("should not retrieve existing payload part")
            << ExistingParts{ { "PLD:RFC822", "somedata" } }
            << AvailableParts()
            << RequestedParts{ "PLD:RFC822" }
            << 0 << 1 << 1;

        QTest::newRow("should not retrieve multiple existing payload parts")
            << ExistingParts{ { "PLD:RFC822", "somedata" }, { "PLD:HEAD", "head" } }
            << AvailableParts()
            << RequestedParts{ "PLD:RFC822", "PLD:HEAD" }
            << 0 << 1 << 2;

        QTest::newRow("should retrieve missing but not existing payload part")
            << ExistingParts{ { "PLD:HEAD", "head" } }
            << AvailableParts{ { "RFC822", "somedata" } }
            << RequestedParts{ "PLD:HEAD", "PLD:RFC822" }
            << 1 << 1 << 2;

        QTest::newRow("should retrieve expired payload part")
            << ExistingParts{ { "PLD:RFC822", QByteArray() } }
            << AvailableParts{ { "RFC822", "somedata" } }
            << RequestedParts{ "PLD:RFc822" }
            << 1 << 1 << 1;

        QTest::newRow("should not retrieve one out of multiple existing payload parts")
            << ExistingParts{ { "PLD:RFC822", "somedata" }, { "PLD:HEAD", "head" }, { "PLD:ENVELOPE", "envelope" } }
            << AvailableParts()
            << RequestedParts{ "PLD:RFC822", "PLD:HEAD" }
            << 0 << 1 << 3;

        QTest::newRow("should retrieve missing payload part and ignore attributes")
            << ExistingParts{ { "ATR:MYATTR", "myattrdata" } }
            << AvailableParts{ { "RFC822", "somedata" } }
            << RequestedParts{ "PLD:RFC822" }
            << 1 << 1 << 2;
    }

    void testRetrieval()
    {
        QFETCH(ExistingParts, existingParts);
        QFETCH(AvailableParts, availableParts);
        QFETCH(RequestedParts, requestedParts);
        QFETCH(int, expectedRetrievalJobs);
        QFETCH(int, expectedSignals);
        QFETCH(int, expectedParts);


        // Setup
        for (int step = 0; step < 2; ++step) {
            DbInitializer dbInitializer;
            FakeItemRetrievalJobFactory factory(dbInitializer);
            ItemRetrievalManager mgr(&factory);
            QTest::qWait(100);

            // Given a PimItem with existing parts
            Resource res = dbInitializer.createResource("testresource");
            Collection col = dbInitializer.createCollection("col1");

            // step 0: do it in the main thread, for easier debugging
            PimItem item = dbInitializer.createItem("1", col);
            Q_FOREACH (const auto &existingPart, existingParts) {
                dbInitializer.createPart(item.id(), existingPart.first, existingPart.second);
            }

            Q_FOREACH (const auto &availablePart, availableParts) {
                factory.addJobResult(item.id(), availablePart.first, availablePart.second);
            }

            if (step == 0) {
                ClientThread thread(item.id(), requestedParts);
                thread.run();

                const ClientThread::Results results = thread.results();
                // ItemRetriever should ... succeed
                QVERIFY(results.success);
                // Emit exactly one signal ...
                QCOMPARE(results.signalsCount, expectedSignals);
                // ... with that one item
                if (expectedSignals > 0) {
                    QCOMPARE(results.emittedItems, QList<qint64>{ item.id() });
                }

                // Check that the factory had exactly one retrieval job
                QCOMPARE(factory.jobsCount(), expectedRetrievalJobs);

            } else {
                QVector<ClientThread *> threads;
                for (int i = 0; i < 20; ++i) {
                    threads.append(new ClientThread(item.id(), requestedParts));
                }
                for (int i = 0; i < threads.size(); ++i) {
                    threads.at(i)->start();
                }
                for (int i = 0; i < threads.size(); ++i) {
                    threads.at(i)->wait();
                }
                for (int i = 0; i < threads.size(); ++i) {
                    const ClientThread::Results results = threads.at(i)->results();
                    QVERIFY(results.success);
                    QCOMPARE(results.signalsCount, expectedSignals);
                    if (expectedSignals > 0) {
                        QCOMPARE(results.emittedItems, QList<qint64>{ item.id() });
                    }
                }
                qDeleteAll(threads);
            }

            // Check that the parts now exist in the DB
            const auto parts = item.parts();
            QCOMPARE(parts.count(), expectedParts);
            Q_FOREACH (const Part &dbPart, item.parts()) {
                const QString fqname = dbPart.partType().ns() + QLatin1Char(':') + dbPart.partType().name();
                if (!requestedParts.contains(fqname.toLatin1())) {
                    continue;
                }

                auto it = std::find_if(availableParts.constBegin(), availableParts.constEnd(),
                        [dbPart](const QPair<QByteArray, QByteArray> &p) {
                        return dbPart.partType().name().toLatin1() == p.first;
                        });
                if (it == availableParts.constEnd()) {
                    it = std::find_if(existingParts.constBegin(), existingParts.constEnd(),
                            [fqname](const QPair<QByteArray, QByteArray> &p) {
                            return fqname.toLatin1() == p.first;
                            });
                    QVERIFY(it != existingParts.constEnd());
                }

                QCOMPARE(dbPart.data(), it->second);
                QCOMPARE(dbPart.datasize(), it->second.size());
            }
        }
    }
};

AKTEST_FAKESERVER_MAIN(ItemRetrieverTest)

#include "itemretrievertest.moc"