Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More test coverage

  • Loading branch information...
commit ff1a7dad7f4eafbd6da510147f07f7f95b0bc6df 1 parent 6c7a8ce
@3rd-Eden authored
Showing with 133 additions and 2 deletions.
  1. +5 −2 lib/connectionpool.js
  2. +128 −0 test/connectionpool.test.js
View
7 lib/connectionpool.js
@@ -215,7 +215,7 @@ Manager.prototype.isAvailable = function isAvailable(net) {
*
* @param {Stream} net
* @returns {Boolean} was the removal successful
- * @api public
+ * @api private
*/
Manager.prototype.release = function release(net) {
@@ -246,7 +246,7 @@ Manager.prototype.remove = Manager.prototype.release;
Manager.prototype.free = function free(keep) {
// default to 0 if no arguments are supplied
- keep = keep || 0;
+ keep = +keep || 0;
var saved = 0
, i = this.pool.length
@@ -266,6 +266,9 @@ Manager.prototype.free = function free(keep) {
this.release(connection);
}
+
+ // see how much connections are still available
+ this.emit('free', saved, this.pool.length);
};
/**
View
128 test/connectionpool.test.js
@@ -86,6 +86,134 @@ describe('connectionpool', function () {
done();
});
});
+
+ it('should NOT emit an error when we can establish a connection', function (done) {
+ var pool = new ConnectionPool();
+
+ pool.factory(function factory() {
+ return net.connect(port, host);
+ });
+
+ pool.allocate(function allocate(err, connection) {
+ expect(err).to.not.be.an.instanceof(Error);
+ expect(connection).to.be.an.instanceof(net.Socket);
+
+ connection.end();
+ done();
+ });
+ });
+
+ it('should an error occure, then remove it from the pool', function (done) {
+ var pool = new ConnectionPool()
+ , differentport = TESTNUMBER;
+
+ pool.once('error', function error(err) {
+ expect(pool.pool).to.have.length(0);
+
+ done();
+ });
+
+ pool.factory(function factory() {
+ return net.connect(differentport, host);
+ });
+
+ // make sure the port is different
+ expect(differentport).to.not.eql(port);
+
+ pool.allocate(function allocate(err, connection) {});
+ });
+
+ it('should increase poolsize when a connection is allocated', function (done) {
+ var pool = new ConnectionPool();
+
+ pool.factory(function factory() {
+ return net.connect(port, host);
+ });
+
+ pool.allocate(function allocate(err, connection) {
+ expect(connection).to.be.an.instanceof(net.Socket);
+ expect(pool.pool).to.have.length(1);
+
+ // make sure it also decreases when the connection is closed
+ connection.on('end', function end() {
+ expect(pool.pool).to.have.length(0);
+
+ done();
+ });
+ connection.end();
+ });
+ });
+
+ it('should increase metrics when allocating a connection', function (done) {
+ var pool = new ConnectionPool();
+
+ pool.factory(function factory() {
+ return net.connect(port, host);
+ });
+
+ pool.allocate(function allocate(err, connection) {
+ expect(pool.metrics.allocations).to.eql(1);
+
+ // make sure it also decreases when the connection is closed
+ connection.on('end', function end() {
+ expect(pool.metrics.allocations).to.eql(1);
+ expect(pool.metrics.releases).to.eql(1);
+
+ done();
+ });
+
+ connection.end();
+ });
+ });
+ });
+
+ describe('#free', function () {
+ it('should kill all connections when 0 is given', function (done) {
+ var pool = new ConnectionPool();
+
+ pool.factory(function factory() {
+ return net.connect(port, host);
+ });
+
+ pool.allocate(function allocate(err, connection) {
+ expect(connection).to.be.an.instanceof(net.Socket);
+ expect(pool.pool).to.have.length(1);
+
+ pool.once('free', function free(saved, size) {
+ expect(pool.pool).to.have.length(0);
+ expect(size).to.eql(pool.pool.length);
+ expect(saved).to.eql(0);
+
+ done();
+ });
+
+ pool.free(0);
+ });
+ });
+
+ it('should keep 1 connection', function (done) {
+ var pool = new ConnectionPool();
+
+ pool.factory(function factory() {
+ return net.connect(port, host);
+ });
+
+ pool.allocate(function allocate(err, connection) {
+ expect(connection).to.be.an.instanceof(net.Socket);
+ expect(pool.pool).to.have.length(1);
+
+ pool.once('free', function free(saved, size) {
+ expect(pool.pool).to.have.length(1);
+ expect(size).to.eql(pool.pool.length);
+ expect(saved).to.eql(1);
+
+ connection.end();
+ done();
+ });
+
+ pool.free(1);
+ });
+ });
});
after(function after(done) {
Please sign in to comment.
Something went wrong with that request. Please try again.