@@ -12,11 +12,11 @@ function tightWork(duration) {
1212/*global describe, it, beforeEach, afterEach */
1313describe ( '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
138139describe ( '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 ;
0 commit comments