Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var Client = function(config) {
ssl: this.connectionParameters.ssl
});
this.queryQueue = [];
this.sentQueryQueue = [];
this.sendImmediately = true;
this.binary = c.binary || defaults.binary;
this.encoding = 'utf8';
this.processID = null;
Expand Down Expand Up @@ -154,6 +156,7 @@ Client.prototype.connect = function(callback) {
var activeQuery = self.activeQuery;
self.activeQuery = null;
self.readyForQuery = true;
self.handshakeDone = true;
self._pulseQueryQueue();
if(activeQuery) {
activeQuery.handleReadyForQuery();
Expand Down Expand Up @@ -279,20 +282,32 @@ Client.prototype.escapeLiteral = function(str) {
};

Client.prototype._pulseQueryQueue = function() {
if(!this.handshakeDone)
return;

while((this.sendImmediately && !this.blocked) || (this.activeQuery === null && this.sentQueryQueue.length === 0)) {
var query = this.queryQueue.shift();
if(!query)
break;

query.submit(this.connection);
this.blocked = query.blocking;
this.sentQueryQueue.push(query);
}

if(this.readyForQuery===true) {
this.activeQuery = this.queryQueue.shift();
this.activeQuery = this.sentQueryQueue.shift();
if(this.activeQuery) {
this.readyForQuery = false;
this.hasExecuted = true;
this.activeQuery.submit(this.connection);
} else if(this.hasExecuted) {
this.activeQuery = null;
this.emit('drain');
}
}
};

Client.prototype._copy = function (text, stream) {
Client.prototype._copy = function (text, stream, blocking) {
var config = {};
config.text = text;
config.stream = stream;
Expand All @@ -304,14 +319,14 @@ Client.prototype._copy = function (text, stream) {
}
};
var query = new Query(config);
query.blocking = blocking;
this.queryQueue.push(query);
this._pulseQueryQueue();
return config.stream;

};

Client.prototype.copyFrom = function (text) {
return this._copy(text, new CopyFromStream());
return this._copy(text, new CopyFromStream(), true);
};

Client.prototype.copyTo = function (text) {
Expand Down
33 changes: 33 additions & 0 deletions test/unit/client/simple-query-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test('executing query', function() {

test("multiple in the queue", function() {
var client = helper.client();
client.sendImmediately = false;
var connection = client.connection;
var queries = connection.queries;
client.query('one');
Expand Down Expand Up @@ -59,6 +60,38 @@ test('executing query', function() {
assert.equal(queries[2], 'three');
});
});

test("multiple in the queue, sending immediately", function() {
var client = helper.client();
client.sendImmediately = true;
var connection = client.connection;
var queries = connection.queries;
client.query('one');
client.query('two');
client.query('three');
assert.empty(queries);

test("after one ready for query",function() {
connection.emit('readyForQuery');
assert.lengthIs(queries, 3);
assert.equal(queries[0], "one");
});

test('after two ready for query', function() {
connection.emit('readyForQuery');
assert.lengthIs(queries, 3);
});

test("after a bunch more", function() {
connection.emit('readyForQuery');
connection.emit('readyForQuery');
connection.emit('readyForQuery');
assert.lengthIs(queries, 3);
assert.equal(queries[0], "one");
assert.equal(queries[1], 'two');
assert.equal(queries[2], 'three');
});
});
});

test("query event binding and flow", function() {
Expand Down