[tests] Fix/improve Asset utests
ASSET=zip make test-* was broken and now works. Also added a test stressing concurrent file access.
This commit is contained in:
@@ -14,6 +14,18 @@
|
||||
|
||||
using namespace mbgl;
|
||||
|
||||
namespace {
|
||||
|
||||
std::string getFileSourceRoot() {
|
||||
#ifdef MBGL_ASSET_ZIP
|
||||
return "test/fixtures/annotations/assets.zip";
|
||||
#else
|
||||
return "";
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<SpriteImage> defaultMarker() {
|
||||
PremultipliedImage image = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png"));
|
||||
return std::make_shared<SpriteImage>(image.width, image.height, 1.0, std::string(reinterpret_cast<char*>(image.data.get()), image.size()));
|
||||
@@ -36,7 +48,7 @@ void checkRendering(Map& map, const char * name) {
|
||||
TEST(Annotations, PointAnnotation) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), "");
|
||||
@@ -49,7 +61,7 @@ TEST(Annotations, PointAnnotation) {
|
||||
TEST(Annotations, LineAnnotation) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), "");
|
||||
@@ -68,7 +80,7 @@ TEST(Annotations, LineAnnotation) {
|
||||
TEST(Annotations, FillAnnotation) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), "");
|
||||
@@ -86,7 +98,7 @@ TEST(Annotations, FillAnnotation) {
|
||||
TEST(Annotations, StyleSourcedShapeAnnotation) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/annotation.json"), "");
|
||||
@@ -101,7 +113,7 @@ TEST(Annotations, StyleSourcedShapeAnnotation) {
|
||||
TEST(Annotations, AddMultiple) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), "");
|
||||
@@ -118,7 +130,7 @@ TEST(Annotations, AddMultiple) {
|
||||
TEST(Annotations, NonImmediateAdd) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), "");
|
||||
@@ -138,7 +150,7 @@ TEST(Annotations, NonImmediateAdd) {
|
||||
TEST(Annotations, RemovePoint) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), "");
|
||||
@@ -155,7 +167,7 @@ TEST(Annotations, RemovePoint) {
|
||||
TEST(Annotations, RemoveShape) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
|
||||
AnnotationSegments segments = {{ {{ { 0, 0 }, { 45, 45 } }} }};
|
||||
|
||||
@@ -177,7 +189,7 @@ TEST(Annotations, RemoveShape) {
|
||||
TEST(Annotations, ImmediateRemoveShape) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
|
||||
map.removeAnnotation(map.addShapeAnnotation(ShapeAnnotation({}, {})));
|
||||
@@ -189,7 +201,7 @@ TEST(Annotations, ImmediateRemoveShape) {
|
||||
TEST(Annotations, SwitchStyle) {
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1);
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
DefaultFileSource fileSource(nullptr, getFileSourceRoot());
|
||||
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"), "");
|
||||
|
||||
@@ -17,7 +17,11 @@ TEST(API, RepeatedRender) {
|
||||
|
||||
auto display = std::make_shared<mbgl::HeadlessDisplay>();
|
||||
HeadlessView view(display, 1, 256, 512);
|
||||
#ifdef MBGL_ASSET_ZIP
|
||||
DefaultFileSource fileSource(nullptr, "test/fixtures/api/assets.zip");
|
||||
#else
|
||||
DefaultFileSource fileSource(nullptr);
|
||||
#endif
|
||||
|
||||
Log::setObserver(std::make_unique<FixtureLogObserver>());
|
||||
|
||||
|
||||
BIN
test/fixtures/annotations/assets.zip
vendored
Normal file
BIN
test/fixtures/annotations/assets.zip
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/api/assets.zip
vendored
Normal file
BIN
test/fixtures/api/assets.zip
vendored
Normal file
Binary file not shown.
@@ -27,7 +27,7 @@ TEST_F(Storage, AssetReadDirectory) {
|
||||
EXPECT_EQ(Seconds::zero(), res.modified);
|
||||
EXPECT_EQ("", res.etag);
|
||||
#ifdef MBGL_ASSET_ZIP
|
||||
EXPECT_EQ("No such file", res.error->message);
|
||||
EXPECT_EQ("Could not stat file in zip archive", res.error->message);
|
||||
#elif MBGL_ASSET_FS
|
||||
EXPECT_EQ("Is a directory", res.error->message);
|
||||
#endif
|
||||
|
||||
@@ -4,6 +4,91 @@
|
||||
#include <mbgl/platform/platform.hpp>
|
||||
#include <mbgl/util/chrono.hpp>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
|
||||
namespace {
|
||||
|
||||
std::string getFileSourceRoot() {
|
||||
#ifdef MBGL_ASSET_ZIP
|
||||
return "test/fixtures/storage/assets.zip";
|
||||
#else
|
||||
return "";
|
||||
#endif
|
||||
}
|
||||
|
||||
class TestWorker {
|
||||
public:
|
||||
TestWorker(mbgl::DefaultFileSource* fs_) : fs(fs_) {}
|
||||
|
||||
void run(std::function<void()> endCallback) {
|
||||
const std::string asset("asset://TEST_DATA/fixtures/storage/nonempty");
|
||||
|
||||
requestCallback = [this, asset, endCallback](mbgl::Response res) {
|
||||
EXPECT_EQ(nullptr, res.error);
|
||||
ASSERT_TRUE(res.data.get());
|
||||
EXPECT_EQ("content is here\n", *res.data);
|
||||
|
||||
if (res.stale) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!--numRequests) {
|
||||
endCallback();
|
||||
request.reset();
|
||||
} else {
|
||||
request = fs->request({ mbgl::Resource::Unknown, asset }, requestCallback);
|
||||
}
|
||||
};
|
||||
|
||||
request = fs->request({ mbgl::Resource::Unknown, asset }, requestCallback);
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned numRequests = 1000;
|
||||
|
||||
mbgl::DefaultFileSource* fs;
|
||||
std::unique_ptr<mbgl::FileRequest> request;
|
||||
|
||||
std::function<void(mbgl::Response)> requestCallback;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
TEST_F(Storage, AssetStress) {
|
||||
SCOPED_TEST(AssetStress)
|
||||
|
||||
using namespace mbgl;
|
||||
|
||||
util::RunLoop loop;
|
||||
|
||||
mbgl::DefaultFileSource fs(nullptr, getFileSourceRoot());
|
||||
|
||||
unsigned numThreads = 50;
|
||||
|
||||
auto callback = [&] {
|
||||
if (!--numThreads) {
|
||||
loop.stop();
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<std::unique_ptr<util::Thread<TestWorker>>> threads;
|
||||
std::vector<std::unique_ptr<mbgl::WorkRequest>> requests;
|
||||
util::ThreadContext context = { "Test", util::ThreadType::Map, util::ThreadPriority::Regular };
|
||||
|
||||
for (unsigned i = 0; i < numThreads; ++i) {
|
||||
std::unique_ptr<util::Thread<TestWorker>> thread =
|
||||
std::make_unique<util::Thread<TestWorker>>(context, &fs);
|
||||
|
||||
requests.push_back(
|
||||
thread->invokeWithCallback(&TestWorker::run, callback));
|
||||
|
||||
threads.push_back(std::move(thread));
|
||||
}
|
||||
|
||||
loop.run();
|
||||
|
||||
AssetStress.finish();
|
||||
}
|
||||
|
||||
TEST_F(Storage, AssetEmptyFile) {
|
||||
SCOPED_TEST(EmptyFile)
|
||||
@@ -12,11 +97,7 @@ TEST_F(Storage, AssetEmptyFile) {
|
||||
|
||||
util::RunLoop loop;
|
||||
|
||||
#ifdef MBGL_ASSET_ZIP
|
||||
DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip");
|
||||
#else
|
||||
DefaultFileSource fs(nullptr);
|
||||
#endif
|
||||
DefaultFileSource fs(nullptr, getFileSourceRoot());
|
||||
|
||||
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/empty" }, [&](Response res) {
|
||||
req.reset();
|
||||
@@ -41,11 +122,7 @@ TEST_F(Storage, AssetNonEmptyFile) {
|
||||
|
||||
util::RunLoop loop;
|
||||
|
||||
#ifdef MBGL_ASSET_ZIP
|
||||
DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip");
|
||||
#else
|
||||
DefaultFileSource fs(nullptr);
|
||||
#endif
|
||||
DefaultFileSource fs(nullptr, getFileSourceRoot());
|
||||
|
||||
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/nonempty" }, [&](Response res) {
|
||||
req.reset();
|
||||
@@ -72,11 +149,7 @@ TEST_F(Storage, AssetNonExistentFile) {
|
||||
|
||||
util::RunLoop loop;
|
||||
|
||||
#ifdef MBGL_ASSET_ZIP
|
||||
DefaultFileSource fs(nullptr, "test/fixtures/storage/assets.zip");
|
||||
#else
|
||||
DefaultFileSource fs(nullptr);
|
||||
#endif
|
||||
DefaultFileSource fs(nullptr, getFileSourceRoot());
|
||||
|
||||
std::unique_ptr<FileRequest> req = fs.request({ Resource::Unknown, "asset://TEST_DATA/fixtures/storage/does_not_exist" }, [&](Response res) {
|
||||
req.reset();
|
||||
@@ -88,7 +161,7 @@ TEST_F(Storage, AssetNonExistentFile) {
|
||||
EXPECT_EQ(Seconds::zero(), res.modified);
|
||||
EXPECT_EQ("", res.etag);
|
||||
#ifdef MBGL_ASSET_ZIP
|
||||
EXPECT_EQ("No such file", res.error->message);
|
||||
EXPECT_EQ("Could not stat file in zip archive", res.error->message);
|
||||
#elif MBGL_ASSET_FS
|
||||
EXPECT_EQ("No such file or directory", res.error->message);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user