diff options
Diffstat (limited to 'store/test')
-rw-r--r-- | store/test/storagetest.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/store/test/storagetest.cpp b/store/test/storagetest.cpp new file mode 100644 index 0000000..dba4f6c --- /dev/null +++ b/store/test/storagetest.cpp | |||
@@ -0,0 +1,113 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include <iostream> | ||
4 | |||
5 | #include <QDebug> | ||
6 | #include <QString> | ||
7 | #include <QtConcurrent/QtConcurrentRun> | ||
8 | |||
9 | #include "store/database.h" | ||
10 | |||
11 | class StorageTest : public QObject | ||
12 | { | ||
13 | Q_OBJECT | ||
14 | private: | ||
15 | //This should point to a directory on disk and not a ramdisk (since we're measuring performance) | ||
16 | QString testDataPath; | ||
17 | QString dbName; | ||
18 | const char *keyPrefix = "key"; | ||
19 | |||
20 | void populate(int count) | ||
21 | { | ||
22 | Database db(testDataPath, dbName); | ||
23 | for (int i = 0; i < count; i++) { | ||
24 | //This should perhaps become an implementation detail of the db? | ||
25 | if (i % 10000 == 0) { | ||
26 | if (i > 0) { | ||
27 | db.commitTransaction(); | ||
28 | } | ||
29 | db.startTransaction(); | ||
30 | } | ||
31 | db.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i)); | ||
32 | } | ||
33 | db.commitTransaction(); | ||
34 | } | ||
35 | |||
36 | bool verify(Database &db, int i) | ||
37 | { | ||
38 | bool error = false; | ||
39 | const auto reference = keyPrefix + std::to_string(i); | ||
40 | if(!db.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) { | ||
41 | if (value != reference) { | ||
42 | qDebug() << "Mismatch while reading"; | ||
43 | error = true; | ||
44 | } | ||
45 | })) { | ||
46 | return false; | ||
47 | } | ||
48 | return !error; | ||
49 | } | ||
50 | |||
51 | private Q_SLOTS: | ||
52 | void initTestCase() | ||
53 | { | ||
54 | testDataPath = "./testdb"; | ||
55 | dbName = "test"; | ||
56 | } | ||
57 | |||
58 | void cleanupTestCase() | ||
59 | { | ||
60 | Database db(testDataPath, dbName); | ||
61 | db.removeFromDisk(); | ||
62 | } | ||
63 | |||
64 | |||
65 | void testRead() | ||
66 | { | ||
67 | const int count = 100; | ||
68 | |||
69 | populate(count); | ||
70 | |||
71 | //ensure we can read everything back correctly | ||
72 | { | ||
73 | Database db(testDataPath, dbName); | ||
74 | for (int i = 0; i < count; i++) { | ||
75 | QVERIFY(verify(db, i)); | ||
76 | } | ||
77 | } | ||
78 | |||
79 | Database db(testDataPath, dbName); | ||
80 | db.removeFromDisk(); | ||
81 | } | ||
82 | |||
83 | void testConcurrentRead() | ||
84 | { | ||
85 | const int count = 10000; | ||
86 | |||
87 | populate(count); | ||
88 | |||
89 | //Try to concurrently read | ||
90 | QList<QFuture<void> > futures; | ||
91 | const int concurrencyLevel = 4; | ||
92 | for (int num = 0; num < concurrencyLevel; num++) { | ||
93 | futures << QtConcurrent::run([this, count](){ | ||
94 | Database db(testDataPath, dbName); | ||
95 | for (int i = 0; i < count; i++) { | ||
96 | if (!verify(db, i)) { | ||
97 | qWarning() << "invalid value"; | ||
98 | break; | ||
99 | } | ||
100 | } | ||
101 | }); | ||
102 | } | ||
103 | for(auto future : futures) { | ||
104 | future.waitForFinished(); | ||
105 | } | ||
106 | |||
107 | Database db(testDataPath, dbName); | ||
108 | db.removeFromDisk(); | ||
109 | } | ||
110 | }; | ||
111 | |||
112 | QTEST_MAIN(StorageTest) | ||
113 | #include "storagetest.moc" | ||