Skip to content

Commit 3867851

Browse files
committed
Make cancel query tests pass
1 parent dd2e71c commit 3867851

File tree

7 files changed

+26
-18
lines changed

7 files changed

+26
-18
lines changed

lib/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ PG.prototype.connect = function(config, callback) {
5555

5656
// cancel the query runned by the given client
5757
PG.prototype.cancel = function(config, client, query) {
58+
if(client.native) {
59+
return client.cancel(query);
60+
}
5861
var c = config;
5962
//allow for no config to be passed
6063
if(typeof c === 'function') {

lib/native/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,11 @@ Client.prototype._pulseQueryQueue = function(initialConnection) {
146146
self._pulseQueryQueue();
147147
});
148148
};
149+
150+
Client.prototype.cancel = function(query) {
151+
if(this._activeQuery == query) {
152+
this.native.cancel(function() {});
153+
} else if (this._queryQueue.indexOf(query) != -1) {
154+
this._queryQueue.splice(this._queryQueue.indexOf(query), 1);
155+
}
156+
};

lib/native/query.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ NativeQuery.prototype.handleError = function(err) {
7070
self.emit('error', err);
7171
}
7272
self.state = 'error';
73-
}
73+
};
7474

7575
NativeQuery.prototype.submit = function(client) {
7676
this.state = 'running';
@@ -104,7 +104,7 @@ NativeQuery.prototype.submit = function(client) {
104104
if(self.callback) {
105105
self.callback(null, result);
106106
}
107-
}
107+
};
108108

109109
if(process.domain) {
110110
after = process.domain.bind(after);
@@ -124,11 +124,11 @@ NativeQuery.prototype.submit = function(client) {
124124
if(err) return self.handleError(err);
125125
client.namedQueries[self.name] = true;
126126
return self.native.execute(self.name, values, after);
127-
})
127+
});
128128
}
129129
else if(this.values) {
130-
var values = this.values.map(utils.prepareValue);
131-
this.native.query(this.text, values, after);
130+
var vals = this.values.map(utils.prepareValue);
131+
this.native.query(this.text, vals, after);
132132
} else {
133133
this.native.query(this.text, after);
134134
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"nan": "~1.3.0",
2424
"packet-reader": "0.2.0",
2525
"pg-connection-string": "0.1.1",
26-
"pg-native": "1.0.2",
26+
"pg-native": "1.1.0",
2727
"pg-types": "1.4.0",
2828
"pgpass": "0.0.3"
2929
},

test/integration/client/cancel-query-tests.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
return console.log('cancel-query-tests.js: GET TO PASS');
21
var helper = require(__dirname+"/test-helper");
32

43
//before running this test make sure you run the script create-test-tables
@@ -10,35 +9,29 @@ test("cancellation of a query", function() {
109

1110
client.on('drain', client.end.bind(client));
1211

13-
var rows1 = 0, rows2 = 0, rows3 = 0, rows4 = 0;
12+
var rows3 = 0;
1413

1514
var query1 = client.query(qry);
1615
query1.on('row', function(row) {
17-
rows1++;
16+
throw new Error('Should not emit a row')
1817
});
1918
var query2 = client.query(qry);
2019
query2.on('row', function(row) {
21-
rows2++;
20+
throw new Error('Should not emit a row')
2221
});
2322
var query3 = client.query(qry);
2423
query3.on('row', function(row) {
2524
rows3++;
2625
});
2726
var query4 = client.query(qry);
2827
query4.on('row', function(row) {
29-
rows4++;
28+
throw new Error('Should not emit a row')
3029
});
3130

3231
helper.pg.cancel(helper.config, client, query1);
3332
helper.pg.cancel(helper.config, client, query2);
3433
helper.pg.cancel(helper.config, client, query4);
3534

36-
setTimeout(function() {
37-
assert.equal(rows1, 0);
38-
assert.equal(rows2, 0);
39-
assert.equal(rows4, 0);
40-
}, 2000);
41-
4235
assert.emits(query3, 'end', function() {
4336
test("returned right number of rows", function() {
4437
assert.equal(rows3, 26);

test/integration/client/results-as-array-tests.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
console.log('results-as-array: GET TO PASS')
2+
console.log('results-as-array: GET TO PASS')
3+
console.log('results-as-array: GET TO PASS')
4+
console.log('results-as-array: GET TO PASS')
15
return console.log('results-as-array: GET TO PASS')
26
var util = require('util');
37
var helper = require('./test-helper');

test/integration/client/simple-query-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test("multiple simple queries", function() {
5151
});
5252

5353
test("multiple select statements", function() {
54-
return console.log('MUST SUPPORT MULTIPLE SIMPLE QURIES')
54+
return console.log('DEPRECATED - multiple queries')
5555
var client = helper.client();
5656
client.query("create temp table boom(age integer); insert into boom(age) values(1); insert into boom(age) values(2); insert into boom(age) values(3)");
5757
client.query({text: "create temp table bang(name varchar(5)); insert into bang(name) values('zoom');"});

0 commit comments

Comments
 (0)