Skip to content
This repository has been archived by the owner on Feb 22, 2019. It is now read-only.

Adding destroy method to connection pool #84

Closed
wants to merge 1 commit into from
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
9 changes: 9 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
30 changes: 30 additions & 0 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
11 changes: 11 additions & 0 deletions test/cql2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions test/cql3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']),

Expand Down
26 changes: 26 additions & 0 deletions test/thrift.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down