CartoDB-SQL-API/test/acceptance/copy-throttle-test.js

169 lines
4.9 KiB
JavaScript
Raw Normal View History

'use strict';
const querystring = require('querystring');
const StatsClient = require('../../lib/stats/client');
const statsClient = StatsClient.getInstance(global.settings.statsd);
const server = require('../../lib/server')(statsClient);
const request = require('request');
const assert = require('assert');
const { Readable } = require('stream');
2019-12-24 01:19:08 +08:00
const createTableQuery = 'CREATE TABLE copy_from_throttle AS (SELECT 0::text AS counter)';
const dropTableQuery = 'DROP TABLE copy_from_throttle';
2019-12-26 23:10:41 +08:00
function * counterGenerator (timeout, maxCount) {
let counter = 0;
2019-12-26 23:43:12 +08:00
/* eslint-disable */
while (!maxCount || counter <= maxCount) {
yield new Promise(resolve => setTimeout(() => resolve(`${counter++}`), timeout));
}
2019-12-26 23:43:12 +08:00
/* eslint-enable */
2019-12-26 23:10:41 +08:00
// generate also a delayed final marker (null) to simplify handling into a stream.
yield new Promise(resolve => setTimeout(() => resolve(null), timeout));
}
class CounterStream extends Readable {
2019-12-24 01:19:08 +08:00
constructor (generator, ...args) {
super(...args);
this.generator = generator;
}
_read () {
const res = this.generator.next();
if (!res.done) {
res.value.then(value => this.push(value));
}
}
}
describe('COPY FROM throttle', function () {
2019-12-24 01:19:08 +08:00
before(function () {
this.copy_from_minimum_input_speed = global.settings.copy_from_minimum_input_speed;
global.settings.copy_from_minimum_input_speed = 2;
this.copy_from_maximum_slow_input_speed_interval = global.settings.copy_from_maximum_slow_input_speed_interval;
global.settings.copy_from_maximum_slow_input_speed_interval = 1;
});
2019-12-24 01:19:08 +08:00
after(function () {
global.settings.copy_from_first_chunk_timeout = this.copy_from_first_chunk_timeout;
global.settings.copy_from_maximum_slow_input_speed_interval = this.copy_from_maximum_slow_input_speed_interval;
});
beforeEach(function (done) {
this.listener = server.listen(0, '127.0.0.1');
this.listener.on('error', done);
this.listener.on('listening', () => {
const { address, port } = this.listener.address();
this.host = address;
this.port = port;
done();
});
});
beforeEach(function (done) {
const { host, port } = this;
2019-12-24 01:19:08 +08:00
const createTable = querystring.stringify({ q: createTableQuery, api_key: 1234 });
const createTableOptions = {
url: `http://${host}:${port}/api/v1/sql?${createTable}`,
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
};
request(createTableOptions, function (err, res) {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(res.statusCode, 200);
done();
});
});
afterEach(function (done) {
const { host, port } = this;
const dropTable = querystring.stringify({ q: dropTableQuery, api_key: 1234 });
const dropTableOptions = {
url: `http://${host}:${port}/api/v1/sql?${dropTable}`,
headers: { host: 'vizzuality.cartodb.com' },
method: 'GET'
};
2019-05-29 01:30:17 +08:00
request(dropTableOptions, function (err) {
if (err) {
return done(err);
}
done();
});
});
afterEach(function (done) {
this.listener.close(done);
});
it('hangs while sending data', function (done) {
const { host, port } = this;
const copy = querystring.stringify({
q: "COPY copy_from_throttle (counter) FROM STDIN WITH (FORMAT CSV, DELIMITER ',')",
api_key: 1234
});
const options = {
url: `http://${host}:${port}/api/v1/sql/copyfrom?${copy}`,
headers: { host: 'vizzuality.cartodb.com' },
body: new CounterStream(counterGenerator(600)),
method: 'POST'
};
request(options, (err, res, body) => {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(res.statusCode, 400);
body = JSON.parse(body);
2019-12-26 21:01:18 +08:00
assert.deepStrictEqual(body, { error: ['Connection closed by server: input data too slow'] });
done();
});
});
it('does not hang while sending data', function (done) {
const { host, port } = this;
const copy = querystring.stringify({
q: "COPY copy_from_throttle (counter) FROM STDIN WITH (FORMAT CSV, DELIMITER ',')",
api_key: 1234
});
const options = {
url: `http://${host}:${port}/api/v1/sql/copyfrom?${copy}`,
headers: { host: 'vizzuality.cartodb.com' },
body: new CounterStream(counterGenerator(400, 7)),
method: 'POST'
};
2019-11-20 20:06:36 +08:00
request(options, (err, res) => {
if (err) {
return done(err);
}
2019-12-26 21:01:18 +08:00
assert.strictEqual(res.statusCode, 200);
done();
});
});
});