2013-02-16 06:13:28 +08:00
|
|
|
var util = require('util');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
|
|
|
|
var libDir = __dirname + '/../../../lib';
|
|
|
|
var defaults = require(libDir + '/defaults');
|
2013-02-21 06:08:48 +08:00
|
|
|
var pools = require(libDir + '/pool');
|
2013-02-16 06:13:28 +08:00
|
|
|
var poolId = 0;
|
|
|
|
|
|
|
|
require(__dirname + '/../../test-helper');
|
|
|
|
|
|
|
|
var FakeClient = function() {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(FakeClient, EventEmitter);
|
|
|
|
|
|
|
|
FakeClient.prototype.connect = function(cb) {
|
|
|
|
process.nextTick(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
FakeClient.prototype.end = function() {
|
2013-02-20 09:34:28 +08:00
|
|
|
this.endCalled = true;
|
2013-02-16 06:13:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Hangs the event loop until 'end' is called on client
|
|
|
|
var HangingClient = function(config) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
this.config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(HangingClient, EventEmitter);
|
|
|
|
|
|
|
|
HangingClient.prototype.connect = function(cb) {
|
|
|
|
this.intervalId = setInterval(function() {
|
|
|
|
console.log('hung client...');
|
|
|
|
}, 1000);
|
|
|
|
process.nextTick(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
HangingClient.prototype.end = function() {
|
|
|
|
clearInterval(this.intervalId);
|
|
|
|
}
|
|
|
|
|
2013-02-21 06:08:48 +08:00
|
|
|
pools.Client = FakeClient;
|
2013-02-16 06:13:28 +08:00
|
|
|
|
|
|
|
test('no pools exist', function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
assert.empty(Object.keys(pools.all));
|
2013-02-16 06:13:28 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('pool creates pool on miss', function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
var p = pools.getOrCreate();
|
2013-02-16 06:13:28 +08:00
|
|
|
assert.ok(p);
|
2013-02-21 06:08:48 +08:00
|
|
|
assert.equal(Object.keys(pools.all).length, 1);
|
|
|
|
var p2 = pools.getOrCreate();
|
2013-02-16 06:13:28 +08:00
|
|
|
assert.equal(p, p2);
|
2013-02-21 06:08:48 +08:00
|
|
|
assert.equal(Object.keys(pools.all).length, 1);
|
|
|
|
var p3 = pools.getOrCreate("pg://postgres:password@localhost:5432/postgres");
|
2013-02-16 06:13:28 +08:00
|
|
|
assert.notEqual(p, p3);
|
2013-02-21 06:08:48 +08:00
|
|
|
assert.equal(Object.keys(pools.all).length, 2);
|
2013-02-16 06:13:28 +08:00
|
|
|
});
|
|
|
|
|
2013-02-20 09:34:28 +08:00
|
|
|
test('pool follows defaults', function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
var p = pools.getOrCreate(poolId++);
|
2013-02-16 06:13:28 +08:00
|
|
|
for(var i = 0; i < 100; i++) {
|
|
|
|
p.acquire(function(err, client) {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
assert.equal(p.getPoolSize(), defaults.poolSize);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('pool#connect with 3 parameters', function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
var p = pools.getOrCreate(poolId++);
|
2013-02-16 06:13:28 +08:00
|
|
|
var tid = setTimeout(function() {
|
|
|
|
throw new Error("Connection callback was never called");
|
|
|
|
}, 100);
|
|
|
|
p.connect(function(err, client, done) {
|
|
|
|
clearTimeout(tid);
|
2013-03-08 05:57:00 +08:00
|
|
|
assert.ifError(err, null);
|
2013-02-16 06:13:28 +08:00
|
|
|
assert.ok(client);
|
|
|
|
assert.equal(p.availableObjectsCount(), 0);
|
|
|
|
assert.equal(p.getPoolSize(), 1);
|
|
|
|
client.emit('drain');
|
|
|
|
assert.equal(p.availableObjectsCount(), 0);
|
|
|
|
assert.equal(p.getPoolSize(), 1);
|
|
|
|
done();
|
|
|
|
assert.equal(p.availableObjectsCount(), 1);
|
|
|
|
assert.equal(p.getPoolSize(), 1);
|
|
|
|
p.destroyAllNow();
|
|
|
|
});
|
|
|
|
});
|
2013-02-20 09:34:28 +08:00
|
|
|
|
|
|
|
test('on client error, client is removed from pool', function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
var p = pools.getOrCreate(poolId++);
|
2013-03-08 05:57:00 +08:00
|
|
|
p.connect(assert.success(function(client, done) {
|
2013-02-20 09:34:28 +08:00
|
|
|
assert.ok(client);
|
2013-03-08 05:57:00 +08:00
|
|
|
done();
|
2013-02-20 09:34:28 +08:00
|
|
|
assert.equal(p.availableObjectsCount(), 1);
|
|
|
|
assert.equal(p.getPoolSize(), 1);
|
|
|
|
//error event fires on pool BEFORE pool.destroy is called with client
|
|
|
|
assert.emits(p, 'error', function(err) {
|
|
|
|
assert.equal(err.message, 'test error');
|
|
|
|
assert.ok(!client.endCalled);
|
|
|
|
assert.equal(p.availableObjectsCount(), 1);
|
|
|
|
assert.equal(p.getPoolSize(), 1);
|
|
|
|
//after we're done in our callback, pool.destroy is called
|
|
|
|
process.nextTick(function() {
|
|
|
|
assert.ok(client.endCalled);
|
|
|
|
assert.equal(p.availableObjectsCount(), 0);
|
|
|
|
assert.equal(p.getPoolSize(), 0);
|
|
|
|
p.destroyAllNow();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
client.emit('error', new Error('test error'));
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('pool with connection error on connection', function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
pools.Client = function() {
|
2013-02-20 09:34:28 +08:00
|
|
|
return {
|
|
|
|
connect: function(cb) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
cb(new Error('Could not connect'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
test('two parameters', function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
var p = pools.getOrCreate(poolId++);
|
2013-02-20 09:34:28 +08:00
|
|
|
p.connect(assert.calls(function(err, client) {
|
|
|
|
assert.ok(err);
|
|
|
|
assert.equal(client, null);
|
|
|
|
//client automatically removed
|
|
|
|
assert.equal(p.availableObjectsCount(), 0);
|
|
|
|
assert.equal(p.getPoolSize(), 0);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
test('three parameters', function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
var p = pools.getOrCreate(poolId++);
|
2013-02-20 09:34:28 +08:00
|
|
|
var tid = setTimeout(function() {
|
|
|
|
assert.fail('Did not call connect callback');
|
|
|
|
}, 100);
|
|
|
|
p.connect(function(err, client, done) {
|
|
|
|
clearTimeout(tid);
|
|
|
|
assert.ok(err);
|
|
|
|
assert.equal(client, null);
|
|
|
|
//done does nothing
|
|
|
|
done(new Error('OH NOOOO'));
|
|
|
|
done();
|
|
|
|
assert.equal(p.availableObjectsCount(), 0);
|
|
|
|
assert.equal(p.getPoolSize(), 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('returnning an error to done()', function() {
|
2013-02-21 06:08:48 +08:00
|
|
|
var p = pools.getOrCreate(poolId++);
|
|
|
|
pools.Client = FakeClient;
|
2013-02-20 09:34:28 +08:00
|
|
|
p.connect(function(err, client, done) {
|
|
|
|
assert.equal(err, null);
|
|
|
|
assert(client);
|
|
|
|
done(new Error("BROKEN"));
|
|
|
|
assert.equal(p.availableObjectsCount(), 0);
|
|
|
|
assert.equal(p.getPoolSize(), 0);
|
|
|
|
});
|
|
|
|
});
|
2013-02-21 06:08:48 +08:00
|
|
|
|
|
|
|
test('fetching pool by object', function() {
|
|
|
|
var p = pools.getOrCreate({
|
|
|
|
user: 'brian',
|
|
|
|
host: 'localhost',
|
|
|
|
password: 'password'
|
|
|
|
});
|
|
|
|
var p2 = pools.getOrCreate({
|
|
|
|
user: 'brian',
|
|
|
|
host: 'localhost',
|
|
|
|
password: 'password'
|
|
|
|
});
|
|
|
|
assert.equal(p, p2);
|
|
|
|
});
|