@@ -549,6 +549,113 @@ describe('ListWatchCache', () => {
549549 } as V1Pod ) ;
550550 expect ( list . length ) . to . equal ( 1 ) ;
551551 } ) ;
552+ it ( 'should not call handlers which have been unregistered' , async ( ) => {
553+ const fakeWatch = mock . mock ( Watch ) ;
554+ const list : V1Namespace [ ] = [ ] ;
555+ const listObj = {
556+ metadata : {
557+ resourceVersion : '12345' ,
558+ } as V1ListMeta ,
559+ items : list ,
560+ } as V1NamespaceList ;
561+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
562+ response : http . IncomingMessage ;
563+ body : V1NamespaceList ;
564+ } > {
565+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > ( ( resolve ) => {
566+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
567+ } ) ;
568+ } ;
569+ const watchCalled = new Promise ( ( resolve ) => {
570+ mock . when (
571+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
572+ ) . thenCall ( resolve ) ;
573+ } ) ;
574+ const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
575+
576+ const addedList1 : V1Namespace [ ] = [ ] ;
577+ const addToList1Fn = function ( obj : V1Namespace ) {
578+ addedList1 . push ( obj ) ;
579+ } ;
580+ const addedList2 : V1Namespace [ ] = [ ] ;
581+ const addToList2Fn = function ( obj : V1Namespace ) {
582+ addedList2 . push ( obj ) ;
583+ } ;
584+
585+ informer . start ( ) ;
586+
587+ await watchCalled ;
588+ const [ , , watchHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
589+
590+ informer . on ( ADD , addToList1Fn ) ;
591+ informer . on ( ADD , addToList2Fn ) ;
592+
593+ watchHandler ( 'ADDED' , {
594+ metadata : {
595+ name : 'name1' ,
596+ } as V1ObjectMeta ,
597+ } as V1Namespace ) ;
598+
599+ informer . off ( ADD , addToList2Fn ) ;
600+
601+ watchHandler ( 'ADDED' , {
602+ metadata : {
603+ name : 'name2' ,
604+ } as V1ObjectMeta ,
605+ } as V1Namespace ) ;
606+
607+ expect ( addedList1 . length ) . to . equal ( 2 ) ;
608+ expect ( addedList2 . length ) . to . equal ( 1 ) ;
609+ } ) ;
610+
611+ it ( 'mutating handlers in a callback should not affect those which remain' , async ( ) => {
612+ const fakeWatch = mock . mock ( Watch ) ;
613+ const list : V1Namespace [ ] = [ ] ;
614+ const listObj = {
615+ metadata : {
616+ resourceVersion : '12345' ,
617+ } as V1ListMeta ,
618+ items : list ,
619+ } as V1NamespaceList ;
620+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
621+ response : http . IncomingMessage ;
622+ body : V1NamespaceList ;
623+ } > {
624+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > ( ( resolve ) => {
625+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
626+ } ) ;
627+ } ;
628+ const watchCalled = new Promise ( ( resolve ) => {
629+ mock . when (
630+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
631+ ) . thenCall ( resolve ) ;
632+ } ) ;
633+ const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
634+
635+ const addedList : V1Namespace [ ] = [ ] ;
636+ const addToListFn = function ( obj : V1Namespace ) {
637+ addedList . push ( obj ) ;
638+ } ;
639+ const removeSelf = function ( ) {
640+ informer . off ( ADD , removeSelf ) ;
641+ } ;
642+
643+ informer . start ( ) ;
644+
645+ await watchCalled ;
646+ const [ , , watchHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
647+
648+ informer . on ( ADD , removeSelf ) ;
649+ informer . on ( ADD , addToListFn ) ;
650+
651+ watchHandler ( 'ADDED' , {
652+ metadata : {
653+ name : 'name1' ,
654+ } as V1ObjectMeta ,
655+ } as V1Namespace ) ;
656+
657+ expect ( addedList . length ) . to . equal ( 1 ) ;
658+ } ) ;
552659} ) ;
553660
554661describe ( 'delete items' , ( ) => {
0 commit comments