From 583d059947be0da7d1ad72420bd1dc4b92c6e912 Mon Sep 17 00:00:00 2001 From: anton Date: Fri, 18 Jan 2013 14:29:37 +0200 Subject: [PATCH] add tests that checks error reporting for incorrect copy to/copy from usage. add tests for fixed bug in native copy from implementation --- test/integration/client/copy-tests.js | 62 +++++++++++++++++++++++++++ test/native/copy-events-tests.js | 4 ++ test/native/copyto-largedata-tests.js | 23 ++++++++++ 3 files changed, 89 insertions(+) create mode 100644 test/native/copyto-largedata-tests.js diff --git a/test/integration/client/copy-tests.js b/test/integration/client/copy-tests.js index c0e53350..577ee96a 100644 --- a/test/integration/client/copy-tests.js +++ b/test/integration/client/copy-tests.js @@ -95,4 +95,66 @@ test('COPY TO, queue queries', function () { }); }); }); +test("COPY TO incorrect usage with large data", function () { + //when many data is loaded from database (and it takes a lot of time) + //there are chance, that query will be canceled before it ends + //but if there are not so much data, cancel message may be + //send after copy query ends + //so we need to test both situations + pg.connect(helper.config, function (error, client) { + assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error)); + //intentionally incorrect usage of copy. + //this has to report error in standart way, instead of just throwing exception + client.query( + "COPY (SELECT GENERATE_SERIES(1, 10000000)) TO STDOUT WITH CSV", + assert.calls(function (error) { + assert.ok(error, "error should be reported when sending copy to query with query method"); + client.query("SELECT 1", assert.calls(function (error, result) { + assert.isNull(error, "incorrect copy usage should not break connection"); + assert.ok(result, "incorrect copy usage should not break connection"); + pg.end(helper.config); + })); + }) + ); + }); +}); +test("COPY TO incorrect usage with small data", function () { + pg.connect(helper.config, function (error, client) { + assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error)); + //intentionally incorrect usage of copy. + //this has to report error in standart way, instead of just throwing exception + client.query( + "COPY (SELECT GENERATE_SERIES(1, 1)) TO STDOUT WITH CSV", + assert.calls(function (error) { + assert.ok(error, "error should be reported when sending copy to query with query method"); + client.query("SELECT 1", assert.calls(function (error, result) { + assert.isNull(error, "incorrect copy usage should not break connection"); + assert.ok(result, "incorrect copy usage should not break connection"); + pg.end(helper.config); + })); + }) + ); + }); +}); + +test("COPY FROM incorrect usage", function () { + pg.connect(helper.config, function (error, client) { + assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error)); + prepareTable(client, function () { + //intentionally incorrect usage of copy. + //this has to report error in standart way, instead of just throwing exception + client.query( + "COPY copy_test from STDIN WITH CSV", + assert.calls(function (error) { + assert.ok(error, "error should be reported when sending copy to query with query method"); + client.query("SELECT 1", assert.calls(function (error, result) { + assert.isNull(error, "incorrect copy usage should not break connection"); + assert.ok(result, "incorrect copy usage should not break connection"); + pg.end(helper.config); + })); + }) + ); + }); + }); +}); diff --git a/test/native/copy-events-tests.js b/test/native/copy-events-tests.js index 0633b566..76f7e292 100644 --- a/test/native/copy-events-tests.js +++ b/test/native/copy-events-tests.js @@ -20,6 +20,10 @@ test('COPY FROM events check', function () { test('COPY TO events check', function () { var con = new Client(helper.config), stdoutStream = con.copyTo('COPY person TO STDOUT'); + assert.emits(con, 'copyOutResponse', + function () {}, + "backend should emit copyOutResponse on copyOutResponse message from server" + ); assert.emits(con, 'copyData', function () { }, diff --git a/test/native/copyto-largedata-tests.js b/test/native/copyto-largedata-tests.js new file mode 100644 index 00000000..518514e5 --- /dev/null +++ b/test/native/copyto-largedata-tests.js @@ -0,0 +1,23 @@ +var helper = require(__dirname+"/../test-helper"); +var Client = require(__dirname + "/../../lib/native"); +test("COPY TO large amount of data from postgres", function () { + //there were a bug in native implementation of COPY TO: + //if there were too much data (if we face situation + //when data is not ready while calling PQgetCopyData); + //while loop in Connection::HandleIOEvent becomes infinite + //in such way hanging node, consumes 100% cpu, and making connection unusable + var con = new Client(helper.config), + rowCount = 100000, + stdoutStream = con.copyTo('COPY (select generate_series(1, ' + rowCount + ')) TO STDOUT'); + con.connect(); + stdoutStream.on('data', function () { + rowCount --; + }); + stdoutStream.on('end', function () { + assert.equal(rowCount, 1, "copy to should load exactly requested number of rows" + rowCount); + con.query("SELECT 1", assert.calls(function (error, result) { + assert.ok(!error && result, "loading large amount of data by copy to should not break connection"); + con.end(); + })); + }); +});