Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[node] Fix memory test hanging after GlyphAtlas refactoring #8394

Merged
merged 1 commit into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions platform/node/src/node_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,19 @@ void NodeRequest::HandleCallback(const Nan::FunctionCallbackInfo<v8::Value>& inf
}

void NodeRequest::Execute() {
asyncExecute = std::make_unique<mbgl::util::AsyncTask>([this] { doExecute(); Unref(); });
asyncExecute->send();

Ref();
}

void NodeRequest::doExecute() {
Nan::HandleScope scope;

v8::Local<v8::Value> argv[] = { handle() };

Nan::MakeCallback(Nan::To<v8::Object>(target->handle()->GetInternalField(1)).ToLocalChecked(), "request", 1, argv);
asyncExecute.reset();
}

NodeRequest::NodeAsyncRequest::NodeAsyncRequest(NodeRequest* request_) : request(request_) {
Expand Down
6 changes: 6 additions & 0 deletions platform/node/src/node_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/async_task.hpp>

#include <memory>

namespace node_mbgl {

Expand Down Expand Up @@ -35,9 +38,12 @@ class NodeRequest : public Nan::ObjectWrap,
void Execute();

private:
void doExecute();

NodeMap* target;
mbgl::FileSource::Callback callback;
NodeAsyncRequest* asyncRequest = nullptr;
std::unique_ptr<mbgl::util::AsyncTask> asyncExecute;
};

}
41 changes: 4 additions & 37 deletions platform/node/test/memory.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

var fs = require('fs');
var mockfs = require('./mockfs');
var mbgl = require('../index');
var path = require('path');
var test = require('tape');

var testParams = {
Expand All @@ -12,20 +11,6 @@ var testParams = {
ratio: 2
};

function readFixture(file) {
return fs.readFileSync(path.join('test/fixtures/resources', file));
}

var style_raster = readFixture('style_raster.json').toString('utf8');
var style_vector = readFixture('style_vector.json').toString('utf8');
var sprite_json = readFixture('sprite.json');
var sprite_png = readFixture('sprite.png');
var glyph = readFixture('glyphs.pbf');
var source_raster = readFixture('source_raster.json');
var source_vector = readFixture('source_vector.json');
var tile_raster = readFixture('raster.tile');
var tile_vector = readFixture('vector.tile');

test('Memory', function(t) {
// Trigger garbage collection before starting test, then initialize
// heap size
Expand All @@ -34,25 +19,7 @@ test('Memory', function(t) {

var options = {
request: function(req, callback) {
if (req.url == null) {
t.fail('invalid file request');
} else if (req.url.indexOf('sprite') > -1 && req.url.endsWith('json')) {
callback(null, { data: sprite_json });
} else if (req.url.indexOf('sprite') > -1 && req.url.endsWith('png')) {
callback(null, { data: sprite_png });
} else if (req.url.indexOf('fonts') > -1 && req.url.endsWith('pbf')) {
callback(null, { data: glyph });
} else if (req.url.endsWith('mapbox.satellite')) {
callback(null, { data: source_raster });
} else if (req.url.indexOf('satellite') > -1 && (req.url.endsWith('png') || req.url.endsWith('webp'))) {
callback(null, { data: tile_raster });
} else if (req.url.endsWith('mapbox.mapbox-streets-v7')) {
callback(null, { data: source_vector });
} else if (req.url.indexOf('streets') > -1 && req.url.endsWith('pbf')) {
callback(null, { data: tile_vector });
} else {
t.fail('unhandled file request: ' + req.url);
}
callback(null, { data: mockfs.dataForRequest(req) });
},
ratio: testParams.ratio,
};
Expand All @@ -75,9 +42,9 @@ test('Memory', function(t) {
var map = mapPool.shift();

if (Math.floor(Math.random() * 2)) {
map.load(style_raster);
map.load(mockfs.style_raster);
} else {
map.load(style_vector);
map.load(mockfs.style_vector);
}

map.render({ zoom: 16 }, function(err, pixels) {
Expand Down
53 changes: 53 additions & 0 deletions platform/node/test/mockfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

var fs = require('fs');
var path = require('path');

function readFixture(file) {
return fs.readFileSync(path.join('test/fixtures/resources', file));
};

var style_raster = readFixture('style_raster.json').toString('utf8');
var style_vector = readFixture('style_vector.json').toString('utf8');
var sprite_json = readFixture('sprite.json');
var sprite_png = readFixture('sprite.png');
var glyph = readFixture('glyphs.pbf');
var source_raster = readFixture('source_raster.json');
var source_vector = readFixture('source_vector.json');
var tile_raster = readFixture('raster.tile');
var tile_vector = readFixture('vector.tile');

function dataForRequest(req) {
if (req.url == null) {
return null;
} else if (req.url.indexOf('sprite') > -1 && req.url.endsWith('json')) {
return sprite_json;
} else if (req.url.indexOf('sprite') > -1 && req.url.endsWith('png')) {
return sprite_png;
} else if (req.url.indexOf('fonts') > -1 && req.url.endsWith('pbf')) {
return glyph;
} else if (req.url.endsWith('mapbox.satellite')) {
return source_raster;
} else if (req.url.indexOf('satellite') > -1 && (req.url.endsWith('png') || req.url.endsWith('webp'))) {
return tile_raster;
} else if (req.url.endsWith('mapbox.mapbox-streets-v7')) {
return source_vector;
} else if (req.url.indexOf('streets') > -1 && req.url.endsWith('pbf')) {
return tile_vector;
} else {
return null;
}
};

module.exports = {
dataForRequest: dataForRequest,
style_raster: style_raster,
style_vector: style_vector,
sprite_json: sprite_json,
sprite_png: sprite_png,
glyph: glyph,
source_raster: source_raster,
source_vector: source_vector,
tile_raster: tile_raster,
tile_vector: tile_vector
};