summaryrefslogtreecommitdiffstats
path: root/tests/storagetest.cpp
blob: 7060ef2b5a88e173fe3d6597f892611a1e3cce6e (plain)
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
#include <QtTest>

#include <iostream>

#include <QDebug>
#include <QString>
#include <QtConcurrent/QtConcurrentRun>

#include "common/storage.h"

class StorageTest : public QObject
{
    Q_OBJECT
private:
    //This should point to a directory on disk and not a ramdisk (since we're measuring performance)
    QString testDataPath;
    QString dbName;
    const char *keyPrefix = "key";

    void populate(int count)
    {
        Akonadi2::Storage storage(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        auto transaction = storage.createTransaction(Akonadi2::Storage::ReadWrite);
        for (int i = 0; i < count; i++) {
            //This should perhaps become an implementation detail of the db?
            if (i % 10000 == 0) {
                if (i > 0) {
                    transaction.commit();
                    transaction = std::move(storage.createTransaction(Akonadi2::Storage::ReadWrite));
                }
            }
            transaction.openDatabase().write(keyPrefix + QByteArray::number(i), keyPrefix + QByteArray::number(i));
        }
        transaction.commit();
    }

    bool verify(Akonadi2::Storage &storage, int i)
    {
        bool success = true;
        bool keyMatch = true;
        const auto reference = keyPrefix + QByteArray::number(i);
        storage.createTransaction(Akonadi2::Storage::ReadOnly).openDatabase().scan(keyPrefix + QByteArray::number(i),
            [&keyMatch, &reference](const QByteArray &key, const QByteArray &value) -> bool {
                if (value != reference) {
                    qDebug() << "Mismatch while reading";
                    keyMatch = false;
                }
                return keyMatch;
            },
            [&success](const Akonadi2::Storage::Error &error) {
                qDebug() << error.message;
                success = false;
            }
        );
        return success && keyMatch;
    }

private Q_SLOTS:
    void initTestCase()
    {
        testDataPath = "./testdb";
        dbName = "test";
        Akonadi2::Storage storage(testDataPath, dbName);
        storage.removeFromDisk();
    }

    void cleanup()
    {
        Akonadi2::Storage storage(testDataPath, dbName);
        storage.removeFromDisk();
    }

    void testCleanup()
    {
        populate(1);
        Akonadi2::Storage storage(testDataPath, dbName);
        storage.removeFromDisk();
        QFileInfo info(testDataPath + "/" + dbName);
        QVERIFY(!info.exists());
    }

    void testRead()
    {
        const int count = 100;

        populate(count);

        //ensure we can read everything back correctly
        {
            Akonadi2::Storage storage(testDataPath, dbName);
            for (int i = 0; i < count; i++) {
                QVERIFY(verify(storage, i));
            }
        }
    }

    void testScan()
    {
        const int count = 100;
        populate(count);

        //ensure we can scan for values
        {
            int hit = 0;
            Akonadi2::Storage store(testDataPath, dbName);
            store.createTransaction(Akonadi2::Storage::ReadOnly).openDatabase().scan("", [&](const QByteArray &key, const QByteArray &value) -> bool {
                if (key == "key50") {
                    hit++;
                }
                return true;
            });
            QCOMPARE(hit, 1);
        }

        //ensure we can read a single value
        {
            int hit = 0;
            bool foundInvalidValue = false;
            Akonadi2::Storage store(testDataPath, dbName);
            store.createTransaction(Akonadi2::Storage::ReadOnly).openDatabase().scan("key50", [&](const QByteArray &key, const QByteArray &value) -> bool {
                if (key != "key50") {
                    foundInvalidValue = true;
                }
                hit++;
                return true;
            });
            QVERIFY(!foundInvalidValue);
            QCOMPARE(hit, 1);
        }
    }

    void testNestedOperations()
    {
        populate(3);
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite);
        transaction.openDatabase().scan("key1", [&](const QByteArray &key, const QByteArray &value) -> bool {
            transaction.openDatabase().remove(key, [](const Akonadi2::Storage::Error &) {
                QVERIFY(false);
            });
            return false;
        });
    }

    void testNestedTransactions()
    {
        populate(3);
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        store.createTransaction(Akonadi2::Storage::ReadOnly).openDatabase().scan("key1", [&](const QByteArray &key, const QByteArray &value) -> bool {
            store.createTransaction(Akonadi2::Storage::ReadWrite).openDatabase().remove(key, [](const Akonadi2::Storage::Error &) {
                QVERIFY(false);
            });
            return false;
        });
    }

    void testReadEmptyDb()
    {
        bool gotResult = false;
        bool gotError = false;
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        auto transaction = store.createTransaction(Akonadi2::Storage::ReadOnly);
        auto db = transaction.openDatabase("default", [&](const Akonadi2::Storage::Error &error) {
            qDebug() << error.message;
            gotError = true;
        });
        int numValues = db.scan("", [&](const QByteArray &key, const QByteArray &value) -> bool {
            gotResult = true;
            return false;
        },
        [&](const Akonadi2::Storage::Error &error) {
            qDebug() << error.message;
            gotError = true;
        });
        QCOMPARE(numValues, 0);
        QVERIFY(!gotResult);
        QVERIFY(!gotError);
    }

    void testConcurrentRead()
    {
        //With a count of 10000 this test is more likely to expose problems, but also takes some time to execute.
        const int count = 1000;

        populate(count);
        // QTest::qWait(500);

        //We repeat the test a bunch of times since failing is relatively random
        for (int tries = 0; tries < 10; tries++) {
            bool error = false;
            //Try to concurrently read
            QList<QFuture<void> > futures;
            const int concurrencyLevel = 20;
            for (int num = 0; num < concurrencyLevel; num++) {
                futures << QtConcurrent::run([this, count, &error](){
                    Akonadi2::Storage storage(testDataPath, dbName, Akonadi2::Storage::ReadOnly);
                    Akonadi2::Storage storage2(testDataPath, dbName + "2", Akonadi2::Storage::ReadOnly);
                    for (int i = 0; i < count; i++) {
                        if (!verify(storage, i)) {
                            error = true;
                            break;
                        }
                    }
                });
            }
            for(auto future : futures) {
                future.waitForFinished();
            }
            QVERIFY(!error);
        }

        {
            Akonadi2::Storage storage(testDataPath, dbName);
            storage.removeFromDisk();
            Akonadi2::Storage storage2(testDataPath, dbName + "2");
            storage2.removeFromDisk();
        }
    }

    void testNoDuplicates()
    {
        bool gotResult = false;
        bool gotError = false;
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite);
        auto db = transaction.openDatabase("default", nullptr, false);
        db.write("key","value");
        db.write("key","value");

        int numValues = db.scan("", [&](const QByteArray &key, const QByteArray &value) -> bool {
            gotResult = true;
            return true;
        },
        [&](const Akonadi2::Storage::Error &error) {
            qDebug() << error.message;
            gotError = true;
        });

        QCOMPARE(numValues, 1);
        QVERIFY(!gotError);
    }

    void testDuplicates()
    {
        bool gotResult = false;
        bool gotError = false;
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite);
        auto db = transaction.openDatabase("default", nullptr, true);
        db.write("key","value1");
        db.write("key","value2");
        int numValues = db.scan("key", [&](const QByteArray &key, const QByteArray &value) -> bool {
            gotResult = true;
            return true;
        },
        [&](const Akonadi2::Storage::Error &error) {
            qDebug() << error.message;
            gotError = true;
        });

        QCOMPARE(numValues, 2);
        QVERIFY(!gotError);
    }

    void testNonexitingNamedDb()
    {
        bool gotResult = false;
        bool gotError = false;
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadOnly);
        int numValues = store.createTransaction(Akonadi2::Storage::ReadOnly).openDatabase("test").scan("", [&](const QByteArray &key, const QByteArray &value) -> bool {
            gotResult = true;
            return false;
        },
        [&](const Akonadi2::Storage::Error &error) {
            qDebug() << error.message;
            gotError = true;
        });
        QCOMPARE(numValues, 0);
        QVERIFY(!gotResult);
        QVERIFY(!gotError);
    }

    void testWriteToNamedDb()
    {
        bool gotError = false;
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        store.createTransaction(Akonadi2::Storage::ReadWrite).openDatabase("test").write("key1", "value1", [&](const Akonadi2::Storage::Error &error) {
            qDebug() << error.message;
            gotError = true;
        });
        QVERIFY(!gotError);
    }

    void testWriteDuplicatesToNamedDb()
    {
        bool gotError = false;
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        store.createTransaction(Akonadi2::Storage::ReadWrite).openDatabase("test", nullptr, true).write("key1", "value1", [&](const Akonadi2::Storage::Error &error) {
            qDebug() << error.message;
            gotError = true;
        });
        QVERIFY(!gotError);
    }

    //By default we want only exact matches
    void testSubstringKeys()
    {
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite);
        auto db = transaction.openDatabase("test", nullptr, true);
        db.write("sub","value1");
        db.write("subsub","value2");
        int numValues = db.scan("sub", [&](const QByteArray &key, const QByteArray &value) -> bool {
            return true;
        });

        QCOMPARE(numValues, 1);
    }

    //Ensure we don't retrieve a key that is greater than the current key. We only want equal keys.
    void testKeyRange()
    {
        Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite);
        auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite);
        auto db = transaction.openDatabase("test", nullptr, true);
        db.write("sub1","value1");
        int numValues = db.scan("sub", [&](const QByteArray &key, const QByteArray &value) -> bool {
            return true;
        });

        QCOMPARE(numValues, 0);
    }
};

QTEST_MAIN(StorageTest)
#include "storagetest.moc"