Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
yarn.lock
examples/
tests.js
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"version": "0.6.3",
"dependencies": {},
"devDependencies": {
"mocha": "1.7.0",
"mocha": "^3.5.0",
"pre-commit": "^1.0.5",
"should": "1.2.1"
"should": "^12.0.0"
},
"maintainers": [
"Samuel Reed <sam@tixelated.com>",
Expand Down
60 changes: 29 additions & 31 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ function tightWork(duration) {
/*global describe, it, beforeEach, afterEach */
describe('the library', function() {
it('should export a couple functions', function() {
(toobusy).should.be.a('function');
(toobusy.maxLag).should.be.a('function');
(toobusy.shutdown).should.be.a('function');
(toobusy.interval).should.be.a('function');
(toobusy.shutdown).should.be.a('function');
should(toobusy).be.Function();
(toobusy.maxLag).should.be.Function();
(toobusy.shutdown).should.be.Function();
(toobusy.interval).should.be.Function();
(toobusy.shutdown).should.be.Function();
(toobusy).should.not.have.property('start');
});
it('should start automatically', function() {
Expand Down Expand Up @@ -61,9 +61,9 @@ describe('toobusy()', function() {
// is nice for making these tests independent of each other.
beforeEach(function() {
toobusy.maxLag(10);
toobusy.interval(250);
toobusy.interval(50);
});
afterEach(function() {
after(function() {
toobusy.maxLag(70);
toobusy.interval(500);
});
Expand Down Expand Up @@ -97,57 +97,58 @@ describe('toobusy()', function() {
it('should not emit lag events if the lag is less than the configured threshold',
testLagEvent(100, 50, false));
it('should emit lag events if the lag is greater than the configured threshold',
testLagEvent(50, 100, true));
testLagEvent(50, 150, true));
it('should emit lag events if lag occurs and no threshold is specified',
testLagEvent(undefined, 500, true));

function testLagEvent(threshold, work, expectFire) {
return function (done) {
var calledDone = false;
var finish = function() {
if (calledDone) return;
calledDone = true;
toobusy.shutdown(); // stops onLag() from firing again
clearTimeout(workTimeout);
done.apply(null, arguments);
};

toobusy.onLag(function (lag) {
if (calledDone) {
return;
}

if (!expectFire) {
calledDone = true;
done(new Error('lag event fired unexpectedly'));
return;
return finish(new Error('lag event fired unexpectedly'));
}

should.exist(lag);
lag.should.be.above(threshold || 0);

calledDone = true;
done();
finish();
}, threshold);

if (!expectFire) {
setTimeout(function () {
if (!calledDone) {
calledDone = true;
done();
}
finish();
}, work + threshold);
}

tightWork(work);
// Do work 3x to work around smoothing factor
var count = 0;
var workTimeout = setTimeout(function working() {
tightWork(work);
if (++count < 3) workTimeout = setTimeout(working);
})
}
}
});
});

describe('smoothingFactor', function() {
//Sometimes the default 2s timeout is hit on this suite, raise to 10s.
// Sometimes the default 2s timeout is hit on this suite, raise to 10s.
this.timeout(10 * 1000);

beforeEach(function() {
toobusy.reset();
toobusy.maxLag(10);
toobusy.interval(250);
});
afterEach(function() {
after(function() {
toobusy.maxLag(70);
toobusy.interval(500);
});
Expand Down Expand Up @@ -204,22 +205,19 @@ function setupSmoothingFactorTests(options) {
var smoothingFunc = options.function;

describe(options.suiteName, function() {
it('should default to ' + options.default, function(done) {
it('should default to ' + options.default, function() {
(smoothingFunc()).should.equal(options.default);
done();
});
it('should throw an exception for invalid values', function(done) {
it('should throw an exception for invalid values', function() {
(function() { smoothingFunc(0); }).should.throw;
(function() { smoothingFunc(2); }).should.throw;
(function() { smoothingFunc(-1); }).should.throw;
(function() { smoothingFunc(1); }).should.not.throw;
done();
});
it('should be configurable', function(done) {
it('should be configurable', function() {
(smoothingFunc(0.9)).should.equal(0.9);
(smoothingFunc(0.1)).should.equal(0.1);
(smoothingFunc()).should.equal(0.1);
done();
});
});
};
Expand Down
10 changes: 7 additions & 3 deletions toobusy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var SMOOTHING_FACTOR = 1/3;
// Vars
//

var lastTime = Date.now();
var lastTime;
var highWater = STANDARD_HIGHWATER;
var interval = STANDARD_INTERVAL;
var smoothingFactorOnRise = SMOOTHING_FACTOR;
Expand Down Expand Up @@ -66,7 +66,7 @@ toobusy.interval = function(newInterval) {
newInterval = Math.round(newInterval);
if(newInterval < 16) throw new Error("Interval should be greater than 16ms.");

toobusy.shutdown();
currentLag = 0;
interval = newInterval;
start();
return interval;
Expand Down Expand Up @@ -157,10 +157,11 @@ toobusy.smoothingFactorOnFall = function(newFactor) {
toobusy.shutdown = function(){
currentLag = 0;
checkInterval = clearInterval(checkInterval);
eventEmitter.removeAllListeners(LAG_EVENT);
};

toobusy.started = function() {
return !!checkInterval;
return Boolean(checkInterval != null);
};

/**
Expand Down Expand Up @@ -197,6 +198,9 @@ toobusy.lagFunction = function (lag, cLag, sFactorRise, sFactorFall) {
* Private - starts checking lag.
*/
function start() {
lastTime = Date.now();

clearInterval(checkInterval);
checkInterval = setInterval(function(){
var now = Date.now();
var lag = now - lastTime;
Expand Down
Loading