[tests] Remove libuv dependency from the tests

This commit is contained in:
Thiago Marcos P. Santos
2015-11-13 17:47:22 +02:00
parent ae3f1c79fa
commit cb06526b10
23 changed files with 110 additions and 145 deletions

View File

@@ -3,16 +3,11 @@
#include <mbgl/util/io.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/timer.hpp>
#include <algorithm>
#include <unordered_map>
namespace {
const uint64_t timeout = 1000000;
}
namespace mbgl {
class MockFileRequest : public FileRequest {
@@ -36,8 +31,8 @@ public:
class MockFileSource::Impl {
public:
Impl(Type type, const std::string& match)
: type_(type), match_(match), timer_(util::RunLoop::getLoop()) {
timer_.start(timeout, timeout, [this] { dispatchPendingRequests(); });
: type_(type), match_(match) {
timer_.start(std::chrono::milliseconds(1000), std::chrono::milliseconds(1000), [this] { dispatchPendingRequests(); });
timer_.unref();
}
@@ -63,7 +58,7 @@ private:
Type type_;
std::string match_;
std::unordered_map<FileRequest*, std::pair<Resource, Callback>> pendingRequests_;
uv::timer timer_;
util::Timer timer_;
std::function<void(void)> requestEnqueuedCallback_;
};

View File

@@ -66,7 +66,7 @@ public:
TEST(Thread, invoke) {
const std::thread::id tid = std::this_thread::get_id();
RunLoop loop(uv_default_loop());
RunLoop loop;
std::vector<std::unique_ptr<mbgl::WorkRequest>> requests;
loop.invoke([&] {
@@ -110,7 +110,7 @@ TEST(Thread, invoke) {
test.clear();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST(Thread, context) {
@@ -122,7 +122,7 @@ TEST(Thread, context) {
const std::thread::id tid = std::this_thread::get_id();
RunLoop loop(uv_default_loop());
RunLoop loop;
std::vector<std::unique_ptr<mbgl::WorkRequest>> requests;
loop.invoke([&] {
@@ -134,7 +134,7 @@ TEST(Thread, context) {
}));
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
class TestWorker {
@@ -148,7 +148,7 @@ public:
};
TEST(Thread, ExecutesAfter) {
RunLoop loop(uv_default_loop());
RunLoop loop;
Thread<TestWorker> thread({"Test", ThreadType::Map, ThreadPriority::Regular});
bool didWork = false;
@@ -161,14 +161,15 @@ TEST(Thread, ExecutesAfter) {
didWork = true;
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
EXPECT_TRUE(didWork);
EXPECT_TRUE(didAfter);
}
TEST(Thread, WorkRequestDeletionWaitsForWorkToComplete) {
RunLoop loop(uv_default_loop());
RunLoop loop;
Thread<TestWorker> thread({"Test", ThreadType::Map, ThreadPriority::Regular});
std::promise<void> started;
@@ -186,7 +187,7 @@ TEST(Thread, WorkRequestDeletionWaitsForWorkToComplete) {
}
TEST(Thread, WorkRequestDeletionCancelsAfter) {
RunLoop loop(uv_default_loop());
RunLoop loop;
Thread<TestWorker> thread({"Test", ThreadType::Map, ThreadPriority::Regular});
std::promise<void> started;
@@ -200,12 +201,12 @@ TEST(Thread, WorkRequestDeletionCancelsAfter) {
started.get_future().get();
request.reset();
uv_run(uv_default_loop(), UV_RUN_ONCE);
loop.runOnce();
EXPECT_FALSE(didAfter);
}
TEST(Thread, WorkRequestDeletionCancelsImmediately) {
RunLoop loop(uv_default_loop());
RunLoop loop;
Thread<TestWorker> thread({"Test", ThreadType::Map, ThreadPriority::Regular});
std::promise<void> started;

View File

@@ -23,7 +23,7 @@ private:
};
TEST(WorkQueue, push) {
RunLoop loop(uv_default_loop());
RunLoop loop;
WorkQueue queue;
Thread<TestThread> thread({"Test", ThreadType::Map, ThreadPriority::Regular}, &queue);
@@ -43,11 +43,11 @@ TEST(WorkQueue, push) {
thread.invoke(&TestThread::send, endTest);
thread.invoke(&TestThread::send, endTest);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST(WorkQueue, cancel) {
RunLoop loop(uv_default_loop());
RunLoop loop;
WorkQueue queue;

View File

@@ -4,6 +4,7 @@
#include <mbgl/sprite/sprite_store.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/thread.hpp>
@@ -186,9 +187,9 @@ private:
class SpriteTest : public testing::Test {
protected:
void runTest(const SpriteParams& params, FileSource* fileSource, SpriteTestCallback callback) {
util::RunLoop loop(uv_default_loop());
util::RunLoop loop;
async_ = std::make_unique<uv::async>(loop.get(), [&] { loop.stop(); });
async_ = std::make_unique<util::AsyncTask>([&] { loop.stop(); });
async_->unref();
const util::ThreadContext context = {"Map", util::ThreadType::Map, util::ThreadPriority::Regular};
@@ -196,7 +197,7 @@ protected:
util::Thread<SpriteThread> tester(context, fileSource, callback);
tester.invoke(&SpriteThread::loadSprite, params);
uv_run(loop.get(), UV_RUN_DEFAULT);
loop.run();
tester.invoke(&SpriteThread::unloadSprite);
}
@@ -206,7 +207,7 @@ protected:
}
private:
std::unique_ptr<uv::async> async_;
std::unique_ptr<util::AsyncTask> async_;
};
TEST_F(SpriteTest, LoadingSuccess) {

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
#include <mbgl/util/chrono.hpp>
@@ -12,9 +10,9 @@ TEST_F(Storage, CacheResponse) {
using namespace mbgl;
util::RunLoop loop;
SQLiteCache cache(":memory:");
DefaultFileSource fs(&cache);
util::RunLoop loop(uv_default_loop());
const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/cache" };
Response response;
@@ -50,5 +48,5 @@ TEST_F(Storage, CacheResponse) {
});
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
#include <mbgl/util/chrono.hpp>
@@ -12,9 +10,9 @@ TEST_F(Storage, CacheRevalidateSame) {
using namespace mbgl;
util::RunLoop loop;
SQLiteCache cache(":memory:");
DefaultFileSource fs(&cache);
util::RunLoop loop(uv_default_loop());
const Resource revalidateSame { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" };
std::unique_ptr<FileRequest> req1;
@@ -64,7 +62,7 @@ TEST_F(Storage, CacheRevalidateSame) {
});
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, CacheRevalidateModified) {
@@ -72,9 +70,9 @@ TEST_F(Storage, CacheRevalidateModified) {
using namespace mbgl;
util::RunLoop loop;
SQLiteCache cache(":memory:");
DefaultFileSource fs(&cache);
util::RunLoop loop(uv_default_loop());
const Resource revalidateModified{ Resource::Unknown,
"http://127.0.0.1:3000/revalidate-modified" };
@@ -124,7 +122,7 @@ TEST_F(Storage, CacheRevalidateModified) {
});
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, CacheRevalidateEtag) {
@@ -132,9 +130,9 @@ TEST_F(Storage, CacheRevalidateEtag) {
using namespace mbgl;
util::RunLoop loop;
SQLiteCache cache(":memory:");
DefaultFileSource fs(&cache);
util::RunLoop loop(uv_default_loop());
const Resource revalidateEtag { Resource::Unknown, "http://127.0.0.1:3000/revalidate-etag" };
std::unique_ptr<FileRequest> req1;
@@ -182,5 +180,5 @@ TEST_F(Storage, CacheRevalidateEtag) {
});
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>
@@ -11,14 +9,14 @@ TEST_F(Storage, AssetReadDirectory) {
using namespace mbgl;
util::RunLoop loop;
#ifdef MBGL_ASSET_ZIP
DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip");
#else
DefaultFileSource fs(nullptr);
#endif
util::RunLoop loop(uv_default_loop());
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage" }, [&](Response res) {
req.reset();
ASSERT_NE(nullptr, res.error);
@@ -38,5 +36,5 @@ TEST_F(Storage, AssetReadDirectory) {
ReadDirectory.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/util/chrono.hpp>
@@ -12,14 +10,14 @@ TEST_F(Storage, AssetEmptyFile) {
using namespace mbgl;
util::RunLoop loop;
#ifdef MBGL_ASSET_ZIP
DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip");
#else
DefaultFileSource fs(nullptr);
#endif
util::RunLoop loop(uv_default_loop());
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/empty" }, [&](Response res) {
req.reset();
EXPECT_EQ(nullptr, res.error);
@@ -33,7 +31,7 @@ TEST_F(Storage, AssetEmptyFile) {
EmptyFile.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, AssetNonEmptyFile) {
@@ -41,14 +39,14 @@ TEST_F(Storage, AssetNonEmptyFile) {
using namespace mbgl;
util::RunLoop loop;
#ifdef MBGL_ASSET_ZIP
DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip");
#else
DefaultFileSource fs(nullptr);
#endif
util::RunLoop loop(uv_default_loop());
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/nonempty" }, [&](Response res) {
req.reset();
EXPECT_EQ(nullptr, res.error);
@@ -64,7 +62,7 @@ TEST_F(Storage, AssetNonEmptyFile) {
NonEmptyFile.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, AssetNonExistentFile) {
@@ -72,14 +70,14 @@ TEST_F(Storage, AssetNonExistentFile) {
using namespace mbgl;
util::RunLoop loop;
#ifdef MBGL_ASSET_ZIP
DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip");
#else
DefaultFileSource fs(nullptr);
#endif
util::RunLoop loop(uv_default_loop());
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/does_not_exist" }, [&](Response res) {
req.reset();
ASSERT_NE(nullptr, res.error);
@@ -98,5 +96,5 @@ TEST_F(Storage, AssetNonExistentFile) {
NonExistentFile.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/util/chrono.hpp>
@@ -14,8 +12,8 @@ TEST_F(Storage, HTTPCancel) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
auto req =
fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" },
@@ -24,7 +22,7 @@ TEST_F(Storage, HTTPCancel) {
req.reset();
HTTPCancel.finish();
uv_run(uv_default_loop(), UV_RUN_ONCE);
loop.runOnce();
}
TEST_F(Storage, HTTPCancelMultiple) {
@@ -32,8 +30,8 @@ TEST_F(Storage, HTTPCancelMultiple) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test" };
@@ -54,5 +52,5 @@ TEST_F(Storage, HTTPCancelMultiple) {
});
req2.reset();
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>
@@ -14,8 +12,8 @@ TEST_F(Storage, HTTPCoalescing) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
static const Response *reference = nullptr;
@@ -52,7 +50,7 @@ TEST_F(Storage, HTTPCoalescing) {
});
}
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, HTTPMultiple) {
@@ -60,8 +58,8 @@ TEST_F(Storage, HTTPMultiple) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test?expires=2147483647" };
std::unique_ptr<FileRequest> req1;
@@ -96,7 +94,7 @@ TEST_F(Storage, HTTPMultiple) {
});
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
// Tests that we get stale responses from previous requests when requesting the same thing again.
@@ -105,8 +103,8 @@ TEST_F(Storage, HTTPStale) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
int updates = 0;
int stale = 0;
@@ -153,7 +151,7 @@ TEST_F(Storage, HTTPStale) {
});
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
EXPECT_EQ(1, stale);
EXPECT_EQ(1, updates);

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/util/chrono.hpp>
@@ -14,16 +12,16 @@ TEST_F(Storage, HTTPTemporaryError) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const auto start = uv_hrtime();
const auto start = Clock::now();
std::unique_ptr<FileRequest> req1 = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/temporary-error" }, [&](Response res) {
static int counter = 0;
switch (counter++) {
case 0: {
const auto duration = double(uv_hrtime() - start) / 1e9;
const auto duration = std::chrono::duration<const double>(Clock::now() - start).count();
EXPECT_GT(0.2, duration) << "Initial error request took too long";
ASSERT_NE(nullptr, res.error);
EXPECT_EQ(Response::Error::Reason::Server, res.error->reason);
@@ -37,7 +35,7 @@ TEST_F(Storage, HTTPTemporaryError) {
} break;
case 1: {
req1.reset();
const auto duration = double(uv_hrtime() - start) / 1e9;
const auto duration = std::chrono::duration<const double>(Clock::now() - start).count();
EXPECT_LT(0.99, duration) << "Backoff timer didn't wait 1 second";
EXPECT_GT(1.2, duration) << "Backoff timer fired too late";
EXPECT_EQ(nullptr, res.error);
@@ -53,7 +51,7 @@ TEST_F(Storage, HTTPTemporaryError) {
}
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, HTTPConnectionError) {
@@ -61,15 +59,15 @@ TEST_F(Storage, HTTPConnectionError) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const auto start = uv_hrtime();
const auto start = Clock::now();
std::unique_ptr<FileRequest> req2 = fs.request({ Resource::Unknown, "http://127.0.0.1:3001/" }, [&](Response res) {
static int counter = 0;
static int wait = 0;
const auto duration = double(uv_hrtime() - start) / 1e9;
const auto duration = std::chrono::duration<const double>(Clock::now() - start).count();
EXPECT_LT(wait - 0.01, duration) << "Backoff timer didn't wait 1 second";
EXPECT_GT(wait + 0.2, duration) << "Backoff timer fired too late";
ASSERT_NE(nullptr, res.error);
@@ -100,5 +98,5 @@ TEST_F(Storage, HTTPConnectionError) {
counter++;
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>
@@ -13,8 +11,8 @@ TEST_F(Storage, HTTPExpiresParsing) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
std::unique_ptr<FileRequest> req1 = fs.request({ Resource::Unknown,
"http://127.0.0.1:3000/test?modified=1420794326&expires=1420797926&etag=foo" },
@@ -31,7 +29,7 @@ TEST_F(Storage, HTTPExpiresParsing) {
HTTPExpiresTest.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, HTTPCacheControlParsing) {
@@ -39,8 +37,8 @@ TEST_F(Storage, HTTPCacheControlParsing) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const Seconds now = toSeconds(SystemClock::now());
@@ -58,5 +56,5 @@ TEST_F(Storage, HTTPCacheControlParsing) {
HTTPCacheControlTest.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/sqlite_cache.hpp>
#include <mbgl/util/chrono.hpp>
@@ -23,9 +21,9 @@ TEST_F(Storage, HTTPIssue1369) {
using namespace mbgl;
util::RunLoop loop;
SQLiteCache cache;
DefaultFileSource fs(&cache);
util::RunLoop loop(uv_default_loop());
const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test" };
@@ -46,5 +44,5 @@ TEST_F(Storage, HTTPIssue1369) {
HTTPIssue1369.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>
@@ -11,8 +9,8 @@ TEST_F(Storage, HTTPLoad) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const int concurrency = 50;
const int max = 10000;
@@ -48,5 +46,5 @@ TEST_F(Storage, HTTPLoad) {
req(i);
}
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>
@@ -12,8 +10,8 @@ TEST_F(Storage, HTTPOtherLoop) {
using namespace mbgl;
// This file source launches a separate thread to do the processing.
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" },
[&](Response res) {
@@ -29,5 +27,5 @@ TEST_F(Storage, HTTPOtherLoop) {
HTTPOtherLoop.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,11 +1,10 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/thread_context.hpp>
#include <future>
@@ -14,15 +13,13 @@ TEST_F(Storage, HTTPTest) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const auto mainThread = uv_thread_self();
std::unique_ptr<FileRequest> req1 = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" },
[&](Response res) {
req1.reset();
EXPECT_EQ(uv_thread_self(), mainThread);
EXPECT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Main));
EXPECT_EQ(nullptr, res.error);
EXPECT_EQ(false, res.stale);
ASSERT_TRUE(res.data.get());
@@ -34,7 +31,7 @@ TEST_F(Storage, HTTPTest) {
HTTPTest.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, HTTP404) {
@@ -42,15 +39,13 @@ TEST_F(Storage, HTTP404) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const auto mainThread = uv_thread_self();
std::unique_ptr<FileRequest> req2 = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/doesnotexist" },
[&](Response res) {
req2.reset();
EXPECT_EQ(uv_thread_self(), mainThread);
EXPECT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Main));
ASSERT_NE(nullptr, res.error);
EXPECT_EQ(Response::Error::Reason::NotFound, res.error->reason);
EXPECT_EQ(false, res.stale);
@@ -64,7 +59,7 @@ TEST_F(Storage, HTTP404) {
HTTP404.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, HTTP500) {
@@ -72,15 +67,13 @@ TEST_F(Storage, HTTP500) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const auto mainThread = uv_thread_self();
std::unique_ptr<FileRequest> req3 = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/permanent-error" },
[&](Response res) {
req3.reset();
EXPECT_EQ(uv_thread_self(), mainThread);
EXPECT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Main));
ASSERT_NE(nullptr, res.error);
EXPECT_EQ(Response::Error::Reason::Server, res.error->reason);
EXPECT_EQ(false, res.stale);
@@ -94,7 +87,7 @@ TEST_F(Storage, HTTP500) {
HTTP500.finish();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
TEST_F(Storage, HTTPNoCallback) {
@@ -102,8 +95,8 @@ TEST_F(Storage, HTTPNoCallback) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
try {
fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" },

View File

@@ -1,11 +1,10 @@
#include "storage.hpp"
#include <mbgl/util/uv_detail.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/timer.hpp>
// Test for https://github.com/mapbox/mapbox-gl-native/issues/2123
//
@@ -18,8 +17,8 @@ TEST_F(Storage, HTTPNetworkStatusChange) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/delayed" };
@@ -38,17 +37,17 @@ TEST_F(Storage, HTTPNetworkStatusChange) {
});
// After 50 milliseconds, we're going to trigger a NetworkStatus change.
uv::timer reachableTimer(uv_default_loop());
reachableTimer.start(50, 0, [] () {
util::Timer reachableTimer;
reachableTimer.start(std::chrono::milliseconds(50), Duration::zero(), [] () {
mbgl::NetworkStatus::Reachable();
});
// This timer will keep the loop alive to make sure we would be getting a response in caes the
// network status change triggered another change (which it shouldn't).
uv::timer delayTimer(uv_default_loop());
delayTimer.start(300, 0, [] () {});
util::Timer delayTimer;
delayTimer.start(std::chrono::milliseconds(300), Duration::zero(), [] () {});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}
// Tests that a change in network status preempts requests that failed due to connection or
@@ -58,15 +57,15 @@ TEST_F(Storage, HTTPNetworkStatusChangePreempt) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
const auto start = uv_hrtime();
const auto start = Clock::now();
const Resource resource{ Resource::Unknown, "http://127.0.0.1:3001/test" };
std::unique_ptr<FileRequest> req = fs.request(resource, [&](Response res) {
static int counter = 0;
const auto duration = double(uv_hrtime() - start) / 1e9;
const auto duration = std::chrono::duration<const double>(Clock::now() - start).count();
if (counter == 0) {
EXPECT_GT(0.2, duration) << "Response came in too late";
} else if (counter == 1) {
@@ -102,10 +101,10 @@ TEST_F(Storage, HTTPNetworkStatusChangePreempt) {
});
// After 400 milliseconds, we're going to trigger a NetworkStatus change.
uv::timer reachableTimer(uv_default_loop());
reachableTimer.start(400, 0, [] () {
util::Timer reachableTimer;
reachableTimer.start(std::chrono::milliseconds(400), Duration::zero(), [] () {
mbgl::NetworkStatus::Reachable();
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
}

View File

@@ -1,7 +1,5 @@
#include "storage.hpp"
#include <uv.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/util/chrono.hpp>
@@ -12,8 +10,8 @@ TEST_F(Storage, HTTPTimeout) {
using namespace mbgl;
util::RunLoop loop;
DefaultFileSource fs(nullptr);
util::RunLoop loop(uv_default_loop());
int counter = 0;
@@ -34,7 +32,7 @@ TEST_F(Storage, HTTPTimeout) {
}
});
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();
EXPECT_EQ(4, counter);
}

View File

@@ -11,4 +11,4 @@ void Storage::SetUpTestCase() {
void Storage::TearDownTestCase() {
mbgl::test::stopServer(pid);
}
}

View File

@@ -3,7 +3,6 @@
#include "../fixtures/util.hpp"
#include <mbgl/storage/response.hpp>
#include <uv.h>
#include <iostream>
class Storage : public testing::Test {

View File

@@ -4,6 +4,7 @@
#include <mbgl/text/font_stack.hpp>
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/thread.hpp>
@@ -53,9 +54,9 @@ private:
class GlyphStoreTest : public testing::Test {
protected:
void runTest(const GlyphStoreParams& params, FileSource* fileSource, GlyphStoreTestCallback callback) {
util::RunLoop loop(uv_default_loop());
util::RunLoop loop;
async_ = std::make_unique<uv::async>(loop.get(), [&]{ loop.stop(); });
async_ = std::make_unique<util::AsyncTask>([&]{ loop.stop(); });
async_->unref();
const util::ThreadContext context = {"Map", util::ThreadType::Map, util::ThreadPriority::Regular};
@@ -63,7 +64,7 @@ protected:
util::Thread<GlyphStoreThread> tester(context, fileSource, callback);
tester.invoke(&GlyphStoreThread::loadGlyphStore, params);
uv_run(loop.get(), UV_RUN_DEFAULT);
loop.run();
tester.invoke(&GlyphStoreThread::unloadGlyphStore);
}
@@ -80,7 +81,7 @@ protected:
private:
bool testDone = false;
std::unique_ptr<uv::async> async_;
std::unique_ptr<util::AsyncTask> async_;
};
TEST_F(GlyphStoreTest, LoadingSuccess) {

View File

@@ -5,9 +5,9 @@
#include <mbgl/map/map.hpp>
#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/uv_detail.hpp>
using namespace mbgl;
@@ -20,7 +20,7 @@ class PendingResources : public ::testing::TestWithParam<std::string> {
// the Map object after that. The idea here is to test if these pending requests
// are getting canceled correctly if on shutdown.
TEST_P(PendingResources, DeleteMapObjectWithPendingRequest) {
util::RunLoop loop(uv_default_loop());
util::RunLoop loop;
auto display = std::make_shared<mbgl::HeadlessDisplay>();
HeadlessView view(display, 1, 1000, 1000);
@@ -28,7 +28,7 @@ TEST_P(PendingResources, DeleteMapObjectWithPendingRequest) {
std::unique_ptr<Map> map = std::make_unique<Map>(view, fileSource, MapMode::Still);
uv::async endTest(loop.get(), [&map, &loop] {
util::AsyncTask endTest([&map, &loop] {
map.reset();
loop.stop();
});
@@ -43,7 +43,7 @@ TEST_P(PendingResources, DeleteMapObjectWithPendingRequest) {
EXPECT_TRUE(false) << "Should never happen.";
});
uv_run(loop.get(), UV_RUN_DEFAULT);
loop.run();
}
// In the test data below, "sprite" will match both "sprite.json" and "sprite.png" and cause two

View File

@@ -80,7 +80,7 @@ private:
void runTestCase(MockFileSource::Type type,
const std::string& param,
const std::string& message) {
util::RunLoop loop(uv_default_loop());
util::RunLoop loop;
MockView view;
MockFileSource fileSource(type, param);
@@ -118,7 +118,7 @@ void runTestCase(MockFileSource::Type type,
std::make_unique<util::Thread<MockMapContext>>(
util::ThreadContext{"Map", util::ThreadType::Map, util::ThreadPriority::Regular}, view, fileSource, callback));
uv_run(loop.get(), UV_RUN_DEFAULT);
loop.run();
// Needed because it will make the Map thread
// join and cease logging after this point.