Comparing version 0.0.2 to 1.0.0
@@ -49,2 +49,6 @@ 'use strict'; | ||
this.peers.sort(function (a, b) { | ||
return a.weight - b.weight; | ||
}); | ||
this.maxS = _.maxBy(this.peers, 'weight').weight; | ||
@@ -60,3 +64,3 @@ | ||
value: value, | ||
weight: weight | ||
weight: weight || 10 | ||
}); | ||
@@ -87,1 +91,42 @@ | ||
}; | ||
Pool.prototype.get = function (predicate) { | ||
var i = _.findIndex(_.map(this.peers, 'value'), predicate); | ||
if (i !== -1) { | ||
return this.peers[i]; | ||
} | ||
return undefined; | ||
}; | ||
Pool.prototype.update = function (predicate, value, weight) { | ||
var i = _.findIndex(_.map(this.peers, 'value'), predicate); | ||
if (i === -1) { | ||
return undefined; | ||
} | ||
this.peers[i] = { | ||
value: value, | ||
weight: weight | ||
}; | ||
this._reset(); | ||
return i; | ||
}; | ||
Pool.prototype.remove = function (predicate) { | ||
var i = _.findIndex(_.map(this.peers, 'value'), predicate); | ||
if (i === -1) { | ||
return undefined; | ||
} | ||
this.peers.splice(i, 1); | ||
this._reset(); | ||
return i; | ||
}; |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"main": "./lib/index.js", | ||
@@ -12,0 +12,0 @@ "keywords": ["weighted", "roundrobin", "round", "robin"], |
@@ -1,2 +0,2 @@ | ||
[![Build Status](https://travis-ci.org/oleksiyk/kafka.png)](https://travis-ci.org/oleksiyk/kafka) | ||
[![Build Status](https://travis-ci.org/oleksiyk/wrr-pool.png)](https://travis-ci.org/oleksiyk/wrr-pool) | ||
@@ -18,5 +18,5 @@ # WRRPool | ||
pool.add('A', 4); | ||
pool.add('B', 3); | ||
pool.add('C', 2); | ||
pool.add('A', 4); // pool.add({ host: '10.0.1.10', port: 8087}, 4) | ||
pool.add('B', 3); // pool.add({ host: '10.0.1.11', port: 8087}, 3) | ||
pool.add('C', 2); // pool.add({ host: '10.0.1.12', port: 8087}, 2) | ||
@@ -23,0 +23,0 @@ pool.next(); // A |
10
test.js
'use strict'; | ||
var WRRPool = require('./lib/index'); | ||
var _ = require('lodash'); | ||
@@ -11,2 +12,3 @@ var pool = new WRRPool(); | ||
/*console.log(pool.next()); | ||
console.log(pool.next()); | ||
@@ -19,3 +21,7 @@ console.log(pool.next()); | ||
console.log(pool.next()); | ||
console.log(pool.next()); | ||
console.log(pool.next()); | ||
console.log(pool.next());*/ | ||
var i = _(0).range(9).map(pool.next.bind(pool)).countBy().value(); | ||
console.log(i); |
113
test/test.js
@@ -15,5 +15,7 @@ 'use strict'; | ||
_.range(0, 9).map(function () { | ||
return pool.next(); | ||
}).join('').should.be.eql('AABABCABC'); | ||
_(0).range(9).map(pool.next.bind(pool)).countBy().value().should.be.eql({ | ||
A: 4, | ||
B: 3, | ||
C: 2 | ||
}); | ||
}); | ||
@@ -32,4 +34,4 @@ | ||
_.range(0, 9).forEach(function () { | ||
pool.next().should.be.eql('A'); | ||
_(0).range(12).map(pool.next.bind(pool)).countBy().value().should.be.eql({ | ||
A: 12 | ||
}); | ||
@@ -45,5 +47,100 @@ }); | ||
_.range(0, 9).map(function () { | ||
return pool.next(); | ||
}).join('').should.be.eql('ABCABCABC'); | ||
_(0).range(12).map(pool.next.bind(pool)).countBy().value().should.be.eql({ | ||
A: 4, | ||
B: 4, | ||
C: 4 | ||
}); | ||
}); | ||
it('get()', function () { | ||
var pool = new WRRPool(); | ||
pool.add({ id: 1 }, 4); | ||
pool.add({ id: 2 }, 3); | ||
pool.add({ id: 3 }, 2); | ||
pool.get({ id: 2 }).should.be.eql({ | ||
value: { id: 2 }, | ||
weight: 3 | ||
}); | ||
}); | ||
it('get() failed predicate returns undefined', function () { | ||
var pool = new WRRPool(); | ||
pool.add({ id: 1 }, 4); | ||
pool.add({ id: 2 }, 3); | ||
pool.add({ id: 3 }, 2); | ||
expect(pool.get({ id: 20 })).to.eql(undefined); | ||
}); | ||
it('update()', function () { | ||
var pool = new WRRPool(); | ||
pool.add('A', 4); | ||
pool.add('B', 3); | ||
pool.add('C', 2); | ||
_(0).range(9).map(pool.next.bind(pool)).countBy().value().should.be.eql({ | ||
A: 4, | ||
B: 3, | ||
C: 2 | ||
}); | ||
pool.update(function (v) { return v === 'B';}, 'B1', 4).should.be.eql(1); | ||
pool.update(function (v) { return v === 'C';}, 'C1', 4).should.be.eql(0); // it was sorted | ||
_(0).range(12).map(pool.next.bind(pool)).countBy().value().should.be.eql({ | ||
A: 4, | ||
B1: 4, | ||
C1: 4 | ||
}); | ||
}); | ||
it('update() failed predicate returns undefined', function () { | ||
var pool = new WRRPool(); | ||
pool.add('A', 4); | ||
pool.add('B', 3); | ||
pool.add('C', 2); | ||
expect(pool.update(function (v) { return v === 'D';}, 'B1', 100)).to.eql(undefined); | ||
_(0).range(9).map(pool.next.bind(pool)).countBy().value().should.be.eql({ | ||
A: 4, | ||
B: 3, | ||
C: 2 | ||
}); | ||
}); | ||
it('remove()', function () { | ||
var pool = new WRRPool(); | ||
pool.add('A', 4); | ||
pool.add('B', 3); | ||
pool.add('C', 2); | ||
pool.remove(function (v) { return v === 'C';}).should.be.eql(0); | ||
_(0).range(7).map(pool.next.bind(pool)).countBy().value().should.be.eql({ | ||
A: 4, | ||
B: 3 | ||
}); | ||
}); | ||
it('remove() failed predicate returns undefined', function () { | ||
var pool = new WRRPool(); | ||
pool.add('A', 4); | ||
pool.add('B', 3); | ||
pool.add('C', 2); | ||
expect(pool.remove(function (v) { return v === 'D';})).to.eql(undefined); | ||
_(0).range(9).map(pool.next.bind(pool)).countBy().value().should.be.eql({ | ||
A: 4, | ||
B: 3, | ||
C: 2 | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
46263
253
0