2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-06-18 16:53:44 +08:00
|
|
|
const querystring = require('querystring');
|
2019-10-04 00:24:39 +08:00
|
|
|
const StatsClient = require('../../lib/stats/client');
|
2018-06-18 16:53:44 +08:00
|
|
|
const statsClient = StatsClient.getInstance(global.settings.statsd);
|
2019-10-04 00:24:39 +08:00
|
|
|
const server = require('../../lib/server')(statsClient);
|
2018-06-18 16:53:44 +08:00
|
|
|
const request = require('request');
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
const copyQuery = `COPY (
|
|
|
|
INSERT INTO copy_to_test
|
|
|
|
SELECT updated_at
|
2018-06-20 16:48:03 +08:00
|
|
|
FROM generate_series(
|
|
|
|
'1984-06-14 01:00:00'::timestamp,
|
|
|
|
'2018-06-14 01:00:00'::timestamp,
|
|
|
|
'1 hour'::interval
|
|
|
|
) updated_at
|
2018-06-18 16:53:44 +08:00
|
|
|
RETURNING updated_at
|
|
|
|
) TO STDOUT`;
|
|
|
|
|
|
|
|
const createTableQuery = `CREATE TABLE copy_to_test AS
|
|
|
|
(SELECT '2018-06-15 14:49:05.126415+00'::timestamp AS updated_at)`;
|
|
|
|
|
|
|
|
const dropTableQuery = `DROP TABLE copy_to_test`;
|
|
|
|
|
|
|
|
const countQuery = `SELECT count(1) as count FROM copy_to_test`;
|
|
|
|
|
|
|
|
function countInsertedRows (host, port, callback) {
|
|
|
|
setTimeout(function () {
|
|
|
|
const count = querystring.stringify({ q: countQuery, api_key: 1234 });
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
url: `http://${host}:${port}/api/v1/sql?${count}`,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
|
|
|
|
request(options, function (err, res, body) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(res.statusCode, 200);
|
2018-06-20 16:48:03 +08:00
|
|
|
const result = JSON.parse(body);
|
2018-06-18 16:53:44 +08:00
|
|
|
callback(null, result);
|
|
|
|
});
|
2018-06-21 01:03:23 +08:00
|
|
|
}, 100);
|
2018-06-18 16:53:44 +08:00
|
|
|
}
|
|
|
|
|
2018-06-18 16:56:14 +08:00
|
|
|
describe('Cancel "copy to" commands', function () {
|
2018-06-21 19:06:38 +08:00
|
|
|
before(function() {
|
|
|
|
this.db_pool_size = global.settings.db_pool_size;
|
|
|
|
global.settings.db_pool_size = 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
global.settings.db_pool_size = this.db_pool_size;
|
|
|
|
});
|
|
|
|
|
2018-06-18 16:53:44 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function (done) {
|
|
|
|
const { host, port } = this;
|
|
|
|
|
|
|
|
const dropTable = querystring.stringify({ q: dropTableQuery, api_key: 1234 });
|
|
|
|
|
2018-06-21 00:39:17 +08:00
|
|
|
const dropTableOptions = {
|
2018-06-18 16:53:44 +08:00
|
|
|
url: `http://${host}:${port}/api/v1/sql?${dropTable}`,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
|
2018-06-21 00:39:17 +08:00
|
|
|
request(dropTableOptions, function (err, res) {
|
2018-06-18 16:53:44 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function (done) {
|
|
|
|
this.listener.close(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('abort on response', function (done) {
|
|
|
|
const { host, port } = this;
|
|
|
|
|
|
|
|
const copy = querystring.stringify({ q: copyQuery, api_key: 1234 });
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
url: `http://${host}:${port}/api/v1/sql/copyto?${copy}`,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
|
|
|
|
const req = request(options);
|
|
|
|
|
|
|
|
req.on('response', function () {
|
|
|
|
req.abort();
|
|
|
|
|
|
|
|
countInsertedRows(host, port, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(result.rows[0].count, 1);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('abort on data', function (done) {
|
|
|
|
const { host, port } = this;
|
|
|
|
|
|
|
|
const copy = querystring.stringify({ q: copyQuery, api_key: 1234 });
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
url: `http://${host}:${port}/api/v1/sql/copyto?${copy}`,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
|
|
|
|
const req = request(options);
|
|
|
|
|
|
|
|
req.once('data', function () {
|
|
|
|
req.abort();
|
|
|
|
|
|
|
|
countInsertedRows(host, port, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(result.rows[0].count, 1);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('destroy on data', function (done) {
|
|
|
|
const { host, port } = this;
|
|
|
|
|
|
|
|
const copy = querystring.stringify({ q: copyQuery, api_key: 1234 });
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
url: `http://${host}:${port}/api/v1/sql/copyto?${copy}`,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
|
|
|
|
const req = request(options);
|
|
|
|
|
|
|
|
let response;
|
|
|
|
|
|
|
|
req.on('response', function (res) {
|
|
|
|
response = res;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.once('data', function () {
|
|
|
|
response.destroy();
|
|
|
|
|
|
|
|
countInsertedRows(host, port, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(result.rows[0].count, 1);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('destroy on response', function (done) {
|
|
|
|
const { host, port } = this;
|
|
|
|
|
|
|
|
const copy = querystring.stringify({ q: copyQuery, api_key: 1234 });
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
url: `http://${host}:${port}/api/v1/sql/copyto?${copy}`,
|
|
|
|
headers: { host: 'vizzuality.cartodb.com' },
|
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
|
|
|
|
const req = request(options);
|
|
|
|
|
|
|
|
req.on('response', function (response) {
|
|
|
|
response.destroy();
|
|
|
|
|
|
|
|
countInsertedRows(host, port, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(result.rows[0].count, 1);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|