capture error message from intermittent failure of copy-tests.js

This commit is contained in:
Ricky Ng-Adam 2014-04-24 08:55:00 +08:00
parent 6216f6089f
commit fbedaf4587

View File

@ -8,7 +8,8 @@ var prepareTable = function (client, callback) {
client.query(
'CREATE TEMP TABLE copy_test (id SERIAL, name CHARACTER VARYING(10), age INT)',
assert.calls(function (err, result) {
assert.equal(err, null, "create table query should not fail");
assert.equal(err, null,
err && err.message ? "create table query should not fail: " + err.message : null);
callback();
})
);
@ -19,7 +20,7 @@ test('COPY FROM', function () {
prepareTable(client, function () {
var stream = client.copyFrom("COPY copy_test (name, age) FROM stdin WITH CSV");
stream.on('error', function (error) {
assert.ok(false, "COPY FROM stream should not emit errors" + helper.sys.inspect(error));
assert.ok(false, "COPY FROM stream should not emit errors" + helper.sys.inspect(error));
});
for (var i = 0; i < ROWS_TO_INSERT; i++) {
stream.write( String(Date.now() + Math.random()).slice(0,10) + ',' + i + '\n');
@ -44,11 +45,11 @@ test('COPY TO', function () {
var stream = client.copyTo("COPY person (id, name, age) TO stdin WITH CSV");
var buf = new Buffer(0);
stream.on('error', function (error) {
assert.ok(false, "COPY TO stream should not emit errors" + helper.sys.inspect(error));
assert.ok(false, "COPY TO stream should not emit errors" + helper.sys.inspect(error));
});
assert.emits(stream, 'data', function (chunk) {
buf = Buffer.concat([buf, chunk]);
}, "COPY IN stream should emit data event for each row");
buf = Buffer.concat([buf, chunk]);
}, "COPY IN stream should emit data event for each row");
assert.emits(stream, 'end', function () {
var lines = buf.toString().split('\n');
assert.equal(lines.length >= 0, true, "copy in should return rows saved by copy from");
@ -73,7 +74,7 @@ test('COPY TO, queue queries', function () {
});
var stream = client.copyTo("COPY person (id, name, age) TO stdin WITH CSV");
//imitate long query, to make impossible,
//that copy query end callback runs after
//that copy query end callback runs after
//second query callback
client.query("SELECT pg_sleep(1)", function () {
query2Done = true;
@ -81,11 +82,11 @@ test('COPY TO, queue queries', function () {
});
var buf = new Buffer(0);
stream.on('error', function (error) {
assert.ok(false, "COPY TO stream should not emit errors" + helper.sys.inspect(error));
assert.ok(false, "COPY TO stream should not emit errors" + helper.sys.inspect(error));
});
assert.emits(stream, 'data', function (chunk) {
buf = Buffer.concat([buf, chunk]);
}, "COPY IN stream should emit data event for each row");
buf = Buffer.concat([buf, chunk]);
}, "COPY IN stream should emit data event for each row");
assert.emits(stream, 'end', function () {
copyQueryDone = true;
assert.ok(query1Done && ! query2Done, "copy query has to be executed before second query and after first");
@ -100,14 +101,14 @@ test('COPY TO, queue queries', function () {
test("COPY TO incorrect usage with large data", function () {
if(helper.config.native) return false;
//when many data is loaded from database (and it takes a lot of time)
//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
//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, assert.calls(function (error, client, done) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
//intentionally incorrect usage of copy.
//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",
@ -127,7 +128,7 @@ test("COPY TO incorrect usage with small data", function () {
if(helper.config.native) return false;
pg.connect(helper.config, assert.calls(function (error, client, done) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
//intentionally incorrect usage of copy.
//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",
@ -147,7 +148,7 @@ test("COPY FROM incorrect usage", function () {
pg.connect(helper.config, function (error, client, done) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
prepareTable(client, function () {
//intentionally incorrect usage of copy.
//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",
@ -157,7 +158,7 @@ test("COPY FROM incorrect usage", function () {
assert.isNull(error, "incorrect copy usage should not break connection: " + error);
assert.ok(result, "incorrect copy usage should not break connection");
done();
pg.end(helper.config);
pg.end(helper.config);
}));
})
);