2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2014-03-17 17:43:55 +08:00
|
|
|
require('../helper');
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
var assert = require('../support/assert');
|
|
|
|
var step = require('step');
|
|
|
|
var net = require('net');
|
2014-03-17 17:43:55 +08:00
|
|
|
|
2015-05-13 17:19:02 +08:00
|
|
|
var sql_server_port = 5540;
|
2016-12-14 00:17:53 +08:00
|
|
|
var sql_server = net.createServer(function(c) {
|
|
|
|
c.destroy();
|
|
|
|
sql_server.close(function() {
|
|
|
|
});
|
2014-03-17 17:43:55 +08:00
|
|
|
});
|
|
|
|
|
2016-12-19 22:28:24 +08:00
|
|
|
describe('backend crash', function() {
|
2014-03-17 17:43:55 +08:00
|
|
|
|
2016-12-14 00:17:53 +08:00
|
|
|
before(function(done){
|
|
|
|
sql_server.listen(sql_server_port, done);
|
|
|
|
});
|
2014-03-17 17:43:55 +08:00
|
|
|
|
2016-12-14 00:17:53 +08:00
|
|
|
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/135
|
|
|
|
it('does not hang server', function(done){
|
|
|
|
var db_host_backup = global.settings.db_host;
|
|
|
|
var db_port_backup = global.settings.db_port;
|
|
|
|
global.settings.db_host = 'localhost';
|
|
|
|
global.settings.db_port = sql_server_port;
|
2019-10-04 00:24:39 +08:00
|
|
|
var server = require('../../lib/server')();
|
2016-12-14 00:17:53 +08:00
|
|
|
step(
|
|
|
|
function sendQuery() {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT+1',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'vizzuality.localhost' }
|
|
|
|
},{}, this);
|
|
|
|
},
|
|
|
|
function checkResponse(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.equal(res.statusCode, 500, res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.error);
|
|
|
|
var msg = parsed.error[0];
|
|
|
|
assert.ok(msg.match(/unexpected.*end/), msg);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function sendAnotherQuery() {
|
|
|
|
assert.response(server, {
|
|
|
|
url: '/api/v1/sql?q=SELECT+2',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'vizzuality.localhost' }
|
|
|
|
},{}, this);
|
|
|
|
},
|
|
|
|
function checkResponse(err, res) {
|
|
|
|
assert.ifError(err);
|
|
|
|
assert.equal(res.statusCode, 500, res.statusCode + ': ' + res.body);
|
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.error);
|
|
|
|
var msg = parsed.error[0];
|
|
|
|
assert.ok(msg.match(/connect/), msg);
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
global.settings.db_host = db_host_backup;
|
|
|
|
global.settings.db_port = db_port_backup;
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2014-03-17 17:43:55 +08:00
|
|
|
|
2016-12-14 00:17:53 +08:00
|
|
|
after(function(done) {
|
2016-12-19 22:37:34 +08:00
|
|
|
// be sure the sql_server is closed
|
|
|
|
if (sql_server.listening) {
|
|
|
|
return sql_server.close(done);
|
2016-12-14 00:17:53 +08:00
|
|
|
}
|
2016-12-19 22:37:34 +08:00
|
|
|
|
|
|
|
done();
|
2016-12-14 00:17:53 +08:00
|
|
|
});
|
2016-12-13 18:10:14 +08:00
|
|
|
|
2014-03-17 17:43:55 +08:00
|
|
|
});
|