55 */
66
77import _ from 'lodash' ;
8- import { Subject } from 'rxjs' ;
8+ import { Subject , of , BehaviorSubject } from 'rxjs' ;
99import { Option , none , some } from 'fp-ts/lib/Option' ;
1010import { createTaskPoller , PollingError , PollingErrorType } from './task_poller' ;
1111import { fakeSchedulers } from 'rxjs-marbles/jest' ;
@@ -24,10 +24,11 @@ describe('TaskPoller', () => {
2424
2525 const work = jest . fn ( async ( ) => true ) ;
2626 createTaskPoller < void , boolean > ( {
27- pollInterval,
27+ pollInterval$ : of ( pollInterval ) ,
2828 bufferCapacity,
2929 getCapacity : ( ) => 1 ,
3030 work,
31+ workTimeout : pollInterval * 5 ,
3132 pollRequests$ : new Subject < Option < void > > ( ) ,
3233 } ) . subscribe ( ( ) => { } ) ;
3334
@@ -40,9 +41,51 @@ describe('TaskPoller', () => {
4041 await sleep ( 0 ) ;
4142 expect ( work ) . toHaveBeenCalledTimes ( 1 ) ;
4243
44+ await sleep ( 0 ) ;
45+ await sleep ( 0 ) ;
46+ advance ( pollInterval + 10 ) ;
47+ await sleep ( 0 ) ;
48+ expect ( work ) . toHaveBeenCalledTimes ( 2 ) ;
49+ } )
50+ ) ;
51+
52+ test (
53+ 'poller adapts to pollInterval changes' ,
54+ fakeSchedulers ( async ( advance ) => {
55+ const pollInterval = 100 ;
56+ const pollInterval$ = new BehaviorSubject ( pollInterval ) ;
57+ const bufferCapacity = 5 ;
58+
59+ const work = jest . fn ( async ( ) => true ) ;
60+ createTaskPoller < void , boolean > ( {
61+ pollInterval$,
62+ bufferCapacity,
63+ getCapacity : ( ) => 1 ,
64+ work,
65+ workTimeout : pollInterval * 5 ,
66+ pollRequests$ : new Subject < Option < void > > ( ) ,
67+ } ) . subscribe ( ( ) => { } ) ;
68+
69+ // `work` is async, we have to force a node `tick`
4370 await sleep ( 0 ) ;
4471 advance ( pollInterval ) ;
72+ expect ( work ) . toHaveBeenCalledTimes ( 1 ) ;
73+
74+ pollInterval$ . next ( pollInterval * 2 ) ;
75+
76+ // `work` is async, we have to force a node `tick`
77+ await sleep ( 0 ) ;
78+ advance ( pollInterval ) ;
79+ expect ( work ) . toHaveBeenCalledTimes ( 1 ) ;
80+ advance ( pollInterval ) ;
4581 expect ( work ) . toHaveBeenCalledTimes ( 2 ) ;
82+
83+ pollInterval$ . next ( pollInterval / 2 ) ;
84+
85+ // `work` is async, we have to force a node `tick`
86+ await sleep ( 0 ) ;
87+ advance ( pollInterval / 2 ) ;
88+ expect ( work ) . toHaveBeenCalledTimes ( 3 ) ;
4689 } )
4790 ) ;
4891
@@ -56,9 +99,10 @@ describe('TaskPoller', () => {
5699
57100 let hasCapacity = true ;
58101 createTaskPoller < void , boolean > ( {
59- pollInterval,
102+ pollInterval$ : of ( pollInterval ) ,
60103 bufferCapacity,
61104 work,
105+ workTimeout : pollInterval * 5 ,
62106 getCapacity : ( ) => ( hasCapacity ? 1 : 0 ) ,
63107 pollRequests$ : new Subject < Option < void > > ( ) ,
64108 } ) . subscribe ( ( ) => { } ) ;
@@ -113,9 +157,10 @@ describe('TaskPoller', () => {
113157 const work = jest . fn ( async ( ) => true ) ;
114158 const pollRequests$ = new Subject < Option < void > > ( ) ;
115159 createTaskPoller < void , boolean > ( {
116- pollInterval,
160+ pollInterval$ : of ( pollInterval ) ,
117161 bufferCapacity,
118162 work,
163+ workTimeout : pollInterval * 5 ,
119164 getCapacity : ( ) => 1 ,
120165 pollRequests$,
121166 } ) . subscribe ( jest . fn ( ) ) ;
@@ -157,9 +202,10 @@ describe('TaskPoller', () => {
157202 const work = jest . fn ( async ( ) => true ) ;
158203 const pollRequests$ = new Subject < Option < void > > ( ) ;
159204 createTaskPoller < void , boolean > ( {
160- pollInterval,
205+ pollInterval$ : of ( pollInterval ) ,
161206 bufferCapacity,
162207 work,
208+ workTimeout : pollInterval * 5 ,
163209 getCapacity : ( ) => ( hasCapacity ? 1 : 0 ) ,
164210 pollRequests$,
165211 } ) . subscribe ( ( ) => { } ) ;
@@ -200,9 +246,10 @@ describe('TaskPoller', () => {
200246 const work = jest . fn ( async ( ) => true ) ;
201247 const pollRequests$ = new Subject < Option < string > > ( ) ;
202248 createTaskPoller < string , boolean > ( {
203- pollInterval,
249+ pollInterval$ : of ( pollInterval ) ,
204250 bufferCapacity,
205251 work,
252+ workTimeout : pollInterval * 5 ,
206253 getCapacity : ( ) => 1 ,
207254 pollRequests$,
208255 } ) . subscribe ( ( ) => { } ) ;
@@ -235,7 +282,7 @@ describe('TaskPoller', () => {
235282 const handler = jest . fn ( ) ;
236283 const pollRequests$ = new Subject < Option < string > > ( ) ;
237284 createTaskPoller < string , string [ ] > ( {
238- pollInterval,
285+ pollInterval$ : of ( pollInterval ) ,
239286 bufferCapacity,
240287 work : async ( ...args ) => {
241288 await worker ;
@@ -285,7 +332,7 @@ describe('TaskPoller', () => {
285332 type ResolvableTupple = [ string , PromiseLike < void > & Resolvable ] ;
286333 const pollRequests$ = new Subject < Option < ResolvableTupple > > ( ) ;
287334 createTaskPoller < [ string , Resolvable ] , string [ ] > ( {
288- pollInterval,
335+ pollInterval$ : of ( pollInterval ) ,
289336 bufferCapacity,
290337 work : async ( ...resolvables ) => {
291338 await Promise . all ( resolvables . map ( ( [ , future ] ) => future ) ) ;
@@ -344,11 +391,12 @@ describe('TaskPoller', () => {
344391 const handler = jest . fn ( ) ;
345392 const pollRequests$ = new Subject < Option < string > > ( ) ;
346393 createTaskPoller < string , string [ ] > ( {
347- pollInterval,
394+ pollInterval$ : of ( pollInterval ) ,
348395 bufferCapacity,
349396 work : async ( ...args ) => {
350397 throw new Error ( 'failed to work' ) ;
351398 } ,
399+ workTimeout : pollInterval * 5 ,
352400 getCapacity : ( ) => 5 ,
353401 pollRequests$,
354402 } ) . subscribe ( handler ) ;
@@ -383,9 +431,10 @@ describe('TaskPoller', () => {
383431 return callCount ;
384432 } ) ;
385433 createTaskPoller < string , number > ( {
386- pollInterval,
434+ pollInterval$ : of ( pollInterval ) ,
387435 bufferCapacity,
388436 work,
437+ workTimeout : pollInterval * 5 ,
389438 getCapacity : ( ) => 5 ,
390439 pollRequests$,
391440 } ) . subscribe ( handler ) ;
@@ -424,9 +473,10 @@ describe('TaskPoller', () => {
424473 const work = jest . fn ( async ( ) => { } ) ;
425474 const pollRequests$ = new Subject < Option < string > > ( ) ;
426475 createTaskPoller < string , void > ( {
427- pollInterval,
476+ pollInterval$ : of ( pollInterval ) ,
428477 bufferCapacity,
429478 work,
479+ workTimeout : pollInterval * 5 ,
430480 getCapacity : ( ) => 5 ,
431481 pollRequests$,
432482 } ) . subscribe ( handler ) ;
0 commit comments