2013-10-29 06:31:11 +08:00
|
|
|
var assert = require('assert')
|
|
|
|
var gonna = require('gonna')
|
|
|
|
|
|
|
|
var _ = require('lodash')
|
2014-03-28 16:17:05 +08:00
|
|
|
var async = require('async')
|
|
|
|
var concat = require('concat-stream')
|
2016-05-04 02:20:04 +08:00
|
|
|
var pg = require('pg')
|
2013-10-29 06:31:11 +08:00
|
|
|
|
2014-03-28 16:17:05 +08:00
|
|
|
var copy = require('../').to
|
2013-10-29 06:31:11 +08:00
|
|
|
|
2014-03-28 16:17:05 +08:00
|
|
|
var client = function() {
|
|
|
|
var client = new pg.Client()
|
|
|
|
client.connect()
|
|
|
|
return client
|
|
|
|
}
|
2013-10-29 06:31:11 +08:00
|
|
|
|
2014-09-16 03:01:39 +08:00
|
|
|
var testConstruction = function() {
|
|
|
|
var txt = 'COPY (SELECT * FROM generate_series(0, 10)) TO STDOUT'
|
|
|
|
var stream = copy(txt, {highWaterMark: 10})
|
|
|
|
assert.equal(stream._readableState.highWaterMark, 10, 'Client should have been set with a correct highWaterMark.')
|
|
|
|
}
|
|
|
|
|
|
|
|
testConstruction()
|
|
|
|
|
2014-03-28 16:17:05 +08:00
|
|
|
var testRange = function(top) {
|
|
|
|
var fromClient = client()
|
2013-10-29 10:27:15 +08:00
|
|
|
var txt = 'COPY (SELECT * from generate_series(0, ' + (top - 1) + ')) TO STDOUT'
|
2016-05-04 02:20:04 +08:00
|
|
|
var res;
|
|
|
|
|
2013-10-29 06:31:11 +08:00
|
|
|
|
|
|
|
var stream = fromClient.query(copy(txt))
|
|
|
|
var done = gonna('finish piping out', 1000, function() {
|
|
|
|
fromClient.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
stream.pipe(concat(function(buf) {
|
2016-05-04 02:20:04 +08:00
|
|
|
res = buf.toString('utf8')
|
|
|
|
}))
|
|
|
|
|
|
|
|
stream.on('end', function() {
|
2013-10-29 10:27:15 +08:00
|
|
|
var expected = _.range(0, top).join('\n') + '\n'
|
2013-10-29 06:31:11 +08:00
|
|
|
assert.equal(res, expected)
|
2013-10-29 10:50:45 +08:00
|
|
|
assert.equal(stream.rowCount, top, 'should have rowCount ' + top + ' but got ' + stream.rowCount)
|
2013-10-29 06:31:11 +08:00
|
|
|
done()
|
2016-05-04 02:20:04 +08:00
|
|
|
});
|
2013-10-29 06:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
testRange(10000)
|
2014-03-28 16:17:05 +08:00
|
|
|
|
2014-04-07 13:02:24 +08:00
|
|
|
var testInternalPostgresError = function() {
|
2014-09-16 12:24:14 +08:00
|
|
|
var cancelClient = client()
|
|
|
|
var queryClient = client()
|
2014-04-07 13:02:24 +08:00
|
|
|
|
|
|
|
var runStream = function(callback) {
|
2014-09-16 12:24:14 +08:00
|
|
|
var txt = "COPY (SELECT pg_sleep(10)) TO STDOUT"
|
|
|
|
var stream = queryClient.query(copy(txt))
|
2014-04-07 13:02:24 +08:00
|
|
|
stream.on('data', function(data) {
|
|
|
|
// Just throw away the data.
|
|
|
|
})
|
|
|
|
stream.on('error', callback)
|
2014-09-16 12:24:14 +08:00
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
var cancelQuery = "SELECT pg_cancel_backend(pid) FROM pg_stat_activity WHERE query ~ 'pg_sleep' AND NOT query ~ 'pg_cancel_backend'"
|
|
|
|
cancelClient.query(cancelQuery)
|
|
|
|
}, 50)
|
2014-04-07 13:02:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
runStream(function(err) {
|
|
|
|
assert.notEqual(err, null)
|
2014-09-16 12:24:14 +08:00
|
|
|
var expectedMessage = 'canceling statement due to user request'
|
|
|
|
assert.notEqual(err.toString().indexOf(expectedMessage), -1, 'Error message should mention reason for query failure.')
|
|
|
|
cancelClient.end()
|
|
|
|
queryClient.end()
|
2014-04-07 13:02:24 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
testInternalPostgresError()
|