@@ -40,11 +40,13 @@ describe('ListWatchCache', () => {
40
40
{
41
41
metadata : {
42
42
name : 'name1' ,
43
+ namespace : 'default' ,
43
44
} as V1ObjectMeta ,
44
45
} as V1Namespace ,
45
46
{
46
47
metadata : {
47
48
name : 'name2' ,
49
+ namespace : 'default' ,
48
50
} as V1ObjectMeta ,
49
51
} as V1Namespace ,
50
52
] ;
@@ -55,13 +57,32 @@ describe('ListWatchCache', () => {
55
57
items : list ,
56
58
} as V1NamespaceList ;
57
59
60
+ const emptyObj = {
61
+ metadata : {
62
+ resourceVersion : '123456' ,
63
+ } as V1ListMeta ,
64
+ items : [
65
+ {
66
+ metadata : {
67
+ name : 'name3' ,
68
+ namespace : 'default' ,
69
+ } as V1ObjectMeta ,
70
+ } as V1Namespace ,
71
+ ] ,
72
+ } as V1NamespaceList ;
73
+
74
+ var calls = 0 ;
58
75
const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
59
76
response : http . IncomingMessage ;
60
77
body : V1NamespaceList ;
61
78
} > {
62
79
return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > (
63
80
( resolve , reject ) => {
64
- resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
81
+ if ( calls ++ === 0 ) {
82
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
83
+ } else {
84
+ resolve ( { response : { } as http . IncomingMessage , body : emptyObj } ) ;
85
+ }
65
86
} ,
66
87
) ;
67
88
} ;
@@ -80,25 +101,34 @@ describe('ListWatchCache', () => {
80
101
} ) ;
81
102
const cache = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
82
103
await promise ;
83
- const [ pathOut , , watchHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
104
+ const [ pathOut , , watchHandler , doneHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
84
105
expect ( pathOut ) . to . equal ( '/some/path' ) ;
85
106
expect ( cache . list ( ) ) . to . deep . equal ( list ) ;
86
107
87
108
expect ( cache . get ( 'name1' ) ) . to . equal ( list [ 0 ] ) ;
88
109
expect ( cache . get ( 'name2' ) ) . to . equal ( list [ 1 ] ) ;
89
110
111
+ expect ( cache . list ( 'default' ) ) . to . deep . equal ( list ) ;
112
+ expect ( cache . list ( 'non-existent' ) ) . to . be . undefined ;
113
+
90
114
watchHandler ( 'ADDED' , {
91
115
metadata : {
92
116
name : 'name3' ,
117
+ namespace : 'other' ,
93
118
} as V1ObjectMeta ,
94
119
} as V1Namespace ) ;
95
120
96
121
expect ( cache . list ( ) . length ) . to . equal ( 3 ) ;
97
122
expect ( cache . get ( 'name3' ) ) . to . not . equal ( null ) ;
98
123
124
+ expect ( cache . list ( 'default' ) . length ) . to . equal ( 2 ) ;
125
+ expect ( cache . list ( 'other' ) . length ) . to . equal ( 1 ) ;
126
+ expect ( cache . list ( 'non-existent' ) ) . to . be . undefined ;
127
+
99
128
watchHandler ( 'MODIFIED' , {
100
129
metadata : {
101
130
name : 'name3' ,
131
+ namespace : 'other' ,
102
132
resourceVersion : 'baz' ,
103
133
} as V1ObjectMeta ,
104
134
} as V1Namespace ) ;
@@ -113,10 +143,26 @@ describe('ListWatchCache', () => {
113
143
watchHandler ( 'DELETED' , {
114
144
metadata : {
115
145
name : 'name2' ,
146
+ namespace : 'default' ,
116
147
} as V1ObjectMeta ,
117
148
} as V1Namespace ) ;
118
149
expect ( cache . list ( ) . length ) . to . equal ( 2 ) ;
119
150
expect ( cache . get ( 'name2' ) ) . to . equal ( undefined ) ;
151
+
152
+ expect ( cache . list ( 'default' ) . length ) . to . equal ( 1 ) ;
153
+ expect ( cache . list ( 'other' ) . length ) . to . equal ( 1 ) ;
154
+
155
+ watchHandler ( 'ADDED' , {
156
+ metadata : {
157
+ name : 'name2' ,
158
+ namespace : 'default' ,
159
+ } as V1ObjectMeta ,
160
+ } as V1Namespace ) ;
161
+
162
+ await doneHandler ( ) ;
163
+ expect ( cache . list ( ) . length , 'all namespace list' ) . to . equal ( 1 ) ;
164
+ expect ( cache . list ( 'default' ) . length , 'default namespace list' ) . to . equal ( 1 ) ;
165
+ expect ( cache . list ( 'other' ) , 'other namespace list' ) . to . be . undefined ;
120
166
} ) ;
121
167
122
168
it ( 'should perform work as an informer' , async ( ) => {
@@ -754,6 +800,76 @@ describe('ListWatchCache', () => {
754
800
await startPromise ;
755
801
expect ( cache . list ( ) . length ) . to . equal ( 2 ) ;
756
802
} ) ;
803
+
804
+ it ( 'should only call update handlers once' , async ( ) => {
805
+ const fakeWatch = mock . mock ( Watch ) ;
806
+ const list : V1Namespace [ ] = [ ] ;
807
+ const listObj = {
808
+ metadata : {
809
+ resourceVersion : '12345' ,
810
+ } as V1ListMeta ,
811
+ items : list ,
812
+ } as V1NamespaceList ;
813
+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
814
+ response : http . IncomingMessage ;
815
+ body : V1NamespaceList ;
816
+ } > {
817
+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > ( ( resolve ) => {
818
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
819
+ } ) ;
820
+ } ;
821
+ const watchCalled = new Promise ( ( resolve ) => {
822
+ mock . when (
823
+ fakeWatch . watch (
824
+ mock . anything ( ) ,
825
+ mock . anything ( ) ,
826
+ mock . anything ( ) ,
827
+ mock . anything ( ) ,
828
+ mock . anything ( ) ,
829
+ ) ,
830
+ ) . thenCall ( resolve ) ;
831
+ } ) ;
832
+ const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
833
+
834
+ const addedList1 : V1Namespace [ ] = [ ] ;
835
+ const addToList1Fn = function ( obj : V1Namespace ) {
836
+ addedList1 . push ( obj ) ;
837
+ } ;
838
+ const addedList2 : V1Namespace [ ] = [ ] ;
839
+ const addToList2Fn = function ( obj : V1Namespace ) {
840
+ addedList2 . push ( obj ) ;
841
+ } ;
842
+
843
+ informer . start ( ) ;
844
+
845
+ await watchCalled ;
846
+ const [ , , watchHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
847
+
848
+ let adds = 0 ;
849
+ informer . on ( ADD , ( ) => adds ++ ) ;
850
+ informer . on ( ADD , addToList1Fn ) ;
851
+ informer . on ( ADD , addToList2Fn ) ;
852
+
853
+ watchHandler ( 'ADDED' , {
854
+ metadata : {
855
+ name : 'name1' ,
856
+ namespace : 'something' ,
857
+ } as V1ObjectMeta ,
858
+ } as V1Namespace ) ;
859
+
860
+ informer . off ( ADD , addToList2Fn ) ;
861
+
862
+ watchHandler ( 'ADDED' , {
863
+ metadata : {
864
+ name : 'name2' ,
865
+ namespace : 'something' ,
866
+ } as V1ObjectMeta ,
867
+ } as V1Namespace ) ;
868
+
869
+ expect ( adds ) . to . equal ( 2 ) ;
870
+ expect ( addedList1 . length ) . to . equal ( 2 ) ;
871
+ expect ( addedList2 . length ) . to . equal ( 1 ) ;
872
+ } ) ;
757
873
} ) ;
758
874
759
875
describe ( 'delete items' , ( ) => {
0 commit comments