Use standard and prettier (#10)
* switch from custom eslint config to standard + prettier * fix new standard eslint violations * add editorconfig file * auto-fix all other violations * update lint yarn script * remove jshint comment
This commit is contained in:
@ -17,25 +17,25 @@ function listenerCount(emitter, event) {
|
||||
}
|
||||
}
|
||||
|
||||
describe('Connection Pool', function () {
|
||||
describe('Adding/Removing/Syncing Connections', function () {
|
||||
describe('Connection Pool', function() {
|
||||
describe('Adding/Removing/Syncing Connections', function() {
|
||||
var pool, host, connection, host2, connection2;
|
||||
|
||||
beforeEach(function () {
|
||||
beforeEach(function() {
|
||||
pool = new ConnectionPool({});
|
||||
|
||||
host = new Host({
|
||||
port: 999
|
||||
port: 999,
|
||||
});
|
||||
connection = new ConnectionAbstract(host);
|
||||
|
||||
host2 = new Host({
|
||||
port: 2222
|
||||
port: 2222,
|
||||
});
|
||||
connection2 = new ConnectionAbstract(host2);
|
||||
});
|
||||
|
||||
it('#addConnection only adds the connection if it doesn\'t already exist', function () {
|
||||
it("#addConnection only adds the connection if it doesn't already exist", function() {
|
||||
expect(_.keys(pool.index).length).to.eql(0);
|
||||
pool.addConnection(connection);
|
||||
|
||||
@ -45,8 +45,8 @@ describe('Connection Pool', function () {
|
||||
expect(pool._conns.dead).to.eql([]);
|
||||
});
|
||||
|
||||
describe('#removeConnection', function () {
|
||||
it('removes the connection if it exist', function () {
|
||||
describe('#removeConnection', function() {
|
||||
it('removes the connection if it exist', function() {
|
||||
pool.addConnection(connection);
|
||||
pool.removeConnection(connection2);
|
||||
|
||||
@ -55,7 +55,7 @@ describe('Connection Pool', function () {
|
||||
expect(_.keys(pool.index).length).to.eql(1);
|
||||
});
|
||||
|
||||
it('closes the connection when it removes it', function () {
|
||||
it('closes the connection when it removes it', function() {
|
||||
pool.addConnection(connection);
|
||||
expect(connection.status).to.eql('alive');
|
||||
expect(listenerCount(connection, 'status set')).to.eql(1);
|
||||
@ -67,7 +67,7 @@ describe('Connection Pool', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('#setHosts syncs the list of Hosts with the connections in the index', function () {
|
||||
it('#setHosts syncs the list of Hosts with the connections in the index', function() {
|
||||
// there should now be two connections
|
||||
pool.setHosts([host, host2]);
|
||||
expect(pool._conns.alive.length).to.eql(2);
|
||||
@ -92,53 +92,56 @@ describe('Connection Pool', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Connection selection', function () {
|
||||
describe('Connection selection', function() {
|
||||
var pool, host, host2;
|
||||
|
||||
beforeEach(function () {
|
||||
beforeEach(function() {
|
||||
pool = new ConnectionPool({});
|
||||
|
||||
host = new Host('localhost:9200');
|
||||
host2 = new Host('localhost:9201');
|
||||
|
||||
pool.setHosts([
|
||||
host,
|
||||
host2
|
||||
]);
|
||||
pool.setHosts([host, host2]);
|
||||
});
|
||||
|
||||
it('detects if the selector is async', function (done) {
|
||||
pool.selector = function (list, cb) {
|
||||
it('detects if the selector is async', function(done) {
|
||||
pool.selector = function(list, cb) {
|
||||
expect(cb).to.be.a('function');
|
||||
cb();
|
||||
};
|
||||
|
||||
pool.select(function (err) {
|
||||
if (err) { throw err; }
|
||||
pool.select(function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('detects if the selector is not async', function (done) {
|
||||
pool.selector = function () {
|
||||
it('detects if the selector is not async', function(done) {
|
||||
pool.selector = function() {
|
||||
expect(arguments.length).to.be(1);
|
||||
};
|
||||
|
||||
pool.select(function (err) {
|
||||
if (err) { throw err; }
|
||||
pool.select(function(err) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('sync selectors should still return async', function (done) {
|
||||
pool.selector = function (list) {
|
||||
it('sync selectors should still return async', function(done) {
|
||||
pool.selector = function(list) {
|
||||
return list[0];
|
||||
};
|
||||
|
||||
var selected = null;
|
||||
|
||||
pool.select(function (err, selection) {
|
||||
if (err) { throw err; }
|
||||
pool.select(function(err, selection) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
expect(selection.host).to.be(host);
|
||||
selected = selection;
|
||||
done();
|
||||
@ -147,51 +150,49 @@ describe('Connection Pool', function () {
|
||||
expect(selected).to.be(null);
|
||||
});
|
||||
|
||||
it('should catch errors in sync selectors', function (done) {
|
||||
pool.selector = function () {
|
||||
it('should catch errors in sync selectors', function(done) {
|
||||
pool.selector = function() {
|
||||
return JSON.notAMethod();
|
||||
};
|
||||
|
||||
pool.select(function (err) {
|
||||
pool.select(function(err) {
|
||||
expect(err).be.an(Error);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Connection selection with no living nodes', function () {
|
||||
it('should ping all of the dead nodes, in order of oldest timeout, and return the first that\'s okay',
|
||||
function (done) {
|
||||
describe('Connection selection with no living nodes', function() {
|
||||
it("should ping all of the dead nodes, in order of oldest timeout, and return the first that's okay", function(done) {
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
stub.autoRelease(clock);
|
||||
var pool = new ConnectionPool({
|
||||
deadTimeout: 10000
|
||||
deadTimeout: 10000,
|
||||
});
|
||||
|
||||
var connections = [
|
||||
new ConnectionAbstract(new Host('http://localhost:9200')),
|
||||
new ConnectionAbstract(new Host('http://localhost:9201')),
|
||||
new ConnectionAbstract(new Host('http://localhost:9202')),
|
||||
new ConnectionAbstract(new Host('http://localhost:9203'))
|
||||
new ConnectionAbstract(new Host('http://localhost:9203')),
|
||||
];
|
||||
var pingQueue = _.shuffle(connections);
|
||||
var expectedSelection = pingQueue[pingQueue.length - 1];
|
||||
|
||||
_.each(pingQueue, function (conn) {
|
||||
_.each(pingQueue, function(conn) {
|
||||
pool.addConnection(conn);
|
||||
stub(conn, 'ping', function (params, cb) {
|
||||
stub(conn, 'ping', function(params, cb) {
|
||||
if (typeof params === 'function') {
|
||||
cb = params;
|
||||
}
|
||||
var expectedConn = pingQueue.shift();
|
||||
expect(conn).to.be(expectedConn);
|
||||
if (pingQueue.length) {
|
||||
process.nextTick(function () {
|
||||
process.nextTick(function() {
|
||||
cb(new Error('keep trying'));
|
||||
});
|
||||
} else {
|
||||
process.nextTick(function () {
|
||||
process.nextTick(function() {
|
||||
cb(null, true);
|
||||
});
|
||||
}
|
||||
@ -200,7 +201,7 @@ describe('Connection Pool', function () {
|
||||
clock.tick(500);
|
||||
});
|
||||
|
||||
pool.select(function (err, selection) {
|
||||
pool.select(function(err, selection) {
|
||||
expect(selection).to.be(expectedSelection);
|
||||
expect(pingQueue.length).to.be(0);
|
||||
pool.setHosts([]);
|
||||
@ -210,19 +211,16 @@ describe('Connection Pool', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Connection state management', function () {
|
||||
describe('Connection state management', function() {
|
||||
var pool, host, host2, connection, connection2;
|
||||
|
||||
beforeEach(function () {
|
||||
beforeEach(function() {
|
||||
pool = new ConnectionPool({});
|
||||
|
||||
host = new Host('localhost:9200');
|
||||
host2 = new Host('localhost:9201');
|
||||
|
||||
pool.setHosts([
|
||||
host,
|
||||
host2
|
||||
]);
|
||||
pool.setHosts([host, host2]);
|
||||
|
||||
connection = pool.index[host.toString()];
|
||||
connection2 = pool.index[host2.toString()];
|
||||
@ -231,32 +229,38 @@ describe('Connection Pool', function () {
|
||||
expect(pool._conns.dead.length).to.be(0);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
afterEach(function() {
|
||||
pool.close();
|
||||
});
|
||||
|
||||
it('moves an alive connection to dead', function () {
|
||||
it('moves an alive connection to dead', function() {
|
||||
connection.setStatus('dead');
|
||||
|
||||
expect(pool._conns.alive.length).to.be(1);
|
||||
expect(pool._conns.dead.length).to.be(1);
|
||||
});
|
||||
|
||||
it('clears and resets the timeout when a connection redies', function () {
|
||||
it('clears and resets the timeout when a connection redies', function() {
|
||||
var clock = sinon.useFakeTimers();
|
||||
stub.autoRelease(clock);
|
||||
|
||||
connection.setStatus('dead');
|
||||
expect(_.size(clock.timers)).to.eql(1);
|
||||
var id = _(clock.timers).keys().head();
|
||||
var id = _(clock.timers)
|
||||
.keys()
|
||||
.head();
|
||||
|
||||
// it re-dies
|
||||
connection.setStatus('dead');
|
||||
expect(_.size(clock.timers)).to.eql(1);
|
||||
expect(_(clock.timers).keys().head()).to.not.eql(id);
|
||||
expect(
|
||||
_(clock.timers)
|
||||
.keys()
|
||||
.head()
|
||||
).to.not.eql(id);
|
||||
});
|
||||
|
||||
it('does nothing when a connection is re-alive', function () {
|
||||
it('does nothing when a connection is re-alive', function() {
|
||||
var last = pool._conns.alive[pool._conns.alive.length - 1];
|
||||
var first = pool._conns.alive[0];
|
||||
|
||||
@ -273,7 +277,7 @@ describe('Connection Pool', function () {
|
||||
expect(pool._conns.alive[pool._conns.alive.length - 1]).to.be(last);
|
||||
});
|
||||
|
||||
it('removes all its connection when it closes, causing them to be closed', function () {
|
||||
it('removes all its connection when it closes, causing them to be closed', function() {
|
||||
pool.close();
|
||||
expect(pool._conns.alive.length).to.be(0);
|
||||
expect(pool._conns.dead.length).to.be(0);
|
||||
@ -281,11 +285,10 @@ describe('Connection Pool', function () {
|
||||
expect(connection.status).to.eql('closed');
|
||||
expect(connection2.status).to.eql('closed');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#getConnections', function () {
|
||||
it('will return all values from the alive list by default', function () {
|
||||
describe('#getConnections', function() {
|
||||
it('will return all values from the alive list by default', function() {
|
||||
var pool = new ConnectionPool({});
|
||||
pool._conns.alive = new Array(1000);
|
||||
var length = pool._conns.alive.length;
|
||||
@ -295,50 +298,58 @@ describe('Connection Pool', function () {
|
||||
|
||||
var result = pool.getConnections();
|
||||
expect(result.length).to.be(1000);
|
||||
expect(_.reduce(result, function (sum, num) {
|
||||
sum += num
|
||||
return sum;
|
||||
}, 0)).to.eql(499500);
|
||||
expect(
|
||||
_.reduce(
|
||||
result,
|
||||
function(sum, num) {
|
||||
sum += num;
|
||||
return sum;
|
||||
},
|
||||
0
|
||||
)
|
||||
).to.eql(499500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#calcDeadTimeout', function () {
|
||||
it('should be configurable via config.calcDeadTimeout', function () {
|
||||
describe('#calcDeadTimeout', function() {
|
||||
it('should be configurable via config.calcDeadTimeout', function() {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'flat'
|
||||
calcDeadTimeout: 'flat',
|
||||
});
|
||||
expect(pool.calcDeadTimeout).to.be(ConnectionPool.calcDeadTimeoutOptions.flat);
|
||||
expect(pool.calcDeadTimeout).to.be(
|
||||
ConnectionPool.calcDeadTimeoutOptions.flat
|
||||
);
|
||||
pool.close();
|
||||
});
|
||||
it('"flat" always returns the base timeout', function () {
|
||||
it('"flat" always returns the base timeout', function() {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'flat'
|
||||
calcDeadTimeout: 'flat',
|
||||
});
|
||||
expect(pool.calcDeadTimeout(0, 1000)).to.eql(1000);
|
||||
expect(pool.calcDeadTimeout(10, 5000)).to.eql(5000);
|
||||
expect(pool.calcDeadTimeout(25, 10000)).to.eql(10000);
|
||||
});
|
||||
it('"exponential" always increases the timeout based on the attempts', function () {
|
||||
it('"exponential" always increases the timeout based on the attempts', function() {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'exponential'
|
||||
calcDeadTimeout: 'exponential',
|
||||
});
|
||||
expect(pool.calcDeadTimeout(0, 1000)).to.eql(1000);
|
||||
expect(pool.calcDeadTimeout(10, 5000)).to.be.greaterThan(5000);
|
||||
expect(pool.calcDeadTimeout(25, 10000)).to.be.greaterThan(10000);
|
||||
});
|
||||
it('"exponential" produces predicatable results', function () {
|
||||
it('"exponential" produces predicatable results', function() {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'exponential'
|
||||
calcDeadTimeout: 'exponential',
|
||||
});
|
||||
expect(pool.calcDeadTimeout(0, 1000)).to.eql(1000);
|
||||
expect(pool.calcDeadTimeout(4, 10000)).to.eql(40000);
|
||||
// maxes out at 30 minutes by default
|
||||
expect(pool.calcDeadTimeout(25, 30000)).to.eql(18e5);
|
||||
});
|
||||
it('"exponential" repects config.maxDeadtimeout', function () {
|
||||
it('"exponential" repects config.maxDeadtimeout', function() {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'exponential',
|
||||
maxDeadTimeout: 10000
|
||||
maxDeadTimeout: 10000,
|
||||
});
|
||||
expect(pool.calcDeadTimeout(0, 1000)).to.eql(1000);
|
||||
expect(pool.calcDeadTimeout(10, 1000)).to.eql(10000);
|
||||
|
||||
Reference in New Issue
Block a user