Skip to content

Commit

Permalink
[api test] Properly pass along the expended duration in which rolled …
Browse files Browse the repository at this point in the history
…up events have been received in addition to the periods.
  • Loading branch information
indexzero committed Jun 5, 2013
1 parent c03fea0 commit c2e8750
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
46 changes: 32 additions & 14 deletions lib/godot/reactor/rollup.js
Expand Up @@ -31,11 +31,13 @@ var Rollup = module.exports = function (interval, limit) {
};
}

this.interval = options.interval || 1000 * 60 * 60;
this.limit = options.limit || 100;
this.period = 0;
this.events = [];
this.next = [];
this.limit = options.limit || 100;
this.interval = options.interval || 1000 * 60 * 60;
this.forceReset = typeof interval === 'function';
this.duration = 0;
this.period = 0;
this.events = [];
this.next = [];
};

//
Expand All @@ -51,6 +53,9 @@ util.inherits(Rollup, ReadWriteStream);
//
Rollup.prototype.write = function (data) {
if (this.events.length === this.limit) {
//
// Remark: Should we drop events here?
//
this.next.push(data);
}
else {
Expand All @@ -67,21 +72,26 @@ Rollup.prototype.write = function (data) {
// Resets the interval for this instance.
//
Rollup.prototype.resetInterval = function () {
var self = this;
var self = this,
nextInterval;

function interval () {
if (typeof self.interval === 'function') {
return self.interval(self.period);
}
return self.interval;
}
//
// Set the nextInterval to the calculated value
// of the interval function or the static interval value.
//
nextInterval = typeof self.interval === 'function'
? self.interval(self.period, self.duration)
: self.interval;

if (this.intervalId) {
clearInterval(this.intervalId);
}

this.intervalId = setInterval(function () {
if (self.events.length) {
self.duration += nextInterval;
self.period += 1;

self.emit('data', self.events.slice());
self.events.length = 0;

Expand All @@ -90,7 +100,15 @@ Rollup.prototype.resetInterval = function () {
}

self.next.length = 0;
self.period += 1;
if (self.forceReset) {
//
// Remark: Basic unit testing was not effective to see if
// this could cause StackOverflow exceptions. Process ran
// out of memory before a StackOverflow exception was thrown.
// A long running stress test would determine this.
//
self.resetInterval();
}
}
}, interval());
}, nextInterval);
};
3 changes: 2 additions & 1 deletion test/reactor/rollup-test.js
Expand Up @@ -8,6 +8,7 @@
var assert = require('assert'),
vows = require('vows'),
godot = require('../../lib/godot'),
helpers = require('../helpers'),
macros = require('../macros').reactor;

vows.describe('godot/reactor/rollup').addBatch({
Expand Down Expand Up @@ -46,7 +47,7 @@ vows.describe('godot/reactor/rollup').addBatch({
}, 2),
'health',
2,
300
200
)
}
}).export(module);

0 comments on commit c2e8750

Please sign in to comment.