diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-04-15 19:11:59 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-04-15 20:38:26 -0700 |
commit | ac25d32a094f83ed51d5854c9f234b10eedbeac0 (patch) | |
tree | 39c9e7ef53e7bd0c4518784ae78436a6ab19b615 | |
parent | 5fa0512be289dd5a6e55d515352f9f8fb93a1b5e (diff) | |
download | qtlocation-mapboxgl-ac25d32a094f83ed51d5854c9f234b10eedbeac0.tar.gz |
Convert storage tests to RunLoop
-rw-r--r-- | test/storage/database.cpp | 248 |
1 files changed, 133 insertions, 115 deletions
diff --git a/test/storage/database.cpp b/test/storage/database.cpp index 5498dd3892..4a9cc3e161 100644 --- a/test/storage/database.cpp +++ b/test/storage/database.cpp @@ -1,36 +1,14 @@ #include "storage.hpp" #include "../fixtures/fixture_log_observer.hpp" -#include <future> - #include <mbgl/storage/sqlite_cache.hpp> #include <mbgl/storage/resource.hpp> #include <mbgl/storage/response.hpp> #include <mbgl/util/io.hpp> +#include <mbgl/util/run_loop.hpp> #include <sqlite3.h> -class ScopedTest { -public: - ScopedTest(std::function<void()> destructor_) : destructor(destructor_) {} - ScopedTest() {} - - void finish() { - promise.set_value(); - } - - ~ScopedTest() { - promise.get_future().get(); - if (destructor) { - destructor(); - } - } - -private: - const std::function<void()> destructor; - std::promise<void> promise; -}; - TEST_F(Storage, DatabaseDoesNotExist) { using namespace mbgl; @@ -38,15 +16,19 @@ TEST_F(Storage, DatabaseDoesNotExist) { SQLiteCache cache("test/fixtures/404/cache.db"); - ScopedTest test([&] { - auto observer = Log::removeObserver(); - EXPECT_EQ(1ul, dynamic_cast<FixtureLogObserver*>(observer.get())->count({ EventSeverity::Error, Event::Database, 14, "unable to open database file" })); - }); + util::RunLoop loop; - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_EQ(nullptr, res.get()); - test.finish(); + loop.invoke([&] { + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_EQ(nullptr, res.get()); + loop.stop(); + }); }); + + loop.run(); + + auto observer = Log::removeObserver(); + EXPECT_EQ(1ul, dynamic_cast<FixtureLogObserver*>(observer.get())->count({ EventSeverity::Error, Event::Database, 14, "unable to open database file" })); } void createDir(const char* name) { @@ -83,14 +65,18 @@ TEST_F(Storage, DatabaseCreate) { SQLiteCache cache("test/fixtures/database/cache.db"); - ScopedTest test([&] { - Log::removeObserver(); - }); + util::RunLoop loop; - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_EQ(nullptr, res.get()); - test.finish(); + loop.invoke([&] { + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_EQ(nullptr, res.get()); + loop.stop(); + }); }); + + loop.run(); + + Log::removeObserver(); } class FileLock { @@ -143,17 +129,20 @@ TEST_F(Storage, DatabaseLockedRead) { SQLiteCache cache("test/fixtures/database/locked.db"); - std::promise<void> promise; - { // First request should fail. Log::setObserver(util::make_unique<FixtureLogObserver>()); - promise = {}; - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_EQ(nullptr, res.get()); - promise.set_value(); + + util::RunLoop loop; + + loop.invoke([&] { + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_EQ(nullptr, res.get()); + loop.stop(); + }); }); - promise.get_future().get(); + + loop.run(); // Make sure that we got a few "database locked" errors auto observer = Log::removeObserver(); @@ -167,13 +156,17 @@ TEST_F(Storage, DatabaseLockedRead) { { // First, try getting a file (the cache value should not exist). Log::setObserver(util::make_unique<FixtureLogObserver>()); - promise = {}; - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_EQ(nullptr, res.get()); - promise.set_value(); + util::RunLoop loop; + + loop.invoke([&] { + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_EQ(nullptr, res.get()); + loop.stop(); + }); }); - promise.get_future().get(); + + loop.run(); // Make sure that we got a no errors Log::removeObserver(); @@ -192,19 +185,22 @@ TEST_F(Storage, DatabaseLockedWrite) { SQLiteCache cache("test/fixtures/database/locked.db"); - std::promise<void> promise; - { // Adds a file (which should fail). Log::setObserver(util::make_unique<FixtureLogObserver>()); - promise = {}; - auto response = std::make_shared<Response>(); - cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_EQ(nullptr, res.get()); - promise.set_value(); + + util::RunLoop loop; + + loop.invoke([&] { + auto response = std::make_shared<Response>(); + cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_EQ(nullptr, res.get()); + loop.stop(); + }); }); - promise.get_future().get(); + + loop.run(); auto observer = Log::removeObserver(); auto flo = dynamic_cast<FixtureLogObserver*>(observer.get()); @@ -217,17 +213,21 @@ TEST_F(Storage, DatabaseLockedWrite) { { // Then, set a file and obtain it again. Log::setObserver(util::make_unique<FixtureLogObserver>()); - promise = {}; - auto response = std::make_shared<Response>(); - response->data = "Demo"; - cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_NE(nullptr, res.get()); - EXPECT_EQ("Demo", res->data); - promise.set_value(); + util::RunLoop loop; + + loop.invoke([&] { + auto response = std::make_shared<Response>(); + response->data = "Demo"; + cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_NE(nullptr, res.get()); + EXPECT_EQ("Demo", res->data); + loop.stop(); + }); }); - promise.get_future().get(); + + loop.run(); // Make sure that we got a no errors Log::removeObserver(); @@ -249,20 +249,23 @@ TEST_F(Storage, DatabaseLockedRefresh) { // Then, lock the file and try again. FileLock guard("test/fixtures/database/locked.db"); - std::promise<void> promise; - { // Adds a file. Log::setObserver(util::make_unique<FixtureLogObserver>()); - promise = {}; - auto response = std::make_shared<Response>(); - response->data = "Demo"; - cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_EQ(nullptr, res.get()); - promise.set_value(); + + util::RunLoop loop; + + loop.invoke([&] { + auto response = std::make_shared<Response>(); + response->data = "Demo"; + cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_EQ(nullptr, res.get()); + loop.stop(); + }); }); - promise.get_future().get(); + + loop.run(); auto observer = Log::removeObserver(); auto flo = dynamic_cast<FixtureLogObserver*>(observer.get()); @@ -272,16 +275,20 @@ TEST_F(Storage, DatabaseLockedRefresh) { { // Then, try to refresh it. Log::setObserver(util::make_unique<FixtureLogObserver>()); - promise = {}; - auto response = std::make_shared<Response>(); - response->data = "Demo"; - cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Refresh); - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_EQ(nullptr, res.get()); - promise.set_value(); + util::RunLoop loop; + + loop.invoke([&] { + auto response = std::make_shared<Response>(); + response->data = "Demo"; + cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Refresh); + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_EQ(nullptr, res.get()); + loop.stop(); + }); }); - promise.get_future().get(); + + loop.run(); // Make sure that we got the right errors. auto observer = Log::removeObserver(); @@ -301,21 +308,24 @@ TEST_F(Storage, DatabaseDeleted) { SQLiteCache cache("test/fixtures/database/locked.db"); - std::promise<void> promise; - { // Adds a file. Log::setObserver(util::make_unique<FixtureLogObserver>()); - promise = {}; - auto response = std::make_shared<Response>(); - response->data = "Demo"; - cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_NE(nullptr, res.get()); - EXPECT_EQ("Demo", res->data); - promise.set_value(); + + util::RunLoop loop; + + loop.invoke([&] { + auto response = std::make_shared<Response>(); + response->data = "Demo"; + cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_NE(nullptr, res.get()); + EXPECT_EQ("Demo", res->data); + loop.stop(); + }); }); - promise.get_future().get(); + + loop.run(); Log::removeObserver(); } @@ -325,16 +335,21 @@ TEST_F(Storage, DatabaseDeleted) { { // Adds a file. Log::setObserver(util::make_unique<FixtureLogObserver>()); - promise = {}; - auto response = std::make_shared<Response>(); - response->data = "Demo"; - cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_NE(nullptr, res.get()); - EXPECT_EQ("Demo", res->data); - promise.set_value(); + + util::RunLoop loop; + + loop.invoke([&] { + auto response = std::make_shared<Response>(); + response->data = "Demo"; + cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_NE(nullptr, res.get()); + EXPECT_EQ("Demo", res->data); + loop.stop(); + }); }); - promise.get_future().get(); + + loop.run(); auto observer = Log::removeObserver(); auto flo = dynamic_cast<FixtureLogObserver*>(observer.get()); @@ -354,21 +369,24 @@ TEST_F(Storage, DatabaseInvalid) { SQLiteCache cache("test/fixtures/database/invalid.db"); - std::promise<void> promise; - { // Adds a file. Log::setObserver(util::make_unique<FixtureLogObserver>()); - promise = {}; - auto response = std::make_shared<Response>(); - response->data = "Demo"; - cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); - cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { - EXPECT_NE(nullptr, res.get()); - EXPECT_EQ("Demo", res->data); - promise.set_value(); + + util::RunLoop loop; + + loop.invoke([&] { + auto response = std::make_shared<Response>(); + response->data = "Demo"; + cache.put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); + cache.get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_NE(nullptr, res.get()); + EXPECT_EQ("Demo", res->data); + loop.stop(); + }); }); - promise.get_future().get(); + + loop.run(); auto observer = Log::removeObserver(); auto flo = dynamic_cast<FixtureLogObserver*>(observer.get()); |