From 0c1400c7f0cf2f545a6cd7347314c1158fbfa36f Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 5 Dec 2014 09:33:35 +0100 Subject: mv storagetest.cpp to the right location --- tests/storagetest.cpp | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 tests/storagetest.cpp (limited to 'tests/storagetest.cpp') diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp new file mode 100644 index 0000000..1b105af --- /dev/null +++ b/tests/storagetest.cpp @@ -0,0 +1,111 @@ +#include + +#include + +#include +#include +#include + +#include "store/database.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) + { + Database db(testDataPath, dbName); + for (int i = 0; i < count; i++) { + //This should perhaps become an implementation detail of the db? + if (i % 10000 == 0) { + if (i > 0) { + db.commitTransaction(); + } + db.startTransaction(); + } + db.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i)); + } + db.commitTransaction(); + } + + bool verify(Database &db, int i) + { + bool error = false; + const auto reference = keyPrefix + std::to_string(i); + db.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) { + if (value != reference) { + qDebug() << "Mismatch while reading"; + error = true; + } + }); + return !error; + } + +private Q_SLOTS: + void initTestCase() + { + testDataPath = "./testdb"; + dbName = "test"; + } + + void cleanupTestCase() + { + Database db(testDataPath, dbName); + db.removeFromDisk(); + } + + + void testRead() + { + const int count = 100; + + populate(count); + + //ensure we can read everything back correctly + { + Database db(testDataPath, dbName); + for (int i = 0; i < count; i++) { + QVERIFY(verify(db, i)); + } + } + + Database db(testDataPath, dbName); + db.removeFromDisk(); + } + + void testConcurrentRead() + { + const int count = 10000; + + populate(count); + + //Try to concurrently read + QList > futures; + const int concurrencyLevel = 4; + for (int num = 0; num < concurrencyLevel; num++) { + futures << QtConcurrent::run([this, count](){ + Database db(testDataPath, dbName); + for (int i = 0; i < count; i++) { + if (!verify(db, i)) { + qWarning() << "invalid value"; + break; + } + } + }); + } + for(auto future : futures) { + future.waitForFinished(); + } + + Database db(testDataPath, dbName); + db.removeFromDisk(); + } +}; + +QTEST_MAIN(StorageTest) +#include "storagetest.moc" -- cgit v1.2.3 From 351a66b5fb1c8659bff8ea20d60f5a6d2d3263ad Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 5 Dec 2014 09:46:53 +0100 Subject: make read return a bool on success not happy with this API, but we need to discuss the whole read thing anyways --- tests/storagetest.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'tests/storagetest.cpp') diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp index 1b105af..2b2805d 100644 --- a/tests/storagetest.cpp +++ b/tests/storagetest.cpp @@ -6,7 +6,7 @@ #include #include -#include "store/database.h" +#include "common/storage.h" class StorageTest : public QObject { @@ -19,31 +19,32 @@ private: void populate(int count) { - Database db(testDataPath, dbName); + Storage storage(testDataPath, dbName); for (int i = 0; i < count; i++) { //This should perhaps become an implementation detail of the db? if (i % 10000 == 0) { if (i > 0) { - db.commitTransaction(); + storage.commitTransaction(); } - db.startTransaction(); + storage.startTransaction(); } - db.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i)); + storage.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i)); } - db.commitTransaction(); + storage.commitTransaction(); } - bool verify(Database &db, int i) + bool verify(Storage &storage, int i) { - bool error = false; + bool success = true; + bool keyMatch = true; const auto reference = keyPrefix + std::to_string(i); - db.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) { + success = storage.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) { if (value != reference) { qDebug() << "Mismatch while reading"; - error = true; + keyMatch = false; } }); - return !error; + return succes && keyMatch; } private Q_SLOTS: -- cgit v1.2.3