Skip to content

Commit 181b58f

Browse files
committed
refactor(pkg): Update devDependencies, more reliable tests
Some of the tests were relying on lag or holdover from previous tests and only happened to pass incidentally. This commit fixes some of that behavior.
1 parent 0f8e411 commit 181b58f

File tree

5 files changed

+380
-35
lines changed

5 files changed

+380
-35
lines changed

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
yarn.lock
2+
examples/
3+
tests.js

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"version": "0.5.1",
66
"dependencies": {},
77
"devDependencies": {
8-
"mocha": "1.7.0",
8+
"mocha": "^3.5.0",
99
"pre-commit": "^1.0.5",
10-
"should": "1.2.1"
10+
"should": "^12.0.0"
1111
},
1212
"maintainers": [
1313
"Samuel Reed <sam@tixelated.com>"

tests.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ function tightWork(duration) {
1212
/*global describe, it, beforeEach, afterEach */
1313
describe('the library', function() {
1414
it('should export a couple functions', function() {
15-
(toobusy).should.be.a('function');
16-
(toobusy.maxLag).should.be.a('function');
17-
(toobusy.shutdown).should.be.a('function');
18-
(toobusy.interval).should.be.a('function');
19-
(toobusy.shutdown).should.be.a('function');
15+
should(toobusy).be.Function();
16+
(toobusy.maxLag).should.be.Function();
17+
(toobusy.shutdown).should.be.Function();
18+
(toobusy.interval).should.be.Function();
19+
(toobusy.shutdown).should.be.Function();
2020
(toobusy).should.not.have.property('start');
2121
});
2222
it('should start automatically', function() {
@@ -61,9 +61,9 @@ describe('toobusy()', function() {
6161
// is nice for making these tests independent of each other.
6262
beforeEach(function() {
6363
toobusy.maxLag(10);
64-
toobusy.interval(250);
64+
toobusy.interval(50);
6565
});
66-
afterEach(function() {
66+
after(function() {
6767
toobusy.maxLag(70);
6868
toobusy.interval(500);
6969
});
@@ -94,75 +94,73 @@ describe('toobusy()', function() {
9494
it('should not emit lag events if the lag is less than the configured threshold',
9595
testLagEvent(100, 50, false));
9696
it('should emit lag events if the lag is greater than the configured threshold',
97-
testLagEvent(50, 100, true));
97+
testLagEvent(50, 150, true));
9898
it('should emit lag events if lag occurs and no threshold is specified',
9999
testLagEvent(undefined, 500, true));
100100

101101
function testLagEvent(threshold, work, expectFire) {
102102
return function (done) {
103103
var calledDone = false;
104+
var finish = function() {
105+
if (calledDone) return;
106+
calledDone = true;
107+
toobusy.shutdown(); // stops onLag() from firing again
108+
clearTimeout(workTimeout);
109+
done.apply(null, arguments);
110+
};
104111

105112
toobusy.onLag(function (lag) {
106-
if (calledDone) {
107-
return;
108-
}
109-
110113
if (!expectFire) {
111-
calledDone = true;
112-
done(new Error('lag event fired unexpectedly'));
113-
return;
114+
return finish(new Error('lag event fired unexpectedly'));
114115
}
115116

116117
should.exist(lag);
117118
lag.should.be.above(threshold || 0);
118-
119-
calledDone = true;
120-
done();
119+
finish();
121120
}, threshold);
122121

123122
if (!expectFire) {
124123
setTimeout(function () {
125-
if (!calledDone) {
126-
calledDone = true;
127-
done();
128-
}
124+
finish();
129125
}, work + threshold);
130126
}
131127

132-
tightWork(work);
128+
// Do work 3x to work around smoothing factor
129+
var count = 0;
130+
var workTimeout = setTimeout(function working() {
131+
tightWork(work);
132+
if (++count < 3) workTimeout = setTimeout(working);
133+
})
133134
}
134135
}
135136
});
136137
});
137138

138139
describe('smoothingFactor', function() {
139-
//Sometimes the default 2s timeout is hit on this suite, raise to 10s.
140+
// Sometimes the default 2s timeout is hit on this suite, raise to 10s.
140141
this.timeout(10 * 1000);
141142

142143
beforeEach(function() {
143144
toobusy.maxLag(10);
144145
toobusy.interval(250);
145146
});
146-
afterEach(function() {
147+
after(function() {
147148
toobusy.maxLag(70);
148149
toobusy.interval(500);
149150
});
150-
it('should default to 1/3', function(done) {
151+
it('should default to 1/3', function() {
151152
(toobusy.smoothingFactor()).should.equal(1/3);
152-
done();
153153
});
154-
it('should throw an exception for invalid values', function(done) {
154+
it('should throw an exception for invalid values', function() {
155155
(function() { toobusy.smoothingFactor(0); }).should.throw;
156156
(function() { toobusy.smoothingFactor(2); }).should.throw;
157157
(function() { toobusy.smoothingFactor(-1); }).should.throw;
158158
(function() { toobusy.smoothingFactor(1); }).should.not.throw;
159-
done();
160159
});
161-
it('should be configurable', function(done) {
160+
it('should be configurable', function() {
162161
(toobusy.smoothingFactor(0.9)).should.equal(0.9);
163162
(toobusy.smoothingFactor(0.1)).should.equal(0.1);
164163
(toobusy.smoothingFactor()).should.equal(0.1);
165-
done();
166164
});
167165
it('should allow no dampening', function(done) {
168166
var cycles_to_toobusy = 0;

toobusy.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ toobusy.interval = function(newInterval) {
5454
newInterval = Math.round(newInterval);
5555
if(newInterval < 16) throw new Error("Interval should be greater than 16ms.");
5656

57-
toobusy.shutdown();
57+
currentLag = 0;
5858
interval = newInterval;
5959
start();
6060
return interval;
@@ -123,7 +123,7 @@ toobusy.shutdown = function(){
123123
};
124124

125125
toobusy.started = function() {
126-
return Boolean(checkInterval);
126+
return Boolean(checkInterval != null);
127127
};
128128

129129
/**
@@ -149,6 +149,7 @@ toobusy.onLag = function (fn, threshold) {
149149
function start() {
150150
lastTime = Date.now();
151151

152+
clearInterval(checkInterval);
152153
checkInterval = setInterval(function(){
153154
var now = Date.now();
154155
var lag = now - lastTime;

0 commit comments

Comments
 (0)