summaryrefslogtreecommitdiff
path: root/test/storage
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-04-10 15:36:32 +0200
committerKonstantin Käfer <mail@kkaefer.com>2015-04-10 15:36:32 +0200
commitbba3d9838a512c75a63f5eb9263913ec96011e8d (patch)
treeafe1462c6e134fffae55fceaae45a8920e63644b /test/storage
parent8eafe36932c3c5407a305268bc5b62e70ad8b8b0 (diff)
downloadqtlocation-mapboxgl-bba3d9838a512c75a63f5eb9263913ec96011e8d.tar.gz
add Thread<T> and RunLoop
Thread<T> is a generic thread management object that starts a thread, creates an object T in that thread and calls its .start() function. When the Thread<T> object is destructed (which must happen in the thread that created it), it'll call the object's .stop() function, and wait for thread termination. The .stop() function must somehow get the thread to terminate. Note that object T's constructor/destructor/start()/stop() must be protected/private, and Thread<T> must be declared as a friend class. All public functions may be called from any thread and are exposed through operator->(). RunLoop encapsulates a libuv runloop and has a facility of dispatching function objects to the thread. It can be used either as a worker thread class by itself, or it can be derived from. This commit converts SQLiteCache to derive from RunLoop and removes the custom code used previously for managing the cache thread.
Diffstat (limited to 'test/storage')
-rw-r--r--test/storage/cache_response.cpp5
-rw-r--r--test/storage/cache_revalidate.cpp5
-rw-r--r--test/storage/database.cpp35
3 files changed, 17 insertions, 28 deletions
diff --git a/test/storage/cache_response.cpp b/test/storage/cache_response.cpp
index ac0dc4c565..f5d7c4bf3c 100644
--- a/test/storage/cache_response.cpp
+++ b/test/storage/cache_response.cpp
@@ -4,14 +4,15 @@
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/default/sqlite_cache.hpp>
+#include <mbgl/util/thread.hpp>
TEST_F(Storage, CacheResponse) {
SCOPED_TEST(CacheResponse);
using namespace mbgl;
- SQLiteCache cache(":memory:");
- DefaultFileSource fs(&cache, uv_default_loop());
+ util::Thread<SQLiteCache> cache(":memory:");
+ DefaultFileSource fs(cache, uv_default_loop());
const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/cache" };
auto &env = *static_cast<const Environment *>(nullptr);
diff --git a/test/storage/cache_revalidate.cpp b/test/storage/cache_revalidate.cpp
index bd32042b94..29f25eb775 100644
--- a/test/storage/cache_revalidate.cpp
+++ b/test/storage/cache_revalidate.cpp
@@ -4,6 +4,7 @@
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/default/sqlite_cache.hpp>
+#include <mbgl/util/thread.hpp>
TEST_F(Storage, CacheRevalidate) {
SCOPED_TEST(CacheRevalidateSame)
@@ -12,8 +13,8 @@ TEST_F(Storage, CacheRevalidate) {
using namespace mbgl;
- SQLiteCache cache(":memory:");
- DefaultFileSource fs(&cache);
+ util::Thread<SQLiteCache> cache(":memory:");
+ DefaultFileSource fs(cache);
auto &env = *static_cast<const Environment *>(nullptr);
diff --git a/test/storage/database.cpp b/test/storage/database.cpp
index 1a2b618a57..ffa82ecb98 100644
--- a/test/storage/database.cpp
+++ b/test/storage/database.cpp
@@ -7,6 +7,7 @@
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/io.hpp>
+#include <mbgl/util/thread.hpp>
#include <sqlite3.h>
@@ -41,9 +42,9 @@ TEST_F(Storage, DatabaseDoesNotExist) {
EXPECT_EQ(1ul, dynamic_cast<FixtureLogObserver*>(observer.get())->count({ EventSeverity::Error, Event::Database, 14, "unable to open database file" }));
});
- SQLiteCache cache("test/fixtures/404/cache.db");
+ util::Thread<SQLiteCache> cache("test/fixtures/404/cache.db");
- cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) {
+ cache->get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) {
EXPECT_EQ(nullptr, res.get());
test.finish();
});
@@ -85,9 +86,9 @@ TEST_F(Storage, DatabaseCreate) {
Log::removeObserver();
});
- SQLiteCache cache("test/fixtures/database/cache.db");
+ util::Thread<SQLiteCache> cache("test/fixtures/database/cache.db");
- cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) {
+ cache->get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) {
EXPECT_EQ(nullptr, res.get());
test.finish();
});
@@ -140,7 +141,8 @@ TEST_F(Storage, DatabaseLockedRead) {
deleteFile("test/fixtures/database/locked.db");
FileLock guard("test/fixtures/database/locked.db");
- auto cache = util::make_unique<SQLiteCache>("test/fixtures/database/locked.db");
+
+ util::Thread<SQLiteCache> cache("test/fixtures/database/locked.db");
std::promise<void> promise;
@@ -177,9 +179,6 @@ TEST_F(Storage, DatabaseLockedRead) {
// Make sure that we got a no errors
Log::removeObserver();
}
-
- // Explicitly delete the Cache now.
- cache.reset();
}
@@ -192,7 +191,7 @@ TEST_F(Storage, DatabaseLockedWrite) {
deleteFile("test/fixtures/database/locked.db");
FileLock guard("test/fixtures/database/locked.db");
- auto cache = util::make_unique<SQLiteCache>("test/fixtures/database/locked.db");
+ util::Thread<SQLiteCache> cache("test/fixtures/database/locked.db");
std::promise<void> promise;
@@ -234,9 +233,6 @@ TEST_F(Storage, DatabaseLockedWrite) {
// Make sure that we got a no errors
Log::removeObserver();
}
-
- // Explicitly delete the Cache now.
- cache.reset();
}
@@ -249,7 +245,7 @@ TEST_F(Storage, DatabaseLockedRefresh) {
createDir("test/fixtures/database");
deleteFile("test/fixtures/database/locked.db");
- auto cache = util::make_unique<SQLiteCache>("test/fixtures/database/locked.db");
+ util::Thread<SQLiteCache> cache("test/fixtures/database/locked.db");
// Then, lock the file and try again.
FileLock guard("test/fixtures/database/locked.db");
@@ -293,9 +289,6 @@ TEST_F(Storage, DatabaseLockedRefresh) {
auto flo = dynamic_cast<FixtureLogObserver*>(observer.get());
EXPECT_EQ(4ul, flo->count({ EventSeverity::Error, Event::Database, 5, "database is locked" }));
}
-
- // Explicitly delete the Cache now.
- cache.reset();
}
@@ -307,7 +300,7 @@ TEST_F(Storage, DatabaseDeleted) {
createDir("test/fixtures/database");
deleteFile("test/fixtures/database/locked.db");
- auto cache = util::make_unique<SQLiteCache>("test/fixtures/database/locked.db");
+ util::Thread<SQLiteCache> cache("test/fixtures/database/locked.db");
std::promise<void> promise;
@@ -348,9 +341,6 @@ TEST_F(Storage, DatabaseDeleted) {
auto flo = dynamic_cast<FixtureLogObserver*>(observer.get());
EXPECT_EQ(1ul, flo->count({ EventSeverity::Error, Event::Database, 8, "attempt to write a readonly database" }));
}
-
- // Explicitly delete the Cache now.
- cache.reset();
}
@@ -363,7 +353,7 @@ TEST_F(Storage, DatabaseInvalid) {
deleteFile("test/fixtures/database/invalid.db");
writeFile("test/fixtures/database/invalid.db", "this is an invalid file");
- auto cache = util::make_unique<SQLiteCache>("test/fixtures/database/invalid.db");
+ util::Thread<SQLiteCache> cache("test/fixtures/database/invalid.db");
std::promise<void> promise;
@@ -385,7 +375,4 @@ TEST_F(Storage, DatabaseInvalid) {
auto flo = dynamic_cast<FixtureLogObserver*>(observer.get());
EXPECT_EQ(1ul, flo->count({ EventSeverity::Warning, Event::Database, -1, "Trashing invalid database" }));
}
-
- // Explicitly delete the Cache now.
- cache.reset();
}