Skip to content

Commit

Permalink
Delay Queue first cut
Browse files Browse the repository at this point in the history
  • Loading branch information
monmohan committed Oct 3, 2013
1 parent 9158e94 commit ad3ce8d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/DelayQueue.js
Original file line number Diff line number Diff line change
@@ -0,0 1,33 @@
(function () {
"use strict";
var PriorityQueue = require('./PriorityQueue');

function DelayQueue(delayFn) {
if(typeof delayFn!=='function') {
throw new Error('A delay function must be provided to construct'
'a DelayQueue');
}
this._delayFn=delayFn;
PriorityQueue.call(this,function (inQ, objToAdd) {
var dInQ = delayFn.call(null, inQ),
dObjToAdd = delayFn.call(null, objToAdd);
return dInQ > dObjToAdd ?
1 : dInQ < dObjToAdd ?
-1 : 0;

});
}

DelayQueue.prototype=new PriorityQueue();
var _super=PriorityQueue.prototype;

DelayQueue.prototype.poll=function(){
var head=this.peek();
return this._delayFn(head)<=0?
_super.poll.call(this):null;
};


module.exports = DelayQueue;

}());
86 changes: 86 additions & 0 deletions tests/TestDelayQueue.js
Original file line number Diff line number Diff line change
@@ -0,0 1,86 @@
var DelayQueue = require('../lib/DelayQueue.js'), assert = require('assert'), fs = require('fs');
(function () {

function testOffer(testDoneFn) {
console.log('**testOffer***');
var times = [Date.now() 10 * 1000, Date.now() 20 * 1000, Date.now() 30 * 1000, Date.now() 40 * 1000]
var tasks = [
{'schedule' : times[0]},
{'schedule' : times[1]},
{'schedule' : times[3]},
{'schedule' : times[2]}
];
var dq = new DelayQueue(function (task) {
return task.schedule - Date.now();
});
tasks.forEach(function (task) {
dq.offer(task);
});
assert.deepEqual(dq._queue, [
{ schedule : times[0] },
{ schedule : times[1] },
{ schedule : times[3] },
{ schedule : times [2]}
]);

assert.equal(dq.size(), 4);
assert.equal(dq.poll(), null);
assert.equal(dq.peek().schedule, times[0]);
setTimeout(function () {
assert.deepEqual(dq.poll().schedule, times[0]);
assert.equal(dq.poll(), null);
testDoneFn();
}, 12000);
}

function testPoll() {
console.log('**** test Poll***');
var times = [],
delay = 2000,
now = Date.now();
var dq = new DelayQueue(function (task) {
return task.schedule - Date.now();
});

for (var i = 30; i > 0; i--) {
var time = (now delay * i);
times[i] = time;
dq.offer({'schedule' : time});
}

function testCleanup() {
//clean and check
for (i = 21; i <= 30; i ) {
dq.poll();
}
assert.equal(dq.size(), 0);
assert.deepEqual(dq._queue, []);
}

setTimeout(function () {
for (i = 1; i <= 20; i ) {
//console.log('iteration ' i);
assert.equal(dq.poll().schedule, times[i]);
assert.equal(dq.size(), 30 - i, 'test failed - size mismatch');


}
for (i = 9; i >= 0; i--) {
assert.equal(dq.peek().schedule, times[21]);
assert.equal(dq.size(), 10, 'test failed - size mismatch');
}
setTimeout(testCleanup, 20 * 1000);

}, 42 * 1000);


}


testOffer(function () {
testPoll();
});



}());

0 comments on commit ad3ce8d

Please sign in to comment.