Skip to content

Commit

Permalink
Always call the ConnectionPool.shutdown()-callback.
Browse files Browse the repository at this point in the history
- If shutting down, the callback is being passed an error.
  • Loading branch information
ctavan committed Jan 31, 2012
1 parent 29776b6 commit 54987ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/driver.js
Expand Up @@ -742,14 +742,15 @@ PooledConnection.prototype._getNextCon = function(callback) {
PooledConnection.prototype.shutdown = function(callback) { PooledConnection.prototype.shutdown = function(callback) {
var self = this; var self = this;


callback = callback || function() {};

// Start shutdown mode, causes no new execute()'s to be accepted // Start shutdown mode, causes no new execute()'s to be accepted
if (self.shuttingDown) { if (self.shuttingDown) {
return; var err = new Error('Already shutting down.');
return callback(err);
} }
self.shuttingDown = true; self.shuttingDown = true;


callback = callback || function() {};

// Close all open connections as soon as the pool has drained // Close all open connections as soon as the pool has drained
self.once('drain', function() { self.once('drain', function() {
self._closeConnections(function() { self._closeConnections(function() {
Expand Down
34 changes: 34 additions & 0 deletions test/test_driver.js
Expand Up @@ -1083,3 +1083,37 @@ exports.testPooledConnectionShutdown = function(test, assert) {
test.finish(); test.finish();
}); });
}; };

exports.testPooledConnectionShutdownTwice = function(test, assert) {
var hosts = ['127.0.0.1:19170'];
var conn = new PooledConnection({'hosts': hosts, 'keyspace': 'Keyspace1'});

var expected = 100;
var cbcount = 0;
var spy = function(err, res) {
assert.ifError(err);
cbcount++;
};

for (var i = 0; i < expected; i++) {
(function(index) {
conn.execute('UPDATE CfUtf8 SET ? = ? WHERE KEY = ?', ['col', 'val', 'key'+index], spy);
})(i);
}

assert.ok(!conn.shuttingDown);
conn.shutdown(function(err) {
assert.ifError(err);
assert.equal(cbcount, expected);
assert.ok(secondCbCalledImmediatelyWithError);
test.finish();
});

// Make sure second callback gets called immediately with an error
var secondCbCalledImmediatelyWithError = false;
assert.ok(conn.shuttingDown);
conn.shutdown(function(err) {
assert.ok(err);
secondCbCalledImmediatelyWithError = true;
});
};

0 comments on commit 54987ca

Please sign in to comment.