From 238571b5b97280f3448b5f7fa0325b2f7eec453e Mon Sep 17 00:00:00 2001 From: Calvin French-Owen Date: Thu, 25 Oct 2012 16:46:24 -0700 Subject: [PATCH] Adding destroy method to connection pool as well as tests --- lib/connection.js | 9 +++++++++ lib/pool.js | 30 ++++++++++++++++++++++++++++++ test/cql2.js | 11 +++++++++++ test/cql3.js | 11 +++++++++++ test/thrift.js | 26 ++++++++++++++++++++++++++ 5 files changed, 87 insertions(+) diff --git a/lib/connection.js b/lib/connection.js index cee9a0f..9797e33 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -513,5 +513,14 @@ Connection.prototype.dropKeyspace = function(keyspace, callback){ Connection.prototype.close = function(){ this._connection.end(); }; + + +/** + * Destroys the connection to the server + */ +Connection.prototype.destroy = function() { + this._connection.destroy(); +}; + //export our client module.exports = Connection; diff --git a/lib/pool.js b/lib/pool.js index b169b12..b2cd86a 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -338,4 +338,34 @@ Pool.prototype.close = function(){ this.clients[i].close(); } }; + + +/** + * Destroys all open connections. + */ +Pool.prototype.destroy = function(){ + var self = this, i = 0, j = 0, len = this.clients.length; + + // Make sure no intervals get set + self.closing = true; + + clearInterval(this.retryInterval); + + if (len === 0){ + this.emit('close'); + } + + function closed(){ + j += 1; + if(j === len){ + self.emit('close'); + } + } + + for(; i < len; i += 1){ + this.clients[i].on('close', closed); + this.clients[i].destroy(); + } +}; + module.exports = Pool; diff --git a/test/cql2.js b/test/cql2.js index 315fc3f..f896adb 100644 --- a/test/cql2.js +++ b/test/cql2.js @@ -25,6 +25,17 @@ module.exports = { }); }, + 'test connection.destroy':function(test, assert){ + var destroyedConn = new Helenus.ConnectionPool(poolConfig); + destroyedConn.on('close', function () { + test.finish(); + }); + destroyedConn.connect(function(err){ + assert.ifError(err); + destroyedConn.destroy(); + }); + }, + 'test a bad connection will return an error':function(test, assert){ var badConn = new Helenus.ConnectionPool(badConfig); // Add error handler to avoid uncaught exception. diff --git a/test/cql3.js b/test/cql3.js index 60b35e2..352c090 100644 --- a/test/cql3.js +++ b/test/cql3.js @@ -66,6 +66,17 @@ module.exports = { }); }, + 'test connection.destroy':function(test, assert){ + var destroyedConn = new Helenus.ConnectionPool(poolConfig); + destroyedConn.on('close', function () { + test.finish(); + }); + destroyedConn.connect(function(err){ + assert.ifError(err); + destroyedConn.destroy(); + }); + }, + 'test cql create keyspace':testResultless(config['create_ks#cql']), 'test cql use keyspace':testResultless(config['use#cql']), diff --git a/test/thrift.js b/test/thrift.js index cb92301..20f0919 100644 --- a/test/thrift.js +++ b/test/thrift.js @@ -27,6 +27,19 @@ module.exports = { }); }, + 'test pool.destroy without keyspace':function(test, assert){ + conn = new Helenus.ConnectionPool(system); + conn.on('close', function () { + test.finish(); + }); + assert.doesNotThrow(function(){ + conn.connect(function(err, keyspace){ + assert.ifError(err); + conn.destroy(); + }); + }); + }, + 'test pool.connect with keyspace':function(test, assert){ system.keyspace = 'system'; conn = new Helenus.ConnectionPool(system); @@ -698,6 +711,19 @@ module.exports = { }); }, + 'test pool.destroy':function(test, assert){ + conn = new Helenus.ConnectionPool(system); + conn.on('close', function(){ + test.finish(); + }); + assert.doesNotThrow(function() { + conn.connect(function (err, keyspace) { + assert.ifError(err); + conn.destroy(); + }); + }); + }, + 'tearDown':function(test, assert){ test.finish(); }