66
77describe ( 'Offline queue management' ,  function  ( )  { 
88  var 
9+     clock , 
910    now , 
10-     kuzzle ; 
11+     kuzzle , 
12+     reset ; 
1113
1214  beforeEach ( function  ( )  { 
1315    var  pastTime  =  60050 ; 
@@ -16,12 +18,26 @@ describe('Offline queue management', function () {
1618
1719    // queuing a bunch of 7 requests from 1min ago to right now, 10s apart 
1820    now  =  Date . now ( ) ; 
21+     clock  =  sinon . useFakeTimers ( now ) ; 
22+ 
23+     reset  =  Kuzzle . __set__ ( { 
24+       setTimeout : clock . setTimeout , 
25+       setInterval : clock . setInterval , 
26+       clearTimeout : clock . clearTimeout , 
27+       clearInterval : clock . clearInterval 
28+     } ) ; 
29+ 
1930    while  ( pastTime  >=  0 )  { 
2031      kuzzle . offlineQueue . push ( { ts : now  -  pastTime ,  query : { requestId : pastTime ,  action : 'foo' ,  controller : 'bar' } ,  cb : function  ( )  { } } ) ; 
2132      pastTime  -=  10000 ; 
2233    } 
2334  } ) ; 
2435
36+   afterEach ( function  ( )  { 
37+     clock . restore ( ) ; 
38+     reset ( ) ; 
39+   } ) ; 
40+ 
2541  describe ( '#cleanQueue' ,  function  ( )  { 
2642    var 
2743      cleanQueue  =  Kuzzle . __get__ ( 'cleanQueue' ) ; 
@@ -92,31 +108,28 @@ describe('Offline queue management', function () {
92108      emitRequestStub . reset ( ) ; 
93109    } ) ; 
94110
95-     it ( 'should play all queued requests' ,  function  ( done )  { 
111+     it ( 'should play all queued requests' ,  function  ( )  { 
96112      var 
97113        numRequests  =  kuzzle . offlineQueue . length , 
98114        eventStub  =  sinon . stub ( ) ; 
99115
100-       this . timeout ( 200 ) ; 
101116      kuzzle . addListener ( 'offlineQueuePop' ,  eventStub ) ; 
102117      dequeue . call ( kuzzle ) ; 
103118
104-       setTimeout ( function  ( )  { 
105-         should ( emitRequestStub . callCount ) . be . exactly ( numRequests ) ; 
106-         should ( kuzzle . offlineQueue ) . be . an . Array ( ) ; 
107-         should ( kuzzle . offlineQueue . length ) . be . exactly ( 0 ) ; 
108-         should ( kuzzle . queuing ) . be . false ( ) ; 
109-         should ( eventStub . callCount ) . be . exactly ( numRequests ) ; 
110-         done ( ) ; 
111-       } ,  numRequests  *  kuzzle . replayInterval  +  50 ) ; 
119+       clock . tick ( numRequests  *  kuzzle . replayInterval  +  50 ) ; 
120+ 
121+       should ( emitRequestStub . callCount ) . be . exactly ( numRequests ) ; 
122+       should ( kuzzle . offlineQueue ) . be . an . Array ( ) ; 
123+       should ( kuzzle . offlineQueue . length ) . be . exactly ( 0 ) ; 
124+       should ( kuzzle . queuing ) . be . false ( ) ; 
125+       should ( eventStub . callCount ) . be . exactly ( numRequests ) ; 
112126    } ) ; 
113127
114-     it ( 'should also load the queue provided by the offlineQueueLoader property' ,  function  ( done )  { 
128+     it ( 'should also load the queue provided by the offlineQueueLoader property' ,  function  ( )  { 
115129      var 
116130        numRequests  =  kuzzle . offlineQueue . length , 
117131        eventStub  =  sinon . stub ( ) ; 
118132
119-       this . timeout ( 200 ) ; 
120133      kuzzle . offlineQueueLoader  =  function  ( )  { 
121134        return  [ 
122135          { query : { requestId : 'foo' ,  action : 'action' ,  controller : 'controller' } } , 
@@ -126,17 +139,16 @@ describe('Offline queue management', function () {
126139      kuzzle . addListener ( 'offlineQueuePop' ,  eventStub ) ; 
127140      dequeue . call ( kuzzle ) ; 
128141
129-       setTimeout ( function  ( )  { 
130-         should ( emitRequestStub . callCount ) . be . exactly ( numRequests  +  2 ) ; 
131-         should ( kuzzle . offlineQueue ) . be . an . Array ( ) ; 
132-         should ( kuzzle . offlineQueue . length ) . be . exactly ( 0 ) ; 
133-         should ( kuzzle . queuing ) . be . false ( ) ; 
134-         should ( eventStub . callCount ) . be . exactly ( numRequests  +  2 ) ; 
135-         done ( ) ; 
136-       } ,  ( numRequests  + 2 )  *  kuzzle . replayInterval  +  50 ) ; 
142+       clock . tick ( ( numRequests  +  2 )  *  kuzzle . replayInterval  +  50 ) ; 
143+ 
144+       should ( emitRequestStub . callCount ) . be . exactly ( numRequests  +  2 ) ; 
145+       should ( kuzzle . offlineQueue ) . be . an . Array ( ) ; 
146+       should ( kuzzle . offlineQueue . length ) . be . exactly ( 0 ) ; 
147+       should ( kuzzle . queuing ) . be . false ( ) ; 
148+       should ( eventStub . callCount ) . be . exactly ( numRequests  +  2 ) ; 
137149    } ) ; 
138150
139-     it ( 'should filter duplicates from the offlineQueueLoader and the cached queue ' ,  function  ( done )  { 
151+     it ( 'should filter duplicates from the offlineQueueLoader and the cached queue ' ,  function  ( )  { 
140152      var 
141153        numRequests  =  kuzzle . offlineQueue . length , 
142154        eventStub  =  sinon . stub ( ) ; 
@@ -151,14 +163,13 @@ describe('Offline queue management', function () {
151163      kuzzle . addListener ( 'offlineQueuePop' ,  eventStub ) ; 
152164      dequeue . call ( kuzzle ) ; 
153165
154-       setTimeout ( function  ( )  { 
155-         should ( emitRequestStub . callCount ) . be . exactly ( numRequests  +  1 ) ; 
156-         should ( kuzzle . offlineQueue ) . be . an . Array ( ) ; 
157-         should ( kuzzle . offlineQueue . length ) . be . exactly ( 0 ) ; 
158-         should ( kuzzle . queuing ) . be . false ( ) ; 
159-         should ( eventStub . callCount ) . be . exactly ( numRequests  +  1 ) ; 
160-         done ( ) ; 
161-       } ,  ( numRequests  +  1 )  *  kuzzle . replayInterval  +  50 ) ; 
166+       clock . tick ( ( numRequests  +  1 )  *  kuzzle . replayInterval  +  50 ) ; 
167+ 
168+       should ( emitRequestStub . callCount ) . be . exactly ( numRequests  +  1 ) ; 
169+       should ( kuzzle . offlineQueue ) . be . an . Array ( ) ; 
170+       should ( kuzzle . offlineQueue . length ) . be . exactly ( 0 ) ; 
171+       should ( kuzzle . queuing ) . be . false ( ) ; 
172+       should ( eventStub . callCount ) . be . exactly ( numRequests  +  1 ) ; 
162173    } ) ; 
163174
164175    it ( 'should throw on erroneous queries returned by the offlineQueueLoader property' ,  function  ( )  { 
0 commit comments