diff options
33 files changed, 195 insertions, 205 deletions
@@ -53,6 +53,7 @@ Xcode/mbgl: config/$(HOST).gypi styles/styles SMCalloutView Makefile/test: test/test.gyp config/$(HOST).gypi styles/styles SMCalloutView deps/run_gyp test/test.gyp $(CONFIG_$(HOST)) $(LIBS_$(HOST)) --generator-output=./build/$(HOST) -f make +.PHONY: test test: Makefile/test $(MAKE) -C build/$(HOST) BUILDTYPE=$(BUILDTYPE) test diff --git a/android/cpp/native_map_view.cpp b/android/cpp/native_map_view.cpp index b51e5cbc67..39a777bff2 100644 --- a/android/cpp/native_map_view.cpp +++ b/android/cpp/native_map_view.cpp @@ -58,8 +58,8 @@ void log_gl_string(GLenum name, const char *label) { NativeMapView::NativeMapView(JNIEnv *env, jobject obj_) : mbgl::View(*this), fileCache(mbgl::android::cachePath + "/mbgl-cache.db"), - fileSource(fileCache), - map(*this, *fileSource) { + fileSource(&fileCache), + map(*this, fileSource) { mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::NativeMapView"); assert(env != nullptr); @@ -145,7 +145,7 @@ void NativeMapView::notify() { mbgl::Map &NativeMapView::getMap() { return map; } -mbgl::DefaultFileSource &NativeMapView::getFileSource() { return *fileSource; } +mbgl::DefaultFileSource &NativeMapView::getFileSource() { return fileSource; } bool NativeMapView::inEmulator() { // Detect if we are in emulator diff --git a/bin/render.cpp b/bin/render.cpp index 879bf840a2..01f6929092 100644 --- a/bin/render.cpp +++ b/bin/render.cpp @@ -2,7 +2,6 @@ #include <mbgl/util/image.hpp> #include <mbgl/util/std.hpp> #include <mbgl/util/io.hpp> -#include <mbgl/util/thread.hpp> #include <mbgl/platform/default/headless_view.hpp> #include <mbgl/platform/default/headless_display.hpp> @@ -67,8 +66,8 @@ int main(int argc, char *argv[]) { using namespace mbgl; - mbgl::util::Thread<mbgl::SQLiteCache> cache(cache_file); - mbgl::DefaultFileSource fileSource(cache); + mbgl::SQLiteCache cache(cache_file); + mbgl::DefaultFileSource fileSource(&cache); // Try to load the token from the environment. if (!token.size()) { diff --git a/include/mbgl/android/native_map_view.hpp b/include/mbgl/android/native_map_view.hpp index 817b6bc8bb..21784f5315 100644 --- a/include/mbgl/android/native_map_view.hpp +++ b/include/mbgl/android/native_map_view.hpp @@ -4,7 +4,6 @@ #include <mbgl/map/map.hpp> #include <mbgl/map/view.hpp> #include <mbgl/util/noncopyable.hpp> -#include <mbgl/util/thread.hpp> #include <mbgl/storage/default/sqlite_cache.hpp> #include <mbgl/storage/default_file_source.hpp> @@ -62,8 +61,8 @@ private: ANativeWindow *window = nullptr; - mbgl::util::Thread<mbgl::SQLiteCache> fileCache; - mbgl::util::Thread<mbgl::DefaultFileSource> fileSource; + mbgl::SQLiteCache fileCache; + mbgl::DefaultFileSource fileSource; mbgl::Map map; EGLDisplay display = EGL_NO_DISPLAY; diff --git a/include/mbgl/storage/default/asset_request.hpp b/include/mbgl/storage/default/asset_request.hpp index 48f402fefe..48d421c3be 100644 --- a/include/mbgl/storage/default/asset_request.hpp +++ b/include/mbgl/storage/default/asset_request.hpp @@ -7,7 +7,7 @@ namespace mbgl { class AssetRequest : public SharedRequestBase { public: - AssetRequest(DefaultFileSource *source, const Resource &resource); + AssetRequest(DefaultFileSource::Impl *source, const Resource &resource); void start(uv_loop_t *loop, std::shared_ptr<const Response> response = nullptr); void cancel(); diff --git a/include/mbgl/storage/default/http_request.hpp b/include/mbgl/storage/default/http_request.hpp index 1cd38a8281..54e9a77ef0 100644 --- a/include/mbgl/storage/default/http_request.hpp +++ b/include/mbgl/storage/default/http_request.hpp @@ -7,7 +7,7 @@ namespace mbgl { class HTTPRequest : public SharedRequestBase { public: - HTTPRequest(DefaultFileSource *source, const Resource &resource); + HTTPRequest(DefaultFileSource::Impl *source, const Resource &resource); void start(uv_loop_t *loop, std::shared_ptr<const Response> response = nullptr); void cancel(); diff --git a/include/mbgl/storage/default/shared_request_base.hpp b/include/mbgl/storage/default/shared_request_base.hpp index 9c5f87b33d..efc78f603d 100644 --- a/include/mbgl/storage/default/shared_request_base.hpp +++ b/include/mbgl/storage/default/shared_request_base.hpp @@ -3,7 +3,7 @@ #include <mbgl/storage/resource.hpp> #include <mbgl/storage/file_cache.hpp> -#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/default_file_source_impl.hpp> #include <mbgl/storage/default/request.hpp> #include <mbgl/util/util.hpp> #include <mbgl/util/noncopyable.hpp> @@ -26,7 +26,7 @@ protected: MBGL_STORE_THREAD(tid) public: - SharedRequestBase(DefaultFileSource *source_, const Resource &resource_) + SharedRequestBase(DefaultFileSource::Impl *source_, const Resource &resource_) : resource(resource_), source(source_) {} virtual void start(uv_loop_t *loop, std::shared_ptr<const Response> response = nullptr) = 0; @@ -95,7 +95,7 @@ public: const Resource resource; protected: - DefaultFileSource *source = nullptr; + DefaultFileSource::Impl *source = nullptr; private: std::set<Request *> observers; diff --git a/include/mbgl/storage/default/sqlite_cache.hpp b/include/mbgl/storage/default/sqlite_cache.hpp index 899cc8a892..db672b222b 100644 --- a/include/mbgl/storage/default/sqlite_cache.hpp +++ b/include/mbgl/storage/default/sqlite_cache.hpp @@ -2,41 +2,27 @@ #define MBGL_STORAGE_DEFAULT_SQLITE_CACHE #include <mbgl/storage/file_cache.hpp> -#include <mbgl/util/run_loop.hpp> #include <string> -namespace mapbox { namespace sqlite { class Database; class Statement; } } - namespace mbgl { -class SQLiteCache : public FileCache, protected util::RunLoop { - friend class util::Thread<SQLiteCache>; +namespace util { +template <typename T> class Thread; +} -private: +class SQLiteCache : public FileCache { +public: SQLiteCache(const std::string &path = ":memory:"); - ~SQLiteCache(); + ~SQLiteCache() override; -public: // FileCache API void get(const Resource &resource, Callback callback) override; void put(const Resource &resource, std::shared_ptr<const Response> response, Hint hint) override; private: - void createDatabase(); - void createSchema(); - - void processGet(const Resource& resource, Callback callback); - void processPut(const Resource& resource, std::shared_ptr<const Response> response); - void processRefresh(const Resource& resource, int64_t expires); - -private: - const std::string path; - std::unique_ptr<::mapbox::sqlite::Database> db; - std::unique_ptr<::mapbox::sqlite::Statement> getStmt; - std::unique_ptr<::mapbox::sqlite::Statement> putStmt; - std::unique_ptr<::mapbox::sqlite::Statement> refreshStmt; - bool schema = false; + class Thread; + const std::unique_ptr<util::Thread<Thread>> thread; }; } diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index f6028217ea..372dee909b 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -5,22 +5,13 @@ #include <mbgl/storage/file_cache.hpp> #include <mbgl/util/run_loop.hpp> -#include <set> -#include <unordered_map> - namespace mbgl { -class SharedRequestBase; - -class DefaultFileSource : public FileSource, protected util::RunLoop { - friend class util::Thread<DefaultFileSource>; - friend class SharedRequestBase; - -private: +class DefaultFileSource : public FileSource { +public: DefaultFileSource(FileCache *cache, const std::string &root = ""); ~DefaultFileSource() override; -public: // FileSource API Request *request(const Resource &resource, uv_loop_t *loop, const Environment &env, Callback callback) override; @@ -29,22 +20,10 @@ public: void abort(const Environment &env) override; -private: - void notify(SharedRequestBase *sharedRequest, const std::set<Request *> &observers, - std::shared_ptr<const Response> response, FileCache::Hint hint); - SharedRequestBase *find(const Resource &resource); - - void processAdd(Request* request); - void processCancel(Request* request); - void processResult(const Resource& resource, std::shared_ptr<const Response> response); - void processAbort(const Environment& env); - public: - const std::string assetRoot; - + class Impl; private: - std::unordered_map<Resource, SharedRequestBase *, Resource::Hash> pending; - FileCache *cache = nullptr; + const std::unique_ptr<util::Thread<Impl>> thread; }; } diff --git a/linux/main.cpp b/linux/main.cpp index d91aa7dec4..4d5474c02f 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -7,7 +7,6 @@ #include <mbgl/platform/default/glfw_view.hpp> #include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/default/sqlite_cache.hpp> -#include <mbgl/util/thread.hpp> #include <signal.h> #include <getopt.h> @@ -66,9 +65,9 @@ int main(int argc, char *argv[]) { view = mbgl::util::make_unique<GLFWView>(); - mbgl::util::Thread<mbgl::SQLiteCache> cache("/tmp/mbgl-cache.db"); - mbgl::util::Thread<mbgl::DefaultFileSource> fileSource(cache); - mbgl::Map map(*view, *fileSource); + mbgl::SQLiteCache cache("/tmp/mbgl-cache.db"); + mbgl::DefaultFileSource fileSource(&cache); + mbgl::Map map(*view, fileSource); // Load settings mbgl::Settings_JSON settings; diff --git a/macosx/main.mm b/macosx/main.mm index d0f3282f8a..087544f7c4 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -6,7 +6,6 @@ #include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/default/sqlite_cache.hpp> #include <mbgl/storage/network_status.hpp> -#include <mbgl/util/thread.hpp> #include <mbgl/util/geo.hpp> @@ -104,9 +103,9 @@ const std::string &defaultCacheDatabase() { int main() { GLFWView view; - mbgl::util::Thread<mbgl::SQLiteCache> cache(defaultCacheDatabase()); - mbgl::util::Thread<mbgl::DefaultFileSource> fileSource(cache); - mbgl::Map map(view, *fileSource); + mbgl::SQLiteCache cache(defaultCacheDatabase()); + mbgl::DefaultFileSource fileSource(&cache); + mbgl::Map map(view, fileSource); URLHandler *handler = [[URLHandler alloc] init]; [handler setMap:&map]; diff --git a/platform/darwin/http_request_nsurl.mm b/platform/darwin/http_request_nsurl.mm index 773f83e5d7..bd28234767 100644 --- a/platform/darwin/http_request_nsurl.mm +++ b/platform/darwin/http_request_nsurl.mm @@ -397,7 +397,7 @@ void HTTPRequestImpl::restart(uv_timer_t *timer, int) { // ------------------------------------------------------------------------------------------------- -HTTPRequest::HTTPRequest(DefaultFileSource *source, const Resource &resource) +HTTPRequest::HTTPRequest(DefaultFileSource::Impl *source, const Resource &resource) : SharedRequestBase(source, resource) { } diff --git a/platform/default/asset_request_fs.cpp b/platform/default/asset_request_fs.cpp index 10d5f33a31..d2b07e0a2f 100644 --- a/platform/default/asset_request_fs.cpp +++ b/platform/default/asset_request_fs.cpp @@ -207,7 +207,7 @@ void AssetRequestImpl::cleanup(uv_fs_t *req) { // ------------------------------------------------------------------------------------------------- -AssetRequest::AssetRequest(DefaultFileSource *source_, const Resource &resource_) +AssetRequest::AssetRequest(DefaultFileSource::Impl *source_, const Resource &resource_) : SharedRequestBase(source_, resource_) { assert(algo::starts_with(resource.url, "asset://")); } diff --git a/platform/default/asset_request_zip.cpp b/platform/default/asset_request_zip.cpp index 85ff0aede4..10a73b99a9 100644 --- a/platform/default/asset_request_zip.cpp +++ b/platform/default/asset_request_zip.cpp @@ -275,7 +275,7 @@ void AssetRequestImpl::cancel() { // ------------------------------------------------------------------------------------------------- -AssetRequest::AssetRequest(DefaultFileSource *source_, const Resource &resource_) +AssetRequest::AssetRequest(DefaultFileSource::Impl *source_, const Resource &resource_) : SharedRequestBase(source_, resource_) { assert(algo::starts_with(resource.url, "asset://")); } diff --git a/platform/default/http_request_curl.cpp b/platform/default/http_request_curl.cpp index 707465a3dc..e13ccbfb21 100644 --- a/platform/default/http_request_curl.cpp +++ b/platform/default/http_request_curl.cpp @@ -722,7 +722,7 @@ void HTTPRequestImpl::handleResult(CURLcode code) { // ------------------------------------------------------------------------------------------------- -HTTPRequest::HTTPRequest(DefaultFileSource *source_, const Resource &resource_) +HTTPRequest::HTTPRequest(DefaultFileSource::Impl *source_, const Resource &resource_) : SharedRequestBase(source_, resource_) { } diff --git a/platform/default/sqlite_cache.cpp b/platform/default/sqlite_cache.cpp index b084a7f2a7..94a8f072ca 100644 --- a/platform/default/sqlite_cache.cpp +++ b/platform/default/sqlite_cache.cpp @@ -4,6 +4,8 @@ #include <mbgl/util/compression.hpp> #include <mbgl/util/io.hpp> +#include <mbgl/util/thread.hpp> +#include <mbgl/util/run_loop.hpp> #include <mbgl/platform/log.hpp> #include "sqlite3.hpp" @@ -11,6 +13,33 @@ namespace mbgl { +class SQLiteCache::Thread : public util::RunLoop { + friend class util::Thread<SQLiteCache::Thread>; + +public: + Thread(const std::string &path = ":memory:"); + ~Thread(); + +public: + void processGet(const Resource& resource, Callback callback); + void processPut(const Resource& resource, std::shared_ptr<const Response> response); + void processRefresh(const Resource& resource, int64_t expires); + +private: + void createDatabase(); + void createSchema(); + +private: + const std::string path; + std::unique_ptr<::mapbox::sqlite::Database> db; + std::unique_ptr<::mapbox::sqlite::Statement> getStmt; + std::unique_ptr<::mapbox::sqlite::Statement> putStmt; + std::unique_ptr<::mapbox::sqlite::Statement> refreshStmt; + bool schema = false; +}; + + + std::string removeAccessTokenFromURL(const std::string &url) { const size_t token_start = url.find("access_token="); // Ensure that token exists, isn't at the front and is preceded by either & or ?. @@ -60,14 +89,19 @@ std::string unifyMapboxURLs(const std::string &url) { using namespace mapbox::sqlite; -SQLiteCache::SQLiteCache(const std::string& path_) +SQLiteCache::SQLiteCache(const std::string& path_) : thread(util::make_unique<util::Thread<Thread>>(path_)) { +} + +SQLiteCache::~SQLiteCache() = default; + +SQLiteCache::Thread::Thread(const std::string& path_) : path(path_) { #ifdef __APPLE__ pthread_setname_np("SQLite Cache"); #endif } -SQLiteCache::~SQLiteCache() { +SQLiteCache::Thread::~Thread() { // Deleting these SQLite objects may result in exceptions, but we're in a destructor, so we // can't throw anything. try { @@ -80,11 +114,11 @@ SQLiteCache::~SQLiteCache() { } } -void SQLiteCache::createDatabase() { +void SQLiteCache::Thread::createDatabase() { db = util::make_unique<Database>(path.c_str(), ReadWrite | Create); } -void SQLiteCache::createSchema() { +void SQLiteCache::Thread::createSchema() { constexpr const char *const sql = "" "CREATE TABLE IF NOT EXISTS `http_cache` (" " `url` TEXT PRIMARY KEY NOT NULL," @@ -127,10 +161,10 @@ void SQLiteCache::get(const Resource &resource, Callback callback) { // Will try to load the URL from the SQLite database and call the callback when done. // Note that the callback is probably going to invoked from another thread, so the caller // must make sure that it can run in that thread. - invoke(std::bind(&SQLiteCache::processGet, this, resource, callback)); + (*thread)->invoke([=] { (*thread)->processGet(resource, callback); }); } -void SQLiteCache::processGet(const Resource &resource, Callback callback) { +void SQLiteCache::Thread::processGet(const Resource &resource, Callback callback) { try { // This is called in the SQLite event loop. if (!db) { @@ -179,13 +213,13 @@ void SQLiteCache::put(const Resource &resource, std::shared_ptr<const Response> // storing a new response or updating the currently stored response, potentially setting a new // expiry date. if (hint == Hint::Full) { - invoke(std::bind(&SQLiteCache::processPut, this, resource, response)); + (*thread)->invoke([=] { (*thread)->processPut(resource, response); }); } else if (hint == Hint::Refresh) { - invoke(std::bind(&SQLiteCache::processRefresh, this, resource, response->expires)); + (*thread)->invoke([=] { (*thread)->processRefresh(resource, response->expires); }); } } -void SQLiteCache::processPut(const Resource& resource, std::shared_ptr<const Response> response) { +void SQLiteCache::Thread::processPut(const Resource& resource, std::shared_ptr<const Response> response) { try { if (!db) { createDatabase(); @@ -234,7 +268,7 @@ void SQLiteCache::processPut(const Resource& resource, std::shared_ptr<const Res } } -void SQLiteCache::processRefresh(const Resource& resource, int64_t expires) { +void SQLiteCache::Thread::processRefresh(const Resource& resource, int64_t expires) { try { if (!db) { createDatabase(); diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index 8281b6ec50..f70f9f348f 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -14,7 +14,6 @@ #include <mbgl/storage/default/sqlite_cache.hpp> #include <mbgl/storage/network_status.hpp> #include <mbgl/util/geo.hpp> -#include <mbgl/util/thread.hpp> #import "MGLTypes.h" #import "NSString+MGLAdditions.h" @@ -112,8 +111,8 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) mbgl::Map *mbglMap = nullptr; MBGLView *mbglView = nullptr; -mbgl::util::Thread<mbgl::SQLiteCache> *mbglFileCache = nullptr; -mbgl::util::Thread<mbgl::DefaultFileSource> *mbglFileSource = nullptr; +mbgl::SQLiteCache *mbglFileCache = nullptr; +mbgl::DefaultFileSource *mbglFileSource = nullptr; - (instancetype)initWithFrame:(CGRect)frame { @@ -270,9 +269,9 @@ mbgl::util::Thread<mbgl::DefaultFileSource> *mbglFileSource = nullptr; // setup mbgl map // mbglView = new MBGLView(self); - mbglFileCache = new mbgl::util::Thread<mbgl::SQLiteCache>(defaultCacheDatabase()); - mbglFileSource = new mbgl::util::Thread<mbgl::DefaultFileSource>(*mbglFileCache); - mbglMap = new mbgl::Map(*mbglView, **mbglFileSource); + mbglFileCache = new mbgl::SQLiteCache(defaultCacheDatabase()); + mbglFileSource = new mbgl::DefaultFileSource(mbglFileCache); + mbglMap = new mbgl::Map(*mbglView, *mbglFileSource); mbglView->resize(self.bounds.size.width, self.bounds.size.height, _glView.contentScaleFactor, _glView.drawableWidth, _glView.drawableHeight); // Notify map object when network reachability status changes. diff --git a/src/mbgl/storage/default_file_source.cpp b/src/mbgl/storage/default_file_source.cpp index 71b3be8506..ee4042459b 100644 --- a/src/mbgl/storage/default_file_source.cpp +++ b/src/mbgl/storage/default_file_source.cpp @@ -1,4 +1,4 @@ -#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/default_file_source_impl.hpp> #include <mbgl/storage/default/request.hpp> #include <mbgl/storage/default/asset_request.hpp> #include <mbgl/storage/default/http_request.hpp> @@ -8,6 +8,8 @@ #include <mbgl/util/uv_detail.hpp> #include <mbgl/util/chrono.hpp> +#include <mbgl/util/thread.hpp> +#include <mbgl/util/run_loop.hpp> #include <mbgl/platform/log.hpp> #pragma GCC diagnostic push @@ -18,7 +20,6 @@ #include <boost/algorithm/string.hpp> #pragma GCC diagnostic pop -#include <thread> #include <algorithm> #include <cassert> @@ -27,18 +28,22 @@ namespace algo = boost::algorithm; namespace mbgl { -DefaultFileSource::DefaultFileSource(FileCache* cache_, const std::string& root) +DefaultFileSource::Impl::Impl(FileCache* cache_, const std::string& root) : assetRoot(root.empty() ? platform::assetRoot() : root), cache(cache_) { #ifdef __APPLE__ pthread_setname_np("FileSource"); #endif } +DefaultFileSource::DefaultFileSource(FileCache* cache, const std::string& root) + : thread(util::make_unique<util::Thread<Impl>>(cache, root)) { +} + DefaultFileSource::~DefaultFileSource() { MBGL_VERIFY_THREAD(tid); } -SharedRequestBase *DefaultFileSource::find(const Resource &resource) { +SharedRequestBase *DefaultFileSource::Impl::find(const Resource &resource) { // We're using a set of pointers here instead of a map between url and SharedRequestBase because // we need to find the requests both by pointer and by URL. Given that the number of requests // is generally very small (typically < 10 at a time), hashing by URL incurs too much overhead @@ -58,7 +63,7 @@ Request* DefaultFileSource::request(const Resource& resource, // This function can be called from any thread. Make sure we're executing the actual call in the // file source loop by sending it over the queue. - invoke([this, req] { processAdd(req); }); + (*thread)->invoke([=] { (*thread)->processAdd(req); }); return req; } @@ -72,14 +77,14 @@ void DefaultFileSource::cancel(Request *req) { // This function can be called from any thread. Make sure we're executing the actual call in the // file source loop by sending it over the queue. - invoke([this, req] { processCancel(req); }); + (*thread)->invoke([=] { (*thread)->processCancel(req); }); } void DefaultFileSource::abort(const Environment &env) { - invoke([this, &env] { processAbort(env); }); + (*thread)->invoke([=, &env] { (*thread)->processAbort(env); }); } -void DefaultFileSource::processAdd(Request* req) { +void DefaultFileSource::Impl::processAdd(Request* req) { const Resource &resource = req->resource; // We're adding a new Request. @@ -111,7 +116,7 @@ void DefaultFileSource::processAdd(Request* req) { sharedRequest->subscribe(req); } -void DefaultFileSource::processCancel(Request* req) { +void DefaultFileSource::Impl::processCancel(Request* req) { SharedRequestBase *sharedRequest = find(req->resource); if (sharedRequest) { // If the number of dependent requests of the SharedRequestBase drops to zero, the @@ -128,7 +133,7 @@ void DefaultFileSource::processCancel(Request* req) { req->destruct(); } -void DefaultFileSource::processResult(const Resource& resource, std::shared_ptr<const Response> response) { +void DefaultFileSource::Impl::processResult(const Resource& resource, std::shared_ptr<const Response> response) { SharedRequestBase *sharedRequest = find(resource); if (sharedRequest) { if (response) { @@ -155,7 +160,7 @@ void DefaultFileSource::processResult(const Resource& resource, std::shared_ptr< } // Aborts all requests that are part of the current environment. -void DefaultFileSource::processAbort(const Environment& env) { +void DefaultFileSource::Impl::processAbort(const Environment& env) { // Construct a cancellation response. auto res = util::make_unique<Response>(); res->status = Response::Error; @@ -182,7 +187,7 @@ void DefaultFileSource::processAbort(const Environment& env) { }); } -void DefaultFileSource::notify(SharedRequestBase *sharedRequest, +void DefaultFileSource::Impl::notify(SharedRequestBase *sharedRequest, const std::set<Request *> &observers, std::shared_ptr<const Response> response, FileCache::Hint hint) { // First, remove the request, since it might be destructed at any point now. diff --git a/test/headless/headless.cpp b/test/headless/headless.cpp index 76c1264333..f4c8accf61 100644 --- a/test/headless/headless.cpp +++ b/test/headless/headless.cpp @@ -4,7 +4,6 @@ #include <mbgl/map/map.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/std.hpp> -#include <mbgl/util/thread.hpp> #include <mbgl/util/io.hpp> #include <rapidjson/document.h> @@ -140,8 +139,8 @@ TEST_P(HeadlessTest, render) { } HeadlessView view(display); - util::Thread<DefaultFileSource> fileSource(nullptr); - Map map(view, *fileSource); + DefaultFileSource fileSource(nullptr); + Map map(view, fileSource); map.setClasses(classes); map.setStyleJSON(style, "test/suite"); diff --git a/test/storage/cache_response.cpp b/test/storage/cache_response.cpp index 46083e2f93..e37899a86f 100644 --- a/test/storage/cache_response.cpp +++ b/test/storage/cache_response.cpp @@ -4,20 +4,19 @@ #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; - util::Thread<SQLiteCache> cache(":memory:"); - util::Thread<DefaultFileSource> fs(cache); + SQLiteCache cache(":memory:"); + DefaultFileSource fs(&cache); const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/cache" }; auto &env = *static_cast<const Environment *>(nullptr); - fs->request(resource, uv_default_loop(), env, [&](const Response &res) { + fs.request(resource, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); EXPECT_EQ("Response 1", res.data); EXPECT_LT(0, res.expires); @@ -25,7 +24,7 @@ TEST_F(Storage, CacheResponse) { EXPECT_EQ("", res.etag); EXPECT_EQ("", res.message); - fs->request(resource, uv_default_loop(), env, [&, res](const Response &res2) { + fs.request(resource, uv_default_loop(), env, [&, res](const Response &res2) { EXPECT_EQ(res.status, res2.status); EXPECT_EQ(res.data, res2.data); EXPECT_EQ(res.expires, res2.expires); diff --git a/test/storage/cache_revalidate.cpp b/test/storage/cache_revalidate.cpp index 901c151f14..bd32042b94 100644 --- a/test/storage/cache_revalidate.cpp +++ b/test/storage/cache_revalidate.cpp @@ -4,7 +4,6 @@ #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) @@ -13,13 +12,13 @@ TEST_F(Storage, CacheRevalidate) { using namespace mbgl; - util::Thread<SQLiteCache> cache(":memory:"); - util::Thread<DefaultFileSource> fs(cache); + SQLiteCache cache(":memory:"); + DefaultFileSource fs(&cache); auto &env = *static_cast<const Environment *>(nullptr); const Resource revalidateSame { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" }; - fs->request(revalidateSame, uv_default_loop(), env, [&](const Response &res) { + fs.request(revalidateSame, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); EXPECT_EQ("Response", res.data); EXPECT_EQ(0, res.expires); @@ -27,7 +26,7 @@ TEST_F(Storage, CacheRevalidate) { EXPECT_EQ("snowfall", res.etag); EXPECT_EQ("", res.message); - fs->request(revalidateSame, uv_default_loop(), env, [&, res](const Response &res2) { + fs.request(revalidateSame, uv_default_loop(), env, [&, res](const Response &res2) { EXPECT_EQ(Response::Successful, res2.status); EXPECT_EQ("Response", res2.data); // We use this to indicate that a 304 reply came back. @@ -43,7 +42,7 @@ TEST_F(Storage, CacheRevalidate) { const Resource revalidateModified{ Resource::Unknown, "http://127.0.0.1:3000/revalidate-modified" }; - fs->request(revalidateModified, uv_default_loop(), env, [&](const Response &res) { + fs.request(revalidateModified, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); EXPECT_EQ("Response", res.data); EXPECT_EQ(0, res.expires); @@ -51,7 +50,7 @@ TEST_F(Storage, CacheRevalidate) { EXPECT_EQ("", res.etag); EXPECT_EQ("", res.message); - fs->request(revalidateModified, uv_default_loop(), env, [&, res](const Response &res2) { + fs.request(revalidateModified, uv_default_loop(), env, [&, res](const Response &res2) { EXPECT_EQ(Response::Successful, res2.status); EXPECT_EQ("Response", res2.data); // We use this to indicate that a 304 reply came back. @@ -65,7 +64,7 @@ TEST_F(Storage, CacheRevalidate) { }); const Resource revalidateEtag { Resource::Unknown, "http://127.0.0.1:3000/revalidate-etag" }; - fs->request(revalidateEtag, uv_default_loop(), env, [&](const Response &res) { + fs.request(revalidateEtag, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); EXPECT_EQ("Response 1", res.data); EXPECT_EQ(0, res.expires); @@ -73,7 +72,7 @@ TEST_F(Storage, CacheRevalidate) { EXPECT_EQ("response-1", res.etag); EXPECT_EQ("", res.message); - fs->request(revalidateEtag, uv_default_loop(), env, [&, res](const Response &res2) { + fs.request(revalidateEtag, uv_default_loop(), env, [&, res](const Response &res2) { EXPECT_EQ(Response::Successful, res2.status); EXPECT_EQ("Response 2", res2.data); EXPECT_EQ(0, res2.expires); diff --git a/test/storage/database.cpp b/test/storage/database.cpp index ffa82ecb98..02b25d696a 100644 --- a/test/storage/database.cpp +++ b/test/storage/database.cpp @@ -7,7 +7,6 @@ #include <mbgl/storage/resource.hpp> #include <mbgl/storage/response.hpp> #include <mbgl/util/io.hpp> -#include <mbgl/util/thread.hpp> #include <sqlite3.h> @@ -37,14 +36,14 @@ TEST_F(Storage, DatabaseDoesNotExist) { Log::setObserver(util::make_unique<FixtureLogObserver>()); + 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::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(); }); @@ -82,13 +81,13 @@ TEST_F(Storage, DatabaseCreate) { Log::setObserver(util::make_unique<FixtureLogObserver>()); + SQLiteCache cache("test/fixtures/database/cache.db"); + ScopedTest test([&] { Log::removeObserver(); }); - 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(); }); @@ -142,7 +141,7 @@ TEST_F(Storage, DatabaseLockedRead) { FileLock guard("test/fixtures/database/locked.db"); - util::Thread<SQLiteCache> cache("test/fixtures/database/locked.db"); + SQLiteCache cache("test/fixtures/database/locked.db"); std::promise<void> promise; @@ -150,7 +149,7 @@ TEST_F(Storage, DatabaseLockedRead) { // First request should fail. Log::setObserver(util::make_unique<FixtureLogObserver>()); promise = {}; - 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()); promise.set_value(); }); @@ -170,7 +169,7 @@ TEST_F(Storage, DatabaseLockedRead) { Log::setObserver(util::make_unique<FixtureLogObserver>()); promise = {}; - 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()); promise.set_value(); }); @@ -191,7 +190,7 @@ TEST_F(Storage, DatabaseLockedWrite) { deleteFile("test/fixtures/database/locked.db"); FileLock guard("test/fixtures/database/locked.db"); - util::Thread<SQLiteCache> cache("test/fixtures/database/locked.db"); + SQLiteCache cache("test/fixtures/database/locked.db"); std::promise<void> promise; @@ -200,8 +199,8 @@ TEST_F(Storage, DatabaseLockedWrite) { 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) { + 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(); }); @@ -222,8 +221,8 @@ TEST_F(Storage, DatabaseLockedWrite) { 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) { + 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(); @@ -245,7 +244,7 @@ TEST_F(Storage, DatabaseLockedRefresh) { createDir("test/fixtures/database"); deleteFile("test/fixtures/database/locked.db"); - util::Thread<SQLiteCache> cache("test/fixtures/database/locked.db"); + SQLiteCache cache("test/fixtures/database/locked.db"); // Then, lock the file and try again. FileLock guard("test/fixtures/database/locked.db"); @@ -258,8 +257,8 @@ TEST_F(Storage, DatabaseLockedRefresh) { 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) { + 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(); }); @@ -277,8 +276,8 @@ TEST_F(Storage, DatabaseLockedRefresh) { 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) { + 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(); }); @@ -300,7 +299,7 @@ TEST_F(Storage, DatabaseDeleted) { createDir("test/fixtures/database"); deleteFile("test/fixtures/database/locked.db"); - util::Thread<SQLiteCache> cache("test/fixtures/database/locked.db"); + SQLiteCache cache("test/fixtures/database/locked.db"); std::promise<void> promise; @@ -310,8 +309,8 @@ TEST_F(Storage, DatabaseDeleted) { 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) { + 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(); @@ -329,8 +328,8 @@ TEST_F(Storage, DatabaseDeleted) { 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) { + 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(); @@ -353,7 +352,7 @@ TEST_F(Storage, DatabaseInvalid) { deleteFile("test/fixtures/database/invalid.db"); writeFile("test/fixtures/database/invalid.db", "this is an invalid file"); - util::Thread<SQLiteCache> cache("test/fixtures/database/invalid.db"); + SQLiteCache cache("test/fixtures/database/invalid.db"); std::promise<void> promise; @@ -363,8 +362,8 @@ TEST_F(Storage, DatabaseInvalid) { 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) { + 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(); diff --git a/test/storage/directory_reading.cpp b/test/storage/directory_reading.cpp index 4264129b22..ccae4177c3 100644 --- a/test/storage/directory_reading.cpp +++ b/test/storage/directory_reading.cpp @@ -3,7 +3,6 @@ #include <uv.h> #include <mbgl/storage/default_file_source.hpp> -#include <mbgl/util/thread.hpp> TEST_F(Storage, AssetReadDirectory) { SCOPED_TEST(ReadDirectory) @@ -11,14 +10,14 @@ TEST_F(Storage, AssetReadDirectory) { using namespace mbgl; #ifdef MBGL_ASSET_ZIP - util::Thread<DefaultFileSource> fs(nullptr, "test/fixtures/storage/assets.zip"); + DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip"); #else - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); #endif auto &env = *static_cast<const Environment *>(nullptr); - fs->request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage" }, uv_default_loop(), + fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage" }, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Error, res.status); EXPECT_EQ(0ul, res.data.size()); diff --git a/test/storage/file_reading.cpp b/test/storage/file_reading.cpp index de834798de..01db75d5c0 100644 --- a/test/storage/file_reading.cpp +++ b/test/storage/file_reading.cpp @@ -4,7 +4,6 @@ #include <mbgl/storage/default_file_source.hpp> #include <mbgl/platform/platform.hpp> -#include <mbgl/util/thread.hpp> TEST_F(Storage, AssetEmptyFile) { SCOPED_TEST(EmptyFile) @@ -12,14 +11,14 @@ TEST_F(Storage, AssetEmptyFile) { using namespace mbgl; #ifdef MBGL_ASSET_ZIP - util::Thread<DefaultFileSource> fs(nullptr, "test/fixtures/storage/assets.zip"); + DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip"); #else - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); #endif auto &env = *static_cast<const Environment *>(nullptr); - fs->request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/empty" }, uv_default_loop(), + fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/empty" }, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); EXPECT_EQ(0ul, res.data.size()); @@ -39,14 +38,14 @@ TEST_F(Storage, AssetNonEmptyFile) { using namespace mbgl; #ifdef MBGL_ASSET_ZIP - util::Thread<DefaultFileSource> fs(nullptr, "test/fixtures/storage/assets.zip"); + DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip"); #else - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); #endif auto &env = *static_cast<const Environment *>(nullptr); - fs->request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/nonempty" }, + fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/nonempty" }, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); EXPECT_EQ(16ul, res.data.size()); @@ -67,14 +66,14 @@ TEST_F(Storage, AssetNonExistentFile) { using namespace mbgl; #ifdef MBGL_ASSET_ZIP - util::Thread<DefaultFileSource> fs(nullptr, "test/fixtures/storage/assets.zip"); + DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip"); #else - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); #endif auto &env = *static_cast<const Environment *>(nullptr); - fs->request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/does_not_exist" }, + fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/does_not_exist" }, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Error, res.status); EXPECT_EQ(0ul, res.data.size()); diff --git a/test/storage/http_cancel.cpp b/test/storage/http_cancel.cpp index 33edbdf7c4..80efb3977b 100644 --- a/test/storage/http_cancel.cpp +++ b/test/storage/http_cancel.cpp @@ -4,7 +4,6 @@ #include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/network_status.hpp> -#include <mbgl/util/thread.hpp> #include <cmath> @@ -13,15 +12,15 @@ TEST_F(Storage, HTTPCancel) { using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); auto req = - fs->request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, uv_default_loop(), env, + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, uv_default_loop(), env, [&](const Response &) { ADD_FAILURE() << "Callback should not be called"; }); - fs->cancel(req); + fs.cancel(req); HTTPCancel.finish(); uv_run(uv_default_loop(), UV_RUN_DEFAULT); @@ -32,15 +31,15 @@ TEST_F(Storage, HTTPCancelMultiple) { using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test" }; - auto req2 = fs->request(resource, uv_default_loop(), env, [&](const Response &) { + auto req2 = fs.request(resource, uv_default_loop(), env, [&](const Response &) { ADD_FAILURE() << "Callback should not be called"; }); - fs->request(resource, uv_default_loop(), env, [&](const Response &res) { + fs.request(resource, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); EXPECT_EQ("Hello World!", res.data); EXPECT_EQ(0, res.expires); @@ -49,7 +48,7 @@ TEST_F(Storage, HTTPCancelMultiple) { EXPECT_EQ("", res.message); HTTPCancelMultiple.finish(); }); - fs->cancel(req2); + fs.cancel(req2); uv_run(uv_default_loop(), UV_RUN_DEFAULT); } diff --git a/test/storage/http_coalescing.cpp b/test/storage/http_coalescing.cpp index 5164117f52..28fa4415b4 100644 --- a/test/storage/http_coalescing.cpp +++ b/test/storage/http_coalescing.cpp @@ -3,7 +3,6 @@ #include <uv.h> #include <mbgl/storage/default_file_source.hpp> -#include <mbgl/util/thread.hpp> TEST_F(Storage, HTTPCoalescing) { SCOPED_TEST(HTTPCoalescing) @@ -13,7 +12,7 @@ TEST_F(Storage, HTTPCoalescing) { using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); @@ -44,7 +43,7 @@ TEST_F(Storage, HTTPCoalescing) { const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test" }; for (int i = 0; i < total; i++) { - fs->request(resource, uv_default_loop(), env, complete); + fs.request(resource, uv_default_loop(), env, complete); } uv_run(uv_default_loop(), UV_RUN_DEFAULT); diff --git a/test/storage/http_environment.cpp b/test/storage/http_environment.cpp index b08a8071c4..41a9d43d5b 100644 --- a/test/storage/http_environment.cpp +++ b/test/storage/http_environment.cpp @@ -4,7 +4,6 @@ #include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/network_status.hpp> -#include <mbgl/util/thread.hpp> #include <cmath> @@ -14,7 +13,7 @@ TEST_F(Storage, HTTPCancelEnvironment) { using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); // Create two fake environment pointers. The FileSource implementation treats these as opaque // pointers and doesn't reach into them. @@ -24,7 +23,7 @@ TEST_F(Storage, HTTPCancelEnvironment) { const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/delayed" }; // Environment 1 - fs->request(resource, uv_default_loop(), env1, [&](const Response &res) { + fs.request(resource, uv_default_loop(), env1, [&](const Response &res) { // This environment gets aborted below. This means the request is marked as failing and // will return an error here. EXPECT_EQ(Response::Error, res.status); @@ -37,7 +36,7 @@ TEST_F(Storage, HTTPCancelEnvironment) { }); // Environment 2 - fs->request(resource, uv_default_loop(), env2, [&](const Response &res) { + fs.request(resource, uv_default_loop(), env2, [&](const Response &res) { // The same request as above, but in a different environment which doesn't get aborted. This // means the request should succeed. EXPECT_EQ(Response::Successful, res.status); @@ -49,7 +48,7 @@ TEST_F(Storage, HTTPCancelEnvironment) { HTTPRetainedEnvironment.finish(); }); - fs->abort(env1); + fs.abort(env1); uv_run(uv_default_loop(), UV_RUN_DEFAULT); } diff --git a/test/storage/http_error.cpp b/test/storage/http_error.cpp index bd5ef9811d..e5728d97b1 100644 --- a/test/storage/http_error.cpp +++ b/test/storage/http_error.cpp @@ -4,7 +4,6 @@ #include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/network_status.hpp> -#include <mbgl/util/thread.hpp> #include <cmath> @@ -21,13 +20,13 @@ TEST_F(Storage, HTTPError) { }, 500, 500); uv_unref((uv_handle_t *)&statusChange); - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); auto start = uv_hrtime(); - fs->request({ Resource::Unknown, "http://127.0.0.1:3000/temporary-error" }, uv_default_loop(), + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/temporary-error" }, uv_default_loop(), env, [&](const Response &res) { const auto duration = double(uv_hrtime() - start) / 1e9; EXPECT_LT(1, duration) << "Backoff timer didn't wait 1 second"; @@ -42,7 +41,7 @@ TEST_F(Storage, HTTPError) { HTTPTemporaryError.finish(); }); - fs->request({ Resource::Unknown, "http://127.0.0.1:3001/" }, uv_default_loop(), env, + fs.request({ Resource::Unknown, "http://127.0.0.1:3001/" }, uv_default_loop(), env, [&](const Response &res) { const auto duration = double(uv_hrtime() - start) / 1e9; // 1.5 seconds == 4 retries, with a 500ms timeout (see above). diff --git a/test/storage/http_header_parsing.cpp b/test/storage/http_header_parsing.cpp index df1fb1fc68..1561660b6f 100644 --- a/test/storage/http_header_parsing.cpp +++ b/test/storage/http_header_parsing.cpp @@ -4,7 +4,6 @@ #include <mbgl/storage/default_file_source.hpp> #include <mbgl/util/chrono.hpp> -#include <mbgl/util/thread.hpp> #include <cmath> @@ -14,11 +13,11 @@ TEST_F(Storage, HTTPHeaderParsing) { using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); - fs->request({ Resource::Unknown, + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test?modified=1420794326&expires=1420797926&etag=foo" }, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); @@ -33,7 +32,7 @@ TEST_F(Storage, HTTPHeaderParsing) { int64_t now = std::chrono::duration_cast<std::chrono::seconds>( SystemClock::now().time_since_epoch()).count(); - fs->request({ Resource::Unknown, "http://127.0.0.1:3000/test?cachecontrol=max-age=120" }, + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test?cachecontrol=max-age=120" }, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); EXPECT_EQ("Hello World!", res.data); diff --git a/test/storage/http_load.cpp b/test/storage/http_load.cpp index b037867233..c4069eba3e 100644 --- a/test/storage/http_load.cpp +++ b/test/storage/http_load.cpp @@ -3,14 +3,13 @@ #include <uv.h> #include <mbgl/storage/default_file_source.hpp> -#include <mbgl/util/thread.hpp> TEST_F(Storage, HTTPLoad) { SCOPED_TEST(HTTPLoad) using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); @@ -20,7 +19,7 @@ TEST_F(Storage, HTTPLoad) { std::function<void()> req = [&]() { const auto current = number++; - fs->request({ Resource::Unknown, + fs.request({ Resource::Unknown, std::string("http://127.0.0.1:3000/load/") + std::to_string(current) }, uv_default_loop(), env, [&, current](const Response &res) { EXPECT_EQ(Response::Successful, res.status); diff --git a/test/storage/http_noloop.cpp b/test/storage/http_noloop.cpp index 0f75c106fe..cd4ebeb2c4 100644 --- a/test/storage/http_noloop.cpp +++ b/test/storage/http_noloop.cpp @@ -4,14 +4,13 @@ #include <mbgl/storage/default_file_source.hpp> #include <mbgl/util/uv.hpp> -#include <mbgl/util/thread.hpp> TEST_F(Storage, HTTPNoLoop) { SCOPED_TEST(HTTPNoLoop) using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); @@ -23,7 +22,7 @@ TEST_F(Storage, HTTPNoLoop) { uv::close(as); }); - fs->request({ Resource::Unknown, "http://127.0.0.1:3000/temporary-error" }, nullptr, env, + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/temporary-error" }, nullptr, env, [&](const Response &res) { EXPECT_NE(uv_thread_self(), mainThread) << "Response was called in the same thread"; EXPECT_EQ(Response::Successful, res.status); diff --git a/test/storage/http_other_loop.cpp b/test/storage/http_other_loop.cpp index 11fa2f324a..9ad673cf79 100644 --- a/test/storage/http_other_loop.cpp +++ b/test/storage/http_other_loop.cpp @@ -3,7 +3,6 @@ #include <uv.h> #include <mbgl/storage/default_file_source.hpp> -#include <mbgl/util/thread.hpp> TEST_F(Storage, HTTPOtherLoop) { SCOPED_TEST(HTTPOtherLoop) @@ -11,11 +10,11 @@ TEST_F(Storage, HTTPOtherLoop) { using namespace mbgl; // This file source launches a separate thread to do the processing. - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); - fs->request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, uv_default_loop(), env, + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(Response::Successful, res.status); EXPECT_EQ("Hello World!", res.data); diff --git a/test/storage/http_reading.cpp b/test/storage/http_reading.cpp index 1ba34801cd..350a8eaa4b 100644 --- a/test/storage/http_reading.cpp +++ b/test/storage/http_reading.cpp @@ -3,7 +3,8 @@ #include <uv.h> #include <mbgl/storage/default_file_source.hpp> -#include <mbgl/util/thread.hpp> + +#include <future> TEST_F(Storage, HTTPReading) { SCOPED_TEST(HTTPTest) @@ -11,13 +12,13 @@ TEST_F(Storage, HTTPReading) { using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); const auto mainThread = uv_thread_self(); - fs->request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, uv_default_loop(), env, + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(uv_thread_self(), mainThread); EXPECT_EQ(Response::Successful, res.status); @@ -29,7 +30,7 @@ TEST_F(Storage, HTTPReading) { HTTPTest.finish(); }); - fs->request({ Resource::Unknown, "http://127.0.0.1:3000/doesnotexist" }, uv_default_loop(), + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/doesnotexist" }, uv_default_loop(), env, [&](const Response &res) { EXPECT_EQ(uv_thread_self(), mainThread); EXPECT_EQ(Response::Error, res.status); @@ -48,11 +49,11 @@ TEST_F(Storage, HTTPNoCallback) { using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); auto &env = *static_cast<const Environment *>(nullptr); - fs->request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, uv_default_loop(), env, + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, uv_default_loop(), env, nullptr); uv_run(uv_default_loop(), UV_RUN_DEFAULT); @@ -60,18 +61,21 @@ TEST_F(Storage, HTTPNoCallback) { HTTPTest.finish(); } -TEST_F(Storage, HTTPNoCallbackNoLoop) { - SCOPED_TEST(HTTPTest) - +TEST_F(Storage, HTTPCallbackNotOnLoop) { using namespace mbgl; - util::Thread<DefaultFileSource> fs(nullptr); + DefaultFileSource fs(nullptr); + + SCOPED_TEST(HTTPTest) auto &env = *static_cast<const Environment *>(nullptr); - fs->request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, env, nullptr); + std::promise<void> promise; + fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" }, env, [&] (const Response &) { + promise.set_value(); + }); - uv_run(uv_default_loop(), UV_RUN_DEFAULT); + promise.get_future().get(); HTTPTest.finish(); } |