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,25 @@ 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 ( ( )  =>  { 
37+     clock . restore ( ) ; 
38+   } ) ; 
39+ 
2540  describe ( '#cleanQueue' ,  function  ( )  { 
2641    var 
2742      cleanQueue  =  Kuzzle . __get__ ( 'cleanQueue' ) ; 
@@ -92,31 +107,28 @@ describe('Offline queue management', function () {
92107      emitRequestStub . reset ( ) ; 
93108    } ) ; 
94109
95-     it ( 'should play all queued requests' ,  function  ( done )  { 
110+     it ( 'should play all queued requests' ,  function  ( )  { 
96111      var 
97112        numRequests  =  kuzzle . offlineQueue . length , 
98113        eventStub  =  sinon . stub ( ) ; 
99114
100-       this . timeout ( 200 ) ; 
101115      kuzzle . addListener ( 'offlineQueuePop' ,  eventStub ) ; 
102116      dequeue . call ( kuzzle ) ; 
103117
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 ) ; 
118+       clock . tick ( numRequests  *  kuzzle . replayInterval  +  50 ) ; 
119+ 
120+       should ( emitRequestStub . callCount ) . be . exactly ( numRequests ) ; 
121+       should ( kuzzle . offlineQueue ) . be . an . Array ( ) ; 
122+       should ( kuzzle . offlineQueue . length ) . be . exactly ( 0 ) ; 
123+       should ( kuzzle . queuing ) . be . false ( ) ; 
124+       should ( eventStub . callCount ) . be . exactly ( numRequests ) ; 
112125    } ) ; 
113126
114-     it ( 'should also load the queue provided by the offlineQueueLoader property' ,  function  ( done )  { 
127+     it ( 'should also load the queue provided by the offlineQueueLoader property' ,  function  ( )  { 
115128      var 
116129        numRequests  =  kuzzle . offlineQueue . length , 
117130        eventStub  =  sinon . stub ( ) ; 
118131
119-       this . timeout ( 200 ) ; 
120132      kuzzle . offlineQueueLoader  =  function  ( )  { 
121133        return  [ 
122134          { query : { requestId : 'foo' ,  action : 'action' ,  controller : 'controller' } } , 
@@ -126,17 +138,16 @@ describe('Offline queue management', function () {
126138      kuzzle . addListener ( 'offlineQueuePop' ,  eventStub ) ; 
127139      dequeue . call ( kuzzle ) ; 
128140
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 ) ; 
141+       clock . tick ( ( numRequests  +  2 )  *  kuzzle . replayInterval  +  50 ) ; 
142+ 
143+       should ( emitRequestStub . callCount ) . be . exactly ( numRequests  +  2 ) ; 
144+       should ( kuzzle . offlineQueue ) . be . an . Array ( ) ; 
145+       should ( kuzzle . offlineQueue . length ) . be . exactly ( 0 ) ; 
146+       should ( kuzzle . queuing ) . be . false ( ) ; 
147+       should ( eventStub . callCount ) . be . exactly ( numRequests  +  2 ) ; 
137148    } ) ; 
138149
139-     it ( 'should filter duplicates from the offlineQueueLoader and the cached queue ' ,  function   ( done )  { 
150+     it ( 'should filter duplicates from the offlineQueueLoader and the cached queue ' ,  ( )   =>  { 
140151      var 
141152        numRequests  =  kuzzle . offlineQueue . length , 
142153        eventStub  =  sinon . stub ( ) ; 
@@ -151,14 +162,13 @@ describe('Offline queue management', function () {
151162      kuzzle . addListener ( 'offlineQueuePop' ,  eventStub ) ; 
152163      dequeue . call ( kuzzle ) ; 
153164
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 ) ; 
165+       clock . tick ( ( numRequests  +  1 )  *  kuzzle . replayInterval  +  50 ) ; 
166+ 
167+       should ( emitRequestStub . callCount ) . be . exactly ( numRequests  +  1 ) ; 
168+       should ( kuzzle . offlineQueue ) . be . an . Array ( ) ; 
169+       should ( kuzzle . offlineQueue . length ) . be . exactly ( 0 ) ; 
170+       should ( kuzzle . queuing ) . be . false ( ) ; 
171+       should ( eventStub . callCount ) . be . exactly ( numRequests  +  1 ) ; 
162172    } ) ; 
163173
164174    it ( 'should throw on erroneous queries returned by the offlineQueueLoader property' ,  function  ( )  { 
0 commit comments