summaryrefslogtreecommitdiffstats
path: root/tests/storagetest.cpp
blob: b3313c0dde52a353945c6a953f36aa2d33d254ec (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
#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)
    {
        Storage storage(testDataPath, dbName, 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) {
                    storage.commitTransaction();
                }
                storage.startTransaction();
            }
            storage.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i));
        }
        storage.commitTransaction();
    }

    bool verify(Storage &storage, int i)
    {
        bool success = true;
        bool keyMatch = true;
        const auto reference = keyPrefix + std::to_string(i);
        storage.read(keyPrefix + std::to_string(i),
            [&keyMatch, &reference](const std::string &value) -> bool {
                if (value != reference) {
                    qDebug() << "Mismatch while reading";
                    keyMatch = false;
                }
                return keyMatch;
            },
            [&success](const Storage::Error &) { success = false; }
            );
        return success && keyMatch;
    }

private Q_SLOTS:
    void initTestCase()
    {
        testDataPath = "./testdb";
        dbName = "test";
    }

    void cleanupTestCase()
    {
        Storage storage(testDataPath, dbName);
        storage.removeFromDisk();
    }

    void testRead()
    {
        const int count = 100;

        populate(count);

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

        Storage storage(testDataPath, dbName);
        storage.removeFromDisk();
    }

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

        //ensure we can scan for values
        {
            int hit = 0;
            Storage store(testDataPath, dbName);
            store.scan("", [&](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool {
                if (std::string(static_cast<char*>(keyValue), keySize) == "key50") {
                    hit++;
                }
                return true;
            });
            QCOMPARE(hit, 1);
        }

        //ensure we can read a single value
        {
            int hit = 0;
            bool foundInvalidValue = false;
            Storage store(testDataPath, dbName);
            store.scan("key50", [&](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool {
                if (std::string(static_cast<char*>(keyValue), keySize) != "key50") {
                    foundInvalidValue = true;
                }
                hit++;
                return true;
            });
            QVERIFY(!foundInvalidValue);
            QCOMPARE(hit, 1);
        }

        Storage storage(testDataPath, dbName);
        storage.removeFromDisk();
    }

    void testConcurrentRead()
    {
        const int count = 10000;

        populate(count);

        //Try to concurrently read
        QList<QFuture<void> > futures;
        const int concurrencyLevel = 4;
        for (int num = 0; num < concurrencyLevel; num++) {
            futures << QtConcurrent::run([this, count](){
                Storage storage(testDataPath, dbName);
                for (int i = 0; i < count; i++) {
                    if (!verify(storage, i)) {
                        qWarning() << "invalid value";
                        break;
                    }
                }
            });
        }
        for(auto future : futures) {
            future.waitForFinished();
        }

        Storage storage(testDataPath, dbName);
        storage.removeFromDisk();
    }
};

QTEST_MAIN(StorageTest)
#include "storagetest.moc"