5 Commits

Author SHA1 Message Date
Brian M. Carlson
1822f399d2 Bump version 2014-04-06 11:09:28 -05:00
Brian C
208861d057 Merge pull request #11 from drob/add-test
Adds a smoke test for copying with BINARY.
2014-04-06 11:06:16 -05:00
Brian C
1957554301 Merge pull request #10 from drob/handler-leak
Fixes another event handler leak.
2014-04-06 11:06:09 -05:00
Dan Robinson
bef3ba5fcd Adds a smoke test for copying with BINARY. 2014-04-06 04:50:54 -07:00
Dan Robinson
591a11c955 Fixes another event handler leak.
It turns out 'error' handlers were leaking as well, although more slowly.
2014-04-06 03:49:25 -07:00
5 changed files with 70 additions and 3 deletions

View File

@@ -15,7 +15,7 @@ var CopyStreamQuery = function(text) {
util.inherits(CopyStreamQuery, Transform)
var eventTypes = ['close', 'data', 'end']
var eventTypes = ['close', 'data', 'end', 'error']
CopyStreamQuery.prototype.submit = function(connection) {
connection.query(this.text)

View File

@@ -1,6 +1,6 @@
{
"name": "pg-copy-streams",
"version": "0.2.2",
"version": "0.2.3",
"description": "Low-Level COPY TO and COPY FROM streams for PostgreSQL in JavaScript using",
"main": "index.js",
"scripts": {

65
test/binary.js Normal file
View File

@@ -0,0 +1,65 @@
var assert = require('assert')
var gonna = require('gonna')
var async = require('async')
var concat = require('concat-stream')
var _ = require('lodash')
var pg = require('pg.js')
var from = require('../').from
var to = require('../').to
var testBinaryCopy = function() {
var client = function() {
var client = new pg.Client()
client.connect()
return client
}
var fromClient = client()
var toClient = client()
queries = [
'CREATE TABLE data (num BIGINT, word TEXT)',
'INSERT INTO data (num, word) VALUES (1, \'hello\'), (2, \'other thing\'), (3, \'goodbye\')',
'CREATE TABLE data_copy (LIKE data INCLUDING ALL)'
]
async.eachSeries(queries, _.bind(fromClient.query, fromClient), function(err) {
assert.ifError(err)
var fromStream = fromClient.query(to('COPY (SELECT * FROM data) TO STDOUT BINARY'))
var toStream = toClient.query(from('COPY data_copy FROM STDIN BINARY'))
runStream = function(callback) {
fromStream.on('error', callback)
toStream.on('error', callback)
toStream.on('finish', callback)
fromStream.pipe(toStream)
}
runStream(function(err) {
assert.ifError(err)
toClient.query('SELECT * FROM data_copy ORDER BY num', function(err, res){
assert.equal(res.rowCount, 3, 'expected 3 rows but got ' + res.rowCount)
assert.equal(res.rows[0].num, 1)
assert.equal(res.rows[0].word, 'hello')
assert.equal(res.rows[1].num, 2)
assert.equal(res.rows[1].word, 'other thing')
assert.equal(res.rows[2].num, 3)
assert.equal(res.rows[2].word, 'goodbye')
queries = [
'DROP TABLE data',
'DROP TABLE data_copy'
]
async.each(queries, _.bind(fromClient.query, fromClient), function(err) {
assert.ifError(err)
fromClient.end()
toClient.end()
})
})
})
})
}
testBinaryCopy()

View File

@@ -49,9 +49,10 @@ var testLeak = function(rounds) {
async.timesSeries(rounds, runStream, function(err) {
assert.equal(err, null)
assert.equal(fromClient.connection.stream.listeners('close').length, 0)
assert.equal(fromClient.connection.stream.listeners('data').length, 1)
assert.equal(fromClient.connection.stream.listeners('end').length, 2)
assert.equal(fromClient.connection.stream.listeners('close').length, 0)
assert.equal(fromClient.connection.stream.listeners('error').length, 1)
fromClient.end()
})
}

View File

@@ -1,2 +1,3 @@
require('./copy-from')
require('./copy-to')
require('./binary')