Skip to content

Commit

Permalink
Test eager spin down
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw committed Jun 10, 2013
1 parent bdb21bc commit 14b5379
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/round-robin.js
Expand Up @@ -29,7 +29,7 @@ function RoundRobin (opts) {
opts.maxUp = opts.maxUp || 2
opts.maxUsage = opts.maxUsage || Infinity
opts.maxAge = opts.maxAge || Infinity
opts.lazySpinDown = opts.lazySpinDown || true
opts.lazySpinDown = opts.lazySpinDown === undefined ? true : opts.lazySpinDown

this.opts = opts
this.consumers = []
Expand All @@ -40,7 +40,7 @@ function RoundRobin (opts) {
var noop = function () {}
this.eagerSpinDownIntervalId = setInterval(function() {
this.consumers.forEach(function (consumer) {
this.spinDownIf.call(this, consumer, noop)
spinDownIf.call(this, consumer, noop)
}.bind(this))
}.bind(this), 250)
}
Expand Down
61 changes: 61 additions & 0 deletions test/round-robin.js
Expand Up @@ -188,6 +188,67 @@ describe("RoundRobin", function () {
})
})

it("should eagerly spin down consumers when expired", function (done) {
this.timeout(3000)

var spunUpConsumers = []
, spunDownConsumers = []

var scheduler = new RoundRobin({
spinUp: function (cb) {
var consumer = {destroyed: false}
spunUpConsumers.push(consumer)
cb(null, consumer)
},
spinDown: function (consumer, cb) {
consumer.destroyed = true
spunDownConsumers.push(consumer)
cb()
},
maxUp: 2,
maxAge: 500,
lazySpinDown: false
})

// Create 2 consumers
scheduler.get(function (er, consumer) {
assert.ifError(er)
assert(consumer)

assert.equal(1, spunUpConsumers.length)
assert.equal(0, spunDownConsumers.length)

scheduler.get(function (er, consumer) {
assert.ifError(er)
assert(consumer)

assert.equal(2, spunUpConsumers.length)
assert.equal(0, spunDownConsumers.length)

// Get 2 more consumers once the current two have expired
setTimeout(function () {

// Because the scheduler eagerly spins down consumers, here, we should have destroyed the two consumers
assert.equal(2, spunUpConsumers.length)
assert.equal(2, spunDownConsumers.length)

spunUpConsumers.forEach(function (consumer) {
assert(consumer.destroyed)
})

// Now get (and hopefully create) two more consumers
scheduler.get(function (er, consumer) {
scheduler.get(function (er, consumer) {
assert.equal(4, spunUpConsumers.length)
assert.equal(2, spunDownConsumers.length)
done()
})
})
}, 2000)
})
})
})

it("should spin down consumers when destroyed", function (done) {

var scheduler = new RoundRobin({
Expand Down

0 comments on commit 14b5379

Please sign in to comment.