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

Commit

Permalink
[node] Add request failure tests
Browse files Browse the repository at this point in the history
Test for timeout and when the request fails.
  • Loading branch information
tmpsantos authored and Bobby Sudekum committed Apr 7, 2017
1 parent e05a6bb commit b1ca199
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
59 changes: 59 additions & 0 deletions platform/node/test/js/request_fail.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

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

function asyncReply(callback, data) {
setTimeout(function() { callback(null, { data: data }); }, 0);
};

function asyncFail(callback) {
setTimeout(function() { callback(new Error('not found')); }, 0);
};

function failRequest(t, style, failedResource) {
var options = {
request: function(req, callback) {
var data = mockfs.dataForRequest(req);

if (failedResource != data) {
asyncReply(callback, data);
} else {
asyncFail(callback);
}
},
ratio: 2,
};

var map = new mbgl.Map(options);
map.load(style);

map.render({ zoom: 16 }, function(err, pixels) {
if (err) {
t.pass("pass");
map.release();
}
});
};

test('Vector', function(t) {
t.plan(5);

failRequest(t, mockfs.style_vector, null);
failRequest(t, mockfs.style_vector, mockfs.sprite_png);
failRequest(t, mockfs.style_vector, mockfs.sprite_json);
failRequest(t, mockfs.style_vector, mockfs.source_vector);
failRequest(t, mockfs.style_vector, mockfs.tile_vector);
failRequest(t, mockfs.style_vector, mockfs.glyph);
});

test('Raster', function(t) {
t.plan(4);

failRequest(t, mockfs.style_raster, null);
failRequest(t, mockfs.style_raster, mockfs.sprite_png);
failRequest(t, mockfs.style_raster, mockfs.sprite_json);
failRequest(t, mockfs.style_raster, mockfs.source_raster);
failRequest(t, mockfs.style_raster, mockfs.tile_raster);
});
67 changes: 67 additions & 0 deletions platform/node/test/js/request_timeout.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

var mockfs = require('../mockfs');
var mbgl = require('../../index');
var test = require('tape');
var timeout = 2000;

function asyncReply(callback, data) {
setTimeout(function() { callback(null, { data: data }); }, 0);
};

function timeoutRequest(t, style, timeoutedResource) {
var options = {
request: function(req, callback) {
var data = mockfs.dataForRequest(req);

if (timeoutedResource != data) {
asyncReply(callback, data);
} else {
setTimeout(function() { callback(null, { data: data }); t.pass("callback"); }, timeout);
}
},
ratio: 2,
};

var map = new mbgl.Map(options);
map.load(style);

map.render({ zoom: 16 }, function(err, pixels) {
t.fail("fail");
});

setTimeout(function() {
map.release();
t.pass("release");
}, timeout / 2);
};

test('Vector', function(t) {
// 1 sprite.png
// 1 spring.json
// 1 source
// 4 tiles
// 7 glyphs
// 5 map.release()
t.plan(19);

timeoutRequest(t, mockfs.style_vector, mockfs.sprite_png);
timeoutRequest(t, mockfs.style_vector, mockfs.sprite_json);
timeoutRequest(t, mockfs.style_vector, mockfs.source_vector);
timeoutRequest(t, mockfs.style_vector, mockfs.tile_vector);
timeoutRequest(t, mockfs.style_vector, mockfs.glyph);
});

test('Raster', function(t) {
// 1 sprite.png
// 1 spring.json
// 1 source
// 4 tiles
// 4 map.release()
t.plan(11);

timeoutRequest(t, mockfs.style_raster, mockfs.sprite_png);
timeoutRequest(t, mockfs.style_raster, mockfs.sprite_json);
timeoutRequest(t, mockfs.style_raster, mockfs.source_raster);
timeoutRequest(t, mockfs.style_raster, mockfs.tile_raster);
});

0 comments on commit b1ca199

Please sign in to comment.