Honour the 'node_socket_timeout' configuration directive

Closes #128
Includes testcase, which required enhancing assert.response to
handle errors.
Changes app controller to a function that only reads configuration
when executed (rather than when only require'd).
This commit is contained in:
Sandro Santilli 2014-01-31 10:55:30 +01:00
parent b736c228cf
commit 6c47537f47
15 changed files with 100 additions and 15 deletions

View File

@ -1,6 +1,14 @@
1.8.3 - 2014-MM-DD 1.8.3 - 2014-MM-DD
------------------ ------------------
Bug fixes:
* Honour the 'node_socket_timeout' configuration directive (#128)
Enhancements:
* Add support for error handling in assert.request
1.8.2 - 2014-01-20 1.8.2 - 2014-01-20
------------------ ------------------

2
app.js
View File

@ -26,7 +26,7 @@ _.extend(global.settings, env);
// kick off controller // kick off controller
if ( ! global.settings.base_url ) global.settings.base_url = '/api/*'; if ( ! global.settings.base_url ) global.settings.base_url = '/api/*';
var app = require(global.settings.app_root + '/app/controllers/app'); var app = require(global.settings.app_root + '/app/controllers/app')();
app.listen(global.settings.node_port, global.settings.node_host, function() { app.listen(global.settings.node_port, global.settings.node_host, function() {
console.log("CartoDB SQL API listening on " + console.log("CartoDB SQL API listening on " +
global.settings.node_host + ":" + global.settings.node_port + global.settings.node_host + ":" + global.settings.node_port +

View File

@ -15,6 +15,8 @@
// //
// //
function App() {
var path = require('path'); var path = require('path');
var express = require('express') var express = require('express')
@ -75,6 +77,15 @@ Date.prototype.toJSON = function() {
return s; return s;
} }
// Set connection timeout
if ( global.settings.hasOwnProperty('node_socket_timeout') ) {
var timeout = parseInt(global.settings.node_socket_timeout);
app.use(function(req, res, next) {
req.connection.setTimeout(timeout);
next()
});
}
app.use(express.bodyParser()); app.use(express.bodyParser());
app.enable('jsonp callback'); app.enable('jsonp callback');
app.set("trust proxy", true); app.set("trust proxy", true);
@ -422,4 +433,8 @@ function handleException(err, res){
} }
} }
module.exports = app; return app;
}
module.exports = App;

View File

@ -1,7 +1,7 @@
require('../helper'); require('../helper');
require('../support/assert'); require('../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, tests = module.exports = {} , tests = module.exports = {}
, querystring = require('querystring'); , querystring = require('querystring');

View File

@ -16,7 +16,7 @@ require('../helper');
require('../support/assert'); require('../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, querystring = require('querystring') , querystring = require('querystring')
, _ = require('underscore') , _ = require('underscore')
@ -26,8 +26,6 @@ var app = require(global.settings.app_root + '/app/controllers/app')
, Step = require('step') , Step = require('step')
; ;
// allow lots of emitters to be set to silence warning
app.setMaxListeners(0);
suite('app.test', function() { suite('app.test', function() {

View File

@ -2,7 +2,7 @@ require('../../helper');
require('../../support/assert'); require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, querystring = require('querystring') , querystring = require('querystring')
, _ = require('underscore') , _ = require('underscore')

View File

@ -2,7 +2,7 @@ require('../../helper');
require('../../support/assert'); require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, querystring = require('querystring') , querystring = require('querystring')
, _ = require('underscore') , _ = require('underscore')

View File

@ -2,7 +2,7 @@
require('../../helper'); require('../../helper');
require('../../support/assert'); require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, querystring = require('querystring') , querystring = require('querystring')
, _ = require('underscore') , _ = require('underscore')

View File

@ -2,7 +2,7 @@ require('../../helper');
require('../../support/assert'); require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, querystring = require('querystring') , querystring = require('querystring')
, _ = require('underscore') , _ = require('underscore')

View File

@ -2,7 +2,7 @@ require('../../helper');
require('../../support/assert'); require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, querystring = require('querystring') , querystring = require('querystring')
, _ = require('underscore') , _ = require('underscore')

View File

@ -2,7 +2,7 @@ require('../../helper');
require('../../support/assert'); require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, querystring = require('querystring') , querystring = require('querystring')
, _ = require('underscore') , _ = require('underscore')

View File

@ -2,7 +2,7 @@ require('../../helper');
require('../../support/assert'); require('../../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, querystring = require('querystring') , querystring = require('querystring')
, _ = require('underscore') , _ = require('underscore')

View File

@ -0,0 +1,60 @@
/**
*
* Requires the database and tables setup in config/environments/test.js to exist
* Ensure the user is present in the pgbouncer auth file too
* TODO: Add OAuth tests.
*
* To run this test, ensure that cartodb_test_user_1_db metadata exists
* in Redis for the vizzuality.cartodb.com domain
*
* SELECT 5
* HSET rails:users:vizzuality id 1
* HSET rails:users:vizzuality database_name cartodb_test_user_1_db
*
*/
require('../helper');
require('../support/assert');
var assert = require('assert')
, App = require(global.settings.app_root + '/app/controllers/app')
, querystring = require('querystring')
, _ = require('underscore')
, Step = require('step')
;
suite('timeout', function() {
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/128
test('after configured milliseconds', function(done){
var testTimeout = 10;
//console.log("settings:"); console.dir(global.settings);
var timeoutBackup = global.settings.node_socket_timeout;
global.settings.node_socket_timeout = testTimeout;
var app = App();
Step(
function sendLongQuery() {
var next = this;
assert.response(app, {
url: '/api/v1/sql?q=SELECT+count(*)+FROM+generate_series(1,100000)',
method: 'GET',
headers: {host: 'vizzuality.localhost' }
},{}, function(res, err) {
next(err, res);
});
},
function checkResponse(err, res) {
assert.ok(err);
assert.ok(err.message.match(/hang up/), err);
return null;
},
function finish(err) {
global.settings.node_socket_timeout = timeoutBackup;
done(err);
}
);
});
// TODO: check that the query is interrupted on timeout!
//See #129
});

View File

@ -1,7 +1,7 @@
require('../helper'); require('../helper');
require('../support/assert'); require('../support/assert');
var app = require(global.settings.app_root + '/app/controllers/app') var app = require(global.settings.app_root + '/app/controllers/app')()
, assert = require('assert') , assert = require('assert')
, querystring = require('querystring') , querystring = require('querystring')
, _ = require('underscore') , _ = require('underscore')
@ -103,5 +103,4 @@ test('survives partial transactions', function(done) {
}); });
}); });
}); });

View File

@ -139,6 +139,11 @@ assert.response = function(server, req, res, msg){
if (data) request.write(data); if (data) request.write(data);
request.on('error', function(err){
check();
callback(null, err);
});
request.on('response', function(response){ request.on('response', function(response){
response.body = ''; response.body = '';
response.setEncoding(encoding); response.setEncoding(encoding);