Skip to content

Commit 583d059

Browse files
antonbrianc
anton
authored andcommitted
add tests that checks error reporting for incorrect copy to/copy from usage. add tests for fixed bug in native copy from implementation
1 parent 7ca21ac commit 583d059

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

test/integration/client/copy-tests.js

+62
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,66 @@ test('COPY TO, queue queries', function () {
9595
});
9696
});
9797
});
98+
test("COPY TO incorrect usage with large data", function () {
99+
//when many data is loaded from database (and it takes a lot of time)
100+
//there are chance, that query will be canceled before it ends
101+
//but if there are not so much data, cancel message may be
102+
//send after copy query ends
103+
//so we need to test both situations
104+
pg.connect(helper.config, function (error, client) {
105+
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
106+
//intentionally incorrect usage of copy.
107+
//this has to report error in standart way, instead of just throwing exception
108+
client.query(
109+
"COPY (SELECT GENERATE_SERIES(1, 10000000)) TO STDOUT WITH CSV",
110+
assert.calls(function (error) {
111+
assert.ok(error, "error should be reported when sending copy to query with query method");
112+
client.query("SELECT 1", assert.calls(function (error, result) {
113+
assert.isNull(error, "incorrect copy usage should not break connection");
114+
assert.ok(result, "incorrect copy usage should not break connection");
115+
pg.end(helper.config);
116+
}));
117+
})
118+
);
119+
});
120+
});
121+
test("COPY TO incorrect usage with small data", function () {
122+
pg.connect(helper.config, function (error, client) {
123+
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
124+
//intentionally incorrect usage of copy.
125+
//this has to report error in standart way, instead of just throwing exception
126+
client.query(
127+
"COPY (SELECT GENERATE_SERIES(1, 1)) TO STDOUT WITH CSV",
128+
assert.calls(function (error) {
129+
assert.ok(error, "error should be reported when sending copy to query with query method");
130+
client.query("SELECT 1", assert.calls(function (error, result) {
131+
assert.isNull(error, "incorrect copy usage should not break connection");
132+
assert.ok(result, "incorrect copy usage should not break connection");
133+
pg.end(helper.config);
134+
}));
135+
})
136+
);
137+
});
138+
});
139+
140+
test("COPY FROM incorrect usage", function () {
141+
pg.connect(helper.config, function (error, client) {
142+
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
143+
prepareTable(client, function () {
144+
//intentionally incorrect usage of copy.
145+
//this has to report error in standart way, instead of just throwing exception
146+
client.query(
147+
"COPY copy_test from STDIN WITH CSV",
148+
assert.calls(function (error) {
149+
assert.ok(error, "error should be reported when sending copy to query with query method");
150+
client.query("SELECT 1", assert.calls(function (error, result) {
151+
assert.isNull(error, "incorrect copy usage should not break connection");
152+
assert.ok(result, "incorrect copy usage should not break connection");
153+
pg.end(helper.config);
154+
}));
155+
})
156+
);
157+
});
158+
});
159+
});
98160

test/native/copy-events-tests.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ test('COPY FROM events check', function () {
2020
test('COPY TO events check', function () {
2121
var con = new Client(helper.config),
2222
stdoutStream = con.copyTo('COPY person TO STDOUT');
23+
assert.emits(con, 'copyOutResponse',
24+
function () {},
25+
"backend should emit copyOutResponse on copyOutResponse message from server"
26+
);
2327
assert.emits(con, 'copyData',
2428
function () {
2529
},

test/native/copyto-largedata-tests.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var helper = require(__dirname+"/../test-helper");
2+
var Client = require(__dirname + "/../../lib/native");
3+
test("COPY TO large amount of data from postgres", function () {
4+
//there were a bug in native implementation of COPY TO:
5+
//if there were too much data (if we face situation
6+
//when data is not ready while calling PQgetCopyData);
7+
//while loop in Connection::HandleIOEvent becomes infinite
8+
//in such way hanging node, consumes 100% cpu, and making connection unusable
9+
var con = new Client(helper.config),
10+
rowCount = 100000,
11+
stdoutStream = con.copyTo('COPY (select generate_series(1, ' + rowCount + ')) TO STDOUT');
12+
con.connect();
13+
stdoutStream.on('data', function () {
14+
rowCount --;
15+
});
16+
stdoutStream.on('end', function () {
17+
assert.equal(rowCount, 1, "copy to should load exactly requested number of rows" + rowCount);
18+
con.query("SELECT 1", assert.calls(function (error, result) {
19+
assert.ok(!error && result, "loading large amount of data by copy to should not break connection");
20+
con.end();
21+
}));
22+
});
23+
});

0 commit comments

Comments
 (0)