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;
|
2014-03-17 17:43:55 +08:00
|
|
|
var sql_server = net.createServer(function(c) {
|
|
|
|
console.log('server connected');
|
|
|
|
c.destroy();
|
|
|
|
console.log('server socket destroyed.');
|
|
|
|
sql_server.close(function() {
|
|
|
|
console.log('server closed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
describe('backend crash', function() {
|
2014-03-17 17:43:55 +08:00
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
before(function(done){
|
2014-03-17 17:43:55 +08:00
|
|
|
sql_server.listen(sql_server_port, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/135
|
2015-05-13 17:21:44 +08:00
|
|
|
it('does not hang server', function(done){
|
2014-03-17 17:43:55 +08:00
|
|
|
//console.log("settings:"); console.dir(global.settings);
|
|
|
|
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;
|
2016-09-15 02:54:24 +08:00
|
|
|
var server = require('../../app/server')();
|
2015-05-13 17:21:44 +08:00
|
|
|
step(
|
2014-03-17 17:43:55 +08:00
|
|
|
function sendQuery() {
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2014-03-17 17:43:55 +08:00
|
|
|
url: '/api/v1/sql?q=SELECT+1',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'vizzuality.localhost' }
|
2016-09-26 20:37:40 +08:00
|
|
|
},{}, this);
|
2014-03-17 17:43:55 +08:00
|
|
|
},
|
|
|
|
function checkResponse(err, res) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2014-08-20 20:52:17 +08:00
|
|
|
assert.equal(res.statusCode, 500, res.statusCode + ': ' + res.body);
|
2014-03-19 17:43:39 +08:00
|
|
|
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() {
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, {
|
2014-03-19 17:43:39 +08:00
|
|
|
url: '/api/v1/sql?q=SELECT+2',
|
|
|
|
method: 'GET',
|
|
|
|
headers: {host: 'vizzuality.localhost' }
|
2016-09-26 20:37:40 +08:00
|
|
|
},{}, this);
|
2014-03-19 17:43:39 +08:00
|
|
|
},
|
|
|
|
function checkResponse(err, res) {
|
2015-05-13 17:21:44 +08:00
|
|
|
assert.ifError(err);
|
2014-08-20 20:52:17 +08:00
|
|
|
assert.equal(res.statusCode, 500, res.statusCode + ': ' + res.body);
|
2014-03-19 17:43:39 +08:00
|
|
|
var parsed = JSON.parse(res.body);
|
|
|
|
assert.ok(parsed.error);
|
|
|
|
var msg = parsed.error[0];
|
|
|
|
assert.ok(msg.match(/connect/), msg);
|
2014-03-17 17:43:55 +08:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
function finish(err) {
|
|
|
|
global.settings.db_host = db_host_backup;
|
|
|
|
global.settings.db_port = db_port_backup;
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:21:44 +08:00
|
|
|
after(function(done) {
|
2014-03-17 17:43:55 +08:00
|
|
|
try {
|
|
|
|
sql_server.close(done);
|
|
|
|
} catch (er) {
|
|
|
|
console.log(er);
|
|
|
|
done(); // error expected as server is probably closed already
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|