Test for the COPY FROM + disconnect

This commit is contained in:
Rafa de la Torre 2018-06-11 19:17:23 +02:00
parent 66664de286
commit b1073dc401

View File

@ -332,6 +332,9 @@ describe('copy-endpoints db connections', function() {
}); });
describe('copy-endpoints client disconnection', function() { describe('copy-endpoints client disconnection', function() {
// Give it enough time to connect and issue the query but not too much so as to disconnect in the middle of the query
const client_disconnect_timeout = 10;
before(function() { before(function() {
this.db_pool_size = global.settings.db_pool_size; this.db_pool_size = global.settings.db_pool_size;
global.settings.db_pool_size = 1; global.settings.db_pool_size = 1;
@ -341,34 +344,51 @@ describe('copy-endpoints client disconnection', function() {
global.settings.db_pool_size = this.db_pool_size; global.settings.db_pool_size = this.db_pool_size;
}); });
it('copy to returns the connection to the pool if the client disconnects', function(done) { var assertCanReuseConnection = function (done) {
assert.response(server, {
var assertCanReuseConnection = function (done) { url: '/api/v1/sql?' + querystring.stringify({
assert.response(server, { q: 'SELECT 1',
url: '/api/v1/sql?' + querystring.stringify({ }),
q: 'SELECT 1', headers: { host: 'vizzuality.cartodb.com' },
}), method: 'GET'
headers: { host: 'vizzuality.cartodb.com' }, }, {}, function(err, res) {
method: 'GET' assert.ifError(err);
}, {}, function(err, res) { assert.ok(res.statusCode === 200);
assert.ifError(err); done();
assert.ok(res.statusCode === 200); });
done(); };
});
};
it('COPY TO returns the connection to the pool if the client disconnects', function(done) {
assert.response(server, { assert.response(server, {
url: '/api/v1/sql/copyto?' + querystring.stringify({ url: '/api/v1/sql/copyto?' + querystring.stringify({
q: 'COPY (SELECT * FROM generate_series(1, 100000)) TO STDOUT', q: 'COPY (SELECT * FROM generate_series(1, 100000)) TO STDOUT',
}), }),
headers: { host: 'vizzuality.cartodb.com' }, headers: { host: 'vizzuality.cartodb.com' },
method: 'GET', method: 'GET',
timeout: 10 timeout: client_disconnect_timeout
}, {}, function(err, res) { }, {}, function(err) {
// we're expecting a timeout error // we're expecting a timeout error
assert.ok(err); assert.ok(err);
assert.ok(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT'); assert.ok(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT');
assertCanReuseConnection(done); assertCanReuseConnection(done);
}); });
}); });
it('COPY FROM returns the connection to the pool if the client disconnects', function(done) {
assert.response(server, {
url: '/api/v1/sql/copyfrom?' + querystring.stringify({
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)",
}),
headers: { host: 'vizzuality.cartodb.com' },
method: 'POST',
data: fs.createReadStream(__dirname + '/../support/csv/copy_test_table.csv'),
timeout: client_disconnect_timeout
}, {}, function(err) {
// we're expecting a timeout error
assert.ok(err);
assert.ok(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT');
assertCanReuseConnection(done);
});
});
}); });