@@ -9,7 +9,7 @@ import { Subject } from 'rxjs';
99import { Option , none , some } from 'fp-ts/lib/Option' ;
1010import { createTaskPoller , PollingError , PollingErrorType } from './task_poller' ;
1111import { fakeSchedulers } from 'rxjs-marbles/jest' ;
12- import { sleep , resolvable } from './test_utils' ;
12+ import { sleep , resolvable , Resolvable } from './test_utils' ;
1313import { asOk , asErr } from './lib/result_type' ;
1414
1515describe ( 'TaskPoller' , ( ) => {
@@ -243,6 +243,7 @@ describe('TaskPoller', () => {
243243 } ,
244244 getCapacity : ( ) => 5 ,
245245 pollRequests$,
246+ workTimeout : pollInterval * 5 ,
246247 } ) . subscribe ( handler ) ;
247248
248249 pollRequests$ . next ( some ( 'one' ) ) ;
@@ -272,6 +273,68 @@ describe('TaskPoller', () => {
272273 } )
273274 ) ;
274275
276+ test (
277+ 'work times out whe nit exceeds a predefined amount of time' ,
278+ fakeSchedulers ( async ( advance ) => {
279+ const pollInterval = 100 ;
280+ const workTimeout = pollInterval * 2 ;
281+ const bufferCapacity = 2 ;
282+
283+ const handler = jest . fn ( ) ;
284+
285+ type ResolvableTupple = [ string , PromiseLike < void > & Resolvable ] ;
286+ const pollRequests$ = new Subject < Option < ResolvableTupple > > ( ) ;
287+ createTaskPoller < [ string , Resolvable ] , string [ ] > ( {
288+ pollInterval,
289+ bufferCapacity,
290+ work : async ( ...resolvables ) => {
291+ await Promise . all ( resolvables . map ( ( [ , future ] ) => future ) ) ;
292+ return resolvables . map ( ( [ name ] ) => name ) ;
293+ } ,
294+ getCapacity : ( ) => 5 ,
295+ pollRequests$,
296+ workTimeout,
297+ } ) . subscribe ( handler ) ;
298+
299+ const one : ResolvableTupple = [ 'one' , resolvable ( ) ] ;
300+ pollRequests$ . next ( some ( one ) ) ;
301+
302+ // split these into two payloads
303+ advance ( pollInterval ) ;
304+
305+ const two : ResolvableTupple = [ 'two' , resolvable ( ) ] ;
306+ const three : ResolvableTupple = [ 'three' , resolvable ( ) ] ;
307+ pollRequests$ . next ( some ( two ) ) ;
308+ pollRequests$ . next ( some ( three ) ) ;
309+
310+ advance ( workTimeout ) ;
311+ await sleep ( workTimeout ) ;
312+
313+ // one resolves too late!
314+ one [ 1 ] . resolve ( ) ;
315+
316+ expect ( handler ) . toHaveBeenCalledWith (
317+ asErr (
318+ new PollingError < string > (
319+ 'Failed to poll for work: Error: work has timed out' ,
320+ PollingErrorType . WorkError ,
321+ none
322+ )
323+ )
324+ ) ;
325+ expect ( handler . mock . calls [ 0 ] [ 0 ] . error . type ) . toEqual ( PollingErrorType . WorkError ) ;
326+
327+ // two and three in time
328+ two [ 1 ] . resolve ( ) ;
329+ three [ 1 ] . resolve ( ) ;
330+
331+ advance ( pollInterval ) ;
332+ await sleep ( pollInterval ) ;
333+
334+ expect ( handler ) . toHaveBeenCalledWith ( asOk ( [ 'two' , 'three' ] ) ) ;
335+ } )
336+ ) ;
337+
275338 test (
276339 'returns an error when polling for work fails' ,
277340 fakeSchedulers ( async ( advance ) => {
0 commit comments