2015-09-25 20:09:35 +08:00
|
|
|
var testHelper = require('../support/test_helper');
|
2015-04-01 23:39:02 +08:00
|
|
|
|
|
|
|
var assert = require('../support/assert');
|
|
|
|
var _ = require('underscore');
|
|
|
|
var redis = require('redis');
|
|
|
|
|
2015-07-05 02:41:22 +08:00
|
|
|
var CartodbWindshaft = require('../../lib/cartodb/server');
|
2015-04-02 01:36:43 +08:00
|
|
|
var serverOptions = require('../../lib/cartodb/server_options');
|
2015-04-01 23:39:02 +08:00
|
|
|
|
2015-09-25 20:09:35 +08:00
|
|
|
var LayergroupToken = require('../../lib/cartodb/models/layergroup_token');
|
|
|
|
|
2015-07-11 01:10:55 +08:00
|
|
|
describe('render limits', function() {
|
2015-04-01 23:39:02 +08:00
|
|
|
|
|
|
|
var layergroupUrl = '/api/v1/map';
|
|
|
|
|
|
|
|
var redisClient = redis.createClient(global.environment.redis.port);
|
|
|
|
|
2015-04-02 01:36:43 +08:00
|
|
|
var server;
|
2015-09-25 20:09:35 +08:00
|
|
|
var keysToDelete;
|
2015-04-02 01:36:43 +08:00
|
|
|
beforeEach(function() {
|
2015-09-25 20:09:35 +08:00
|
|
|
keysToDelete = {};
|
2015-07-05 02:41:22 +08:00
|
|
|
server = new CartodbWindshaft(serverOptions);
|
2015-04-02 01:36:43 +08:00
|
|
|
server.setMaxListeners(0);
|
|
|
|
});
|
|
|
|
|
2015-04-01 23:39:02 +08:00
|
|
|
afterEach(function(done) {
|
2015-09-26 00:04:45 +08:00
|
|
|
testHelper.deleteRedisKeys(keysToDelete, done);
|
2015-04-01 23:39:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
var user = 'localhost';
|
|
|
|
|
2015-04-02 01:36:43 +08:00
|
|
|
var pointSleepSql = "SELECT pg_sleep(0.5)," +
|
2015-04-01 23:39:02 +08:00
|
|
|
" 'SRID=3857;POINT(0 0)'::geometry the_geom_webmercator, 1 cartodb_id";
|
|
|
|
var pointCartoCss = '#layer { marker-fill:red; }';
|
2015-04-02 01:36:43 +08:00
|
|
|
var polygonSleepSql = "SELECT pg_sleep(0.5)," +
|
2015-04-01 23:39:02 +08:00
|
|
|
" ST_Buffer('SRID=3857;POINT(0 0)'::geometry, 100000000) the_geom_webmercator, 1 cartodb_id";
|
|
|
|
var polygonCartoCss = '#layer { polygon-fill:red; }';
|
|
|
|
|
|
|
|
function singleLayergroupConfig(sql, cartocss) {
|
|
|
|
return {
|
|
|
|
version: '1.0.0',
|
|
|
|
layers: [
|
|
|
|
{
|
|
|
|
type: 'mapnik',
|
|
|
|
options: {
|
|
|
|
sql: sql,
|
|
|
|
cartocss: cartocss,
|
|
|
|
cartocss_version: '2.0.1'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRequest(layergroup, userHost) {
|
|
|
|
return {
|
|
|
|
url: layergroupUrl,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
host: userHost,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
data: JSON.stringify(layergroup)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function withRenderLimit(user, renderLimit, callback) {
|
|
|
|
redisClient.SELECT(5, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
var userLimitsKey = 'limits:tiler:' + user;
|
|
|
|
redisClient.HSET(userLimitsKey, 'render', renderLimit, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-09-25 20:09:35 +08:00
|
|
|
keysToDelete[userLimitsKey] = 5;
|
2015-04-01 23:39:02 +08:00
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
describe('with onTileErrorStrategy DISABLED', function() {
|
|
|
|
var onTileErrorStrategyEnabled;
|
|
|
|
before(function() {
|
|
|
|
onTileErrorStrategyEnabled = global.environment.enabledFeatures.onTileErrorStrategy;
|
|
|
|
global.environment.enabledFeatures.onTileErrorStrategy = false;
|
|
|
|
});
|
2015-04-01 23:39:02 +08:00
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
after(function() {
|
|
|
|
global.environment.enabledFeatures.onTileErrorStrategy = onTileErrorStrategyEnabled;
|
2015-04-01 23:39:02 +08:00
|
|
|
});
|
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
it("layergroup creation fails if test tile is slow", function(done) {
|
|
|
|
withRenderLimit(user, 50, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-04-02 01:36:43 +08:00
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
var layergroup = singleLayergroupConfig(polygonSleepSql, polygonCartoCss);
|
|
|
|
assert.response(server,
|
|
|
|
createRequest(layergroup, user),
|
|
|
|
{
|
|
|
|
status: 400
|
|
|
|
},
|
|
|
|
function(res) {
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.deepEqual(parsed, { errors: [ 'Render timed out' ] });
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("layergroup creation does not fail if user limit is high enough even if test tile is slow", function(done) {
|
|
|
|
withRenderLimit(user, 5000, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
2015-04-02 01:36:43 +08:00
|
|
|
}
|
2015-04-07 00:44:57 +08:00
|
|
|
|
|
|
|
var layergroup = singleLayergroupConfig(polygonSleepSql, polygonCartoCss);
|
|
|
|
assert.response(server,
|
|
|
|
createRequest(layergroup, user),
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res) {
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.layergroupid);
|
2015-09-25 20:09:35 +08:00
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
2015-09-26 00:04:45 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-04-07 00:44:57 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2015-04-02 01:36:43 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
it("layergroup creation works if test tile is fast but tile request fails if they are slow", function(done) {
|
|
|
|
withRenderLimit(user, 50, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2015-04-01 23:39:02 +08:00
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
var layergroup = singleLayergroupConfig(pointSleepSql, pointCartoCss);
|
|
|
|
assert.response(server,
|
|
|
|
createRequest(layergroup, user),
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res) {
|
2015-09-25 20:09:35 +08:00
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(JSON.parse(res.body).layergroupid).token] = 0;
|
2015-09-26 00:04:45 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-04-07 00:44:57 +08:00
|
|
|
assert.response(server,
|
|
|
|
{
|
|
|
|
url: layergroupUrl + _.template('/<%= layergroupId %>/<%= z %>/<%= x %>/<%= y %>.png', {
|
|
|
|
layergroupId: JSON.parse(res.body).layergroupid,
|
|
|
|
z: 0,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
}),
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
host: 'localhost'
|
|
|
|
},
|
|
|
|
encoding: 'binary'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 400
|
2015-04-01 23:39:02 +08:00
|
|
|
},
|
2015-04-07 00:44:57 +08:00
|
|
|
function(res) {
|
|
|
|
var parsed = JSON.parse(res.body);
|
2015-06-06 01:39:25 +08:00
|
|
|
assert.deepEqual(parsed, { errors: ['Render timed out'] });
|
2015-04-07 00:44:57 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2015-04-01 23:39:02 +08:00
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
it("tile request does not fail if user limit is high enough", function(done) {
|
|
|
|
withRenderLimit(user, 5000, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
2015-04-01 23:39:02 +08:00
|
|
|
}
|
2015-04-07 00:44:57 +08:00
|
|
|
|
|
|
|
var layergroup = singleLayergroupConfig(pointSleepSql, pointCartoCss);
|
|
|
|
assert.response(server,
|
|
|
|
createRequest(layergroup, user),
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res) {
|
2015-09-25 20:09:35 +08:00
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(JSON.parse(res.body).layergroupid).token] = 0;
|
2015-09-26 00:04:45 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-04-07 00:44:57 +08:00
|
|
|
assert.response(server,
|
|
|
|
{
|
|
|
|
url: layergroupUrl + _.template('/<%= layergroupId %>/<%= z %>/<%= x %>/<%= y %>.png', {
|
|
|
|
layergroupId: JSON.parse(res.body).layergroupid,
|
|
|
|
z: 0,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
}),
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
host: 'localhost'
|
|
|
|
},
|
|
|
|
encoding: 'binary'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'image/png'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2015-04-01 23:39:02 +08:00
|
|
|
});
|
2015-04-07 00:44:57 +08:00
|
|
|
|
2015-04-01 23:39:02 +08:00
|
|
|
});
|
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
describe('with onTileErrorStrategy', function() {
|
2015-04-02 01:36:43 +08:00
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
it("layergroup creation works even if test tile is slow", function(done) {
|
|
|
|
withRenderLimit(user, 50, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var layergroup = singleLayergroupConfig(polygonSleepSql, polygonCartoCss);
|
|
|
|
assert.response(server,
|
|
|
|
createRequest(layergroup, user),
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res) {
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.layergroupid);
|
2015-09-25 20:09:35 +08:00
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(parsed.layergroupid).token] = 0;
|
2015-09-26 00:04:45 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-04-07 00:44:57 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("layergroup creation and tile requests works even if they are slow but returns fallback", function(done) {
|
|
|
|
withRenderLimit(user, 50, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var layergroup = singleLayergroupConfig(pointSleepSql, pointCartoCss);
|
|
|
|
assert.response(server,
|
|
|
|
createRequest(layergroup, user),
|
|
|
|
{
|
|
|
|
status: 200
|
|
|
|
},
|
|
|
|
function(res) {
|
2015-09-25 20:09:35 +08:00
|
|
|
keysToDelete['map_cfg|' + LayergroupToken.parse(JSON.parse(res.body).layergroupid).token] = 0;
|
2015-09-26 00:04:45 +08:00
|
|
|
keysToDelete['user:localhost:mapviews:global'] = 5;
|
2015-04-07 00:44:57 +08:00
|
|
|
assert.response(server,
|
|
|
|
{
|
|
|
|
url: layergroupUrl + _.template('/<%= layergroupId %>/<%= z %>/<%= x %>/<%= y %>.png', {
|
|
|
|
layergroupId: JSON.parse(res.body).layergroupid,
|
|
|
|
z: 0,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
}),
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
host: 'localhost'
|
|
|
|
},
|
|
|
|
encoding: 'binary'
|
2015-04-02 01:36:43 +08:00
|
|
|
},
|
2015-04-07 00:44:57 +08:00
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'image/png'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(res, err) {
|
|
|
|
if (err) {
|
|
|
|
done(err);
|
|
|
|
}
|
2016-02-22 23:36:06 +08:00
|
|
|
var referenceImagePath = './test/fixtures/render-timeout-fallback.png';
|
|
|
|
assert.imageBufferIsSimilarToFile(res.body, referenceImagePath, 25,
|
2015-04-07 00:44:57 +08:00
|
|
|
function(imgErr/*, similarity*/) {
|
|
|
|
done(imgErr);
|
|
|
|
}
|
|
|
|
);
|
2015-04-02 01:36:43 +08:00
|
|
|
}
|
2015-04-07 00:44:57 +08:00
|
|
|
);
|
2015-04-02 01:36:43 +08:00
|
|
|
|
2015-04-07 00:44:57 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2015-04-02 01:36:43 +08:00
|
|
|
});
|
2015-04-07 00:44:57 +08:00
|
|
|
|
2015-04-02 01:36:43 +08:00
|
|
|
});
|
|
|
|
|
2015-04-01 23:39:02 +08:00
|
|
|
});
|