@@ -9,7 +9,7 @@ import { EventEmitter } from 'ws';
9
9
10
10
import { V1Namespace , V1NamespaceList , V1ObjectMeta , V1Pod , V1ListMeta } from './api' ;
11
11
import { deleteObject , ListWatch , deleteItems } from './cache' ;
12
- import { ADD , UPDATE , DELETE , ListPromise } from './informer' ;
12
+ import { ADD , UPDATE , DELETE , ERROR , ListPromise } from './informer' ;
13
13
14
14
use ( chaiAsPromised ) ;
15
15
@@ -577,6 +577,7 @@ describe('ListWatchCache', () => {
577
577
expect ( cache . list ( 'ns2' ) . length ) . to . equal ( 0 ) ;
578
578
expect ( cache . get ( 'name2' , 'ns2' ) ) . to . equal ( undefined ) ;
579
579
} ) ;
580
+
580
581
it ( 'should delete an object correctly' , ( ) => {
581
582
const list : V1Pod [ ] = [
582
583
{
@@ -614,6 +615,7 @@ describe('ListWatchCache', () => {
614
615
} as V1Pod ) ;
615
616
expect ( list . length ) . to . equal ( 1 ) ;
616
617
} ) ;
618
+
617
619
it ( 'should not call handlers which have been unregistered' , async ( ) => {
618
620
const fakeWatch = mock . mock ( Watch ) ;
619
621
const list : V1Namespace [ ] = [ ] ;
@@ -721,6 +723,7 @@ describe('ListWatchCache', () => {
721
723
722
724
expect ( addedList . length ) . to . equal ( 1 ) ;
723
725
} ) ;
726
+
724
727
it ( 'should resolve start promise after seeding the cache' , async ( ) => {
725
728
const fakeWatch = mock . mock ( Watch ) ;
726
729
const list : V1Namespace [ ] = [
@@ -823,6 +826,127 @@ describe('ListWatchCache', () => {
823
826
expect ( addedList1 . length ) . to . equal ( 2 ) ;
824
827
expect ( addedList2 . length ) . to . equal ( 1 ) ;
825
828
} ) ;
829
+
830
+ it ( 'should not auto-restart after explicitly stopping until restarted again' , async ( ) => {
831
+ const fakeWatch = mock . mock ( Watch ) ;
832
+ const list : V1Pod [ ] = [
833
+ {
834
+ metadata : {
835
+ name : 'name1' ,
836
+ namespace : 'ns1' ,
837
+ } as V1ObjectMeta ,
838
+ } as V1Pod ,
839
+ {
840
+ metadata : {
841
+ name : 'name2' ,
842
+ namespace : 'ns2' ,
843
+ } as V1ObjectMeta ,
844
+ } as V1Pod ,
845
+ ] ;
846
+ const listObj = {
847
+ metadata : {
848
+ resourceVersion : '12345' ,
849
+ } as V1ListMeta ,
850
+ items : list ,
851
+ } as V1NamespaceList ;
852
+
853
+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
854
+ response : http . IncomingMessage ;
855
+ body : V1NamespaceList ;
856
+ } > {
857
+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > (
858
+ ( resolve , reject ) => {
859
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
860
+ } ,
861
+ ) ;
862
+ } ;
863
+ let promise = new Promise ( ( resolve ) => {
864
+ mock . when (
865
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
866
+ ) . thenCall ( ( ) => {
867
+ resolve ( new FakeRequest ( ) ) ;
868
+ } ) ;
869
+ } ) ;
870
+
871
+ const cache = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
872
+ await promise ;
873
+
874
+ const [ , , , doneHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
875
+
876
+ // stop the informer
877
+ cache . stop ( ) ;
878
+
879
+ await doneHandler ( null ) ;
880
+
881
+ mock . verify (
882
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
883
+ ) . once ( ) ;
884
+
885
+ // restart the informer
886
+ await cache . start ( ) ;
887
+
888
+ mock . verify (
889
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
890
+ ) . twice ( ) ;
891
+ } ) ;
892
+
893
+ it ( 'does not auto-restart after an error' , async ( ) => {
894
+ const fakeWatch = mock . mock ( Watch ) ;
895
+ const list : V1Pod [ ] = [
896
+ {
897
+ metadata : {
898
+ name : 'name1' ,
899
+ namespace : 'ns1' ,
900
+ } as V1ObjectMeta ,
901
+ } as V1Pod ,
902
+ {
903
+ metadata : {
904
+ name : 'name2' ,
905
+ namespace : 'ns2' ,
906
+ } as V1ObjectMeta ,
907
+ } as V1Pod ,
908
+ ] ;
909
+ const listObj = {
910
+ metadata : {
911
+ resourceVersion : '12345' ,
912
+ } as V1ListMeta ,
913
+ items : list ,
914
+ } as V1NamespaceList ;
915
+
916
+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
917
+ response : http . IncomingMessage ;
918
+ body : V1NamespaceList ;
919
+ } > {
920
+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > (
921
+ ( resolve , reject ) => {
922
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
923
+ } ,
924
+ ) ;
925
+ } ;
926
+ let promise = new Promise ( ( resolve ) => {
927
+ mock . when (
928
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
929
+ ) . thenCall ( ( ) => {
930
+ resolve ( new FakeRequest ( ) ) ;
931
+ } ) ;
932
+ } ) ;
933
+
934
+ const cache = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
935
+ await promise ;
936
+
937
+ let errorEmitted = false ;
938
+ cache . on ( ERROR , ( ) => ( errorEmitted = true ) ) ;
939
+
940
+ const [ , , , doneHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
941
+
942
+ const error = new Error ( 'testing' ) ;
943
+ await doneHandler ( error ) ;
944
+
945
+ mock . verify (
946
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
947
+ ) . once ( ) ;
948
+ expect ( errorEmitted ) . to . equal ( true ) ;
949
+ } ) ;
826
950
} ) ;
827
951
828
952
describe ( 'delete items' , ( ) => {
0 commit comments