2019-05-29 01:24:29 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const querystring = require('querystring');
|
2019-10-04 00:24:39 +08:00
|
|
|
const StatsClient = require('../../lib/stats/client');
|
2019-05-29 01:24:29 +08:00
|
|
|
const statsClient = StatsClient.getInstance(global.settings.statsd);
|
2019-10-04 00:24:39 +08:00
|
|
|
const server = require('../../lib/server')(statsClient);
|
2019-05-29 01:24:29 +08:00
|
|
|
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-05-29 01:24:29 +08:00
|
|
|
|
2019-12-26 23:10:41 +08:00
|
|
|
function * counterGenerator (timeout, maxCount) {
|
2019-05-29 01:24:29 +08:00
|
|
|
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-05-29 01:24:29 +08:00
|
|
|
}
|
2019-12-26 23:43:12 +08:00
|
|
|
/* eslint-enable */
|
2019-12-26 23:10:41 +08:00
|
|
|
|
2019-11-20 19:09:06 +08:00
|
|
|
// generate also a delayed final marker (null) to simplify handling into a stream.
|
|
|
|
yield new Promise(resolve => setTimeout(() => resolve(null), timeout));
|
2019-05-29 01:24:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class CounterStream extends Readable {
|
2019-12-24 01:19:08 +08:00
|
|
|
constructor (generator, ...args) {
|
2019-05-29 01:24:29 +08:00
|
|
|
super(...args);
|
|
|
|
this.generator = generator;
|
|
|
|
}
|
|
|
|
|
|
|
|
_read () {
|
|
|
|
const res = this.generator.next();
|
2019-11-20 19:09:06 +08:00
|
|
|
if (!res.done) {
|
|
|
|
res.value.then(value => this.push(value));
|
|
|
|
}
|
2019-05-29 01:24:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('COPY FROM throttle', function () {
|
2019-12-24 01:19:08 +08:00
|
|
|
before(function () {
|
2019-05-29 01:24:29 +08:00
|
|
|
this.copy_from_minimum_input_speed = global.settings.copy_from_minimum_input_speed;
|
2019-11-20 19:09:06 +08:00
|
|
|
global.settings.copy_from_minimum_input_speed = 2;
|
2019-05-29 01:24:29 +08:00
|
|
|
|
|
|
|
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 () {
|
2019-05-29 01:24:29 +08:00
|
|
|
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 });
|
2019-05-29 01:24:29 +08:00
|
|
|
|
|
|
|
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);
|
2019-05-29 01:24:29 +08:00
|
|
|
|
|
|
|
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) {
|
2019-05-29 01:24:29 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function (done) {
|
|
|
|
this.listener.close(done);
|
|
|
|
});
|
|
|
|
|
2019-11-20 19:09:06 +08:00
|
|
|
it('hangs while sending data', function (done) {
|
2019-05-29 01:24:29 +08:00
|
|
|
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' },
|
2019-11-20 19:09:06 +08:00
|
|
|
body: new CounterStream(counterGenerator(600)),
|
2019-05-29 01:24:29 +08:00
|
|
|
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);
|
2019-05-29 01:24:29 +08:00
|
|
|
body = JSON.parse(body);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.deepStrictEqual(body, { error: ['Connection closed by server: input data too slow'] });
|
2019-05-29 01:24:29 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2019-11-20 19:09:06 +08:00
|
|
|
|
|
|
|
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) => {
|
2019-11-20 19:09:06 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
2019-11-20 19:09:06 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2019-05-29 01:24:29 +08:00
|
|
|
});
|