forked from monmohan/dsjslib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
||
|
||
|
||
}()); |