@@ -230,9 +230,10 @@ it('removes synchronizer that reports unrecoverable error and loops on remaining
230230 _statusCallback : ( status : DataSourceState , err ?: any ) => void ,
231231 ) => {
232232 _statusCallback ( DataSourceState . Initializing ) ;
233- _statusCallback ( DataSourceState . Off , {
233+ _statusCallback ( DataSourceState . Closed , {
234234 name : 'Error' ,
235- message : 'I am an unrecoverable error!' , // error will lead to culling
235+ message : 'I am an unrecoverable error!' , // error will lead to culling,
236+ recoverable : false ,
236237 } ) ;
237238 } ,
238239 ) ,
@@ -380,93 +381,99 @@ it('reports error when all initializers fail', async () => {
380381 ) ;
381382 expect ( statusCallback ) . toHaveBeenNthCalledWith ( 4 , DataSourceState . Closed , {
382383 name : 'ExhaustedDataSources' ,
383- message :
384- 'CompositeDataSource has exhausted all configured datasources (2 initializers, 0 synchronizers).' ,
384+ message : 'CompositeDataSource has exhausted all configured initializers and synchronizers.' ,
385385 } ) ;
386386 expect ( statusCallback ) . toHaveBeenCalledTimes ( 4 ) ;
387387} ) ;
388388
389- // it('it reports DataSourceState Off when all synchronizers report Off', async () => {
390- // const mockInitializer1: DataSource = {
391- // start: jest
392- // .fn()
393- // .mockImplementation(
394- // (
395- // _dataCallback: (basis: boolean, data: any) => void,
396- // _statusCallback: (status: DataSourceState, err?: any) => void,
397- // ) => {
398- // _statusCallback(DataSourceState.Initializing);
399- // _statusCallback(DataSourceState.Valid);
400- // _dataCallback(true, { key: 'init1' });
401- // _statusCallback(DataSourceState.Closed);
402- // },
403- // ),
404- // stop: jest.fn(),
405- // };
406-
407- // const mockSynchronizer1 = {
408- // start: jest
409- // .fn()
410- // .mockImplementation(
411- // (
412- // _dataCallback: (basis: boolean, data: any) => void,
413- // _statusCallback: (status: DataSourceState, err?: any) => void,
414- // ) => {
415- // _statusCallback(DataSourceState.Initializing);
416- // _statusCallback(DataSourceState.Off, {
417- // name: 'Error1',
418- // message: 'I am an unrecoverable error!',
419- // });
420- // },
421- // ),
422- // stop: jest.fn(),
423- // };
424-
425- // const mockSynchronizer2 = {
426- // start: jest
427- // .fn()
428- // .mockImplementation(
429- // (
430- // _dataCallback: (basis: boolean, data: any) => void,
431- // _statusCallback: (status: DataSourceState, err?: any) => void,
432- // ) => {
433- // _statusCallback(DataSourceState.Initializing);
434- // _statusCallback(DataSourceState.Off, {
435- // name: 'Error2',
436- // message: 'I am an unrecoverable error!',
437- // });
438- // },
439- // ),
440- // stop: jest.fn(),
441- // };
442-
443- // const underTest = new CompositeDataSource(
444- // [makeDataSourceFactory(mockInitializer1)],
445- // [makeDataSourceFactory(mockSynchronizer1), makeDataSourceFactory(mockSynchronizer2)],
446- // undefined,
447- // makeTestTransitionConditions(),
448- // makeZeroBackoff(),
449- // );
450-
451- // let statusCallback;
452- // await new Promise<void>((resolve) => {
453- // statusCallback = jest.fn((state: DataSourceState, err: any) => {
454- // if (err && err.name === 'Error2') {
455- // resolve();
456- // }
457- // });
458-
459- // underTest.start(jest.fn(), statusCallback);
460- // });
461-
462- // expect(mockInitializer1.start).toHaveBeenCalledTimes(1);
463- // expect(mockSynchronizer1.start).toHaveBeenCalledTimes(1);
464- // expect(mockSynchronizer2.start).toHaveBeenCalledTimes(1);
465- // expect(statusCallback).toHaveBeenNthCalledWith(1, DataSourceState.Initializing, null);
466- // expect(statusCallback).toHaveBeenNthCalledWith(2, DataSourceState.Valid, null);
467- // expect(statusCallback).toHaveBeenNthCalledWith(3, DataSourceState.Interrupted, expect.anything());
468- // expect(statusCallback).toHaveBeenNthCalledWith(4, DataSourceState.Off, null);
469- // });
389+ it ( 'it reports DataSourceState Closed when all synchronizers report Closed with unrecoverable errors' , async ( ) => {
390+ const mockInitializer1 : DataSource = {
391+ start : jest
392+ . fn ( )
393+ . mockImplementation (
394+ (
395+ _dataCallback : ( basis : boolean , data : any ) => void ,
396+ _statusCallback : ( status : DataSourceState , err ?: any ) => void ,
397+ ) => {
398+ _statusCallback ( DataSourceState . Initializing ) ;
399+ _statusCallback ( DataSourceState . Valid ) ;
400+ _dataCallback ( true , { key : 'init1' } ) ;
401+ _statusCallback ( DataSourceState . Closed ) ;
402+ } ,
403+ ) ,
404+ stop : jest . fn ( ) ,
405+ } ;
406+
407+ const mockSynchronizer1 = {
408+ start : jest
409+ . fn ( )
410+ . mockImplementation (
411+ (
412+ _dataCallback : ( basis : boolean , data : any ) => void ,
413+ _statusCallback : ( status : DataSourceState , err ?: any ) => void ,
414+ ) => {
415+ _statusCallback ( DataSourceState . Initializing ) ;
416+ _statusCallback ( DataSourceState . Closed , {
417+ name : 'Error1' ,
418+ message : 'I am an unrecoverable error!' ,
419+ recoverable : false ,
420+ } ) ;
421+ } ,
422+ ) ,
423+ stop : jest . fn ( ) ,
424+ } ;
425+
426+ const mockSynchronizer2 = {
427+ start : jest
428+ . fn ( )
429+ . mockImplementation (
430+ (
431+ _dataCallback : ( basis : boolean , data : any ) => void ,
432+ _statusCallback : ( status : DataSourceState , err ?: any ) => void ,
433+ ) => {
434+ _statusCallback ( DataSourceState . Initializing ) ;
435+ _statusCallback ( DataSourceState . Closed , {
436+ name : 'Error2' ,
437+ message : 'I am an unrecoverable error!' ,
438+ recoverable : false ,
439+ } ) ;
440+ } ,
441+ ) ,
442+ stop : jest . fn ( ) ,
443+ } ;
444+
445+ const underTest = new CompositeDataSource (
446+ [ makeDataSourceFactory ( mockInitializer1 ) ] ,
447+ [ makeDataSourceFactory ( mockSynchronizer1 ) , makeDataSourceFactory ( mockSynchronizer2 ) ] ,
448+ undefined ,
449+ makeTestTransitionConditions ( ) ,
450+ makeZeroBackoff ( ) ,
451+ ) ;
452+
453+ let statusCallback ;
454+ await new Promise < void > ( ( resolve ) => {
455+ statusCallback = jest . fn ( ( state : DataSourceState , _ : any ) => {
456+ if ( state === DataSourceState . Closed ) {
457+ resolve ( ) ;
458+ }
459+ } ) ;
460+
461+ underTest . start ( jest . fn ( ) , statusCallback ) ;
462+ } ) ;
463+
464+ expect ( mockInitializer1 . start ) . toHaveBeenCalledTimes ( 1 ) ;
465+ expect ( mockSynchronizer1 . start ) . toHaveBeenCalledTimes ( 1 ) ;
466+ expect ( mockSynchronizer2 . start ) . toHaveBeenCalledTimes ( 1 ) ;
467+ expect ( statusCallback ) . toHaveBeenNthCalledWith ( 1 , DataSourceState . Initializing , undefined ) ;
468+ expect ( statusCallback ) . toHaveBeenNthCalledWith ( 2 , DataSourceState . Valid , undefined ) ;
469+ expect ( statusCallback ) . toHaveBeenNthCalledWith ( 3 , DataSourceState . Interrupted , undefined ) ; // initializer closes properly
470+ expect ( statusCallback ) . toHaveBeenNthCalledWith ( 4 , DataSourceState . Interrupted , expect . anything ( ) ) ; // sync1 closed with unrecoverable error
471+ expect ( statusCallback ) . toHaveBeenNthCalledWith ( 5 , DataSourceState . Interrupted , expect . anything ( ) ) ; // sync2 closed with unrecoverable error
472+ expect ( statusCallback ) . toHaveBeenNthCalledWith ( 6 , DataSourceState . Closed , {
473+ name : 'ExhaustedDataSources' ,
474+ message : `CompositeDataSource has exhausted all configured initializers and synchronizers.` ,
475+ } ) ;
476+ } ) ;
470477
471478it ( 'can be stopped when in thrashing synchronizer fallback loop' , async ( ) => {
472479 const mockInitializer1 = {
@@ -639,8 +646,7 @@ it('is well behaved with no initializers and no synchronizers configured', async
639646 expect ( statusCallback ) . toHaveBeenNthCalledWith ( 1 , DataSourceState . Initializing , undefined ) ; // initializer
640647 expect ( statusCallback ) . toHaveBeenNthCalledWith ( 2 , DataSourceState . Closed , {
641648 name : 'ExhaustedDataSources' ,
642- message :
643- 'CompositeDataSource has exhausted all configured datasources (0 initializers, 0 synchronizers).' ,
649+ message : 'CompositeDataSource has exhausted all configured initializers and synchronizers.' ,
644650 } ) ;
645651} ) ;
646652
@@ -730,8 +736,7 @@ it('is well behaved with an initializer and no synchronizers configured', async
730736 expect ( statusCallback ) . toHaveBeenNthCalledWith ( 3 , DataSourceState . Interrupted , undefined ) ; // initializer got data
731737 expect ( statusCallback ) . toHaveBeenNthCalledWith ( 4 , DataSourceState . Closed , {
732738 name : 'ExhaustedDataSources' ,
733- message :
734- 'CompositeDataSource has exhausted all configured datasources (1 initializers, 0 synchronizers).' ,
739+ message : 'CompositeDataSource has exhausted all configured initializers and synchronizers.' ,
735740 } ) ;
736741} ) ;
737742
0 commit comments