@@ -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 , ERROR , ListPromise , CHANGE } from './informer' ;
12
+ import { ListPromise } from './informer' ;
13
13
14
14
use ( chaiAsPromised ) ;
15
15
@@ -44,7 +44,9 @@ describe('ListWatchCache', () => {
44
44
} ;
45
45
const lw = new ListWatch ( '/some/path' , fake , listFn ) ;
46
46
const verb = 'FOOBAR' ;
47
- expect ( ( ) => lw . on ( verb , ( obj : V1Namespace ) => { } ) ) . to . throw ( `Unknown verb: ${ verb } ` ) ;
47
+ // The 'as any' is a hack to get around Typescript which prevents an unknown verb from being
48
+ // passed. We want to test for Javascript clients also, where this is possible
49
+ expect ( ( ) => ( lw as any ) . on ( verb , ( obj ?: V1Namespace ) => { } ) ) . to . throw ( `Unknown verb: ${ verb } ` ) ;
48
50
} ) ;
49
51
50
52
it ( 'should perform basic caching' , async ( ) => {
@@ -216,19 +218,19 @@ describe('ListWatchCache', () => {
216
218
expect ( pathOut ) . to . equal ( '/some/path' ) ;
217
219
218
220
const addPromise = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
219
- informer . on ( ADD , ( obj : V1Namespace ) => {
221
+ informer . on ( 'add' , ( obj ? : V1Namespace ) => {
220
222
resolve ( obj ) ;
221
223
} ) ;
222
224
} ) ;
223
225
224
226
const updatePromise = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
225
- informer . on ( UPDATE , ( obj : V1Namespace ) => {
227
+ informer . on ( 'update' , ( obj ? : V1Namespace ) => {
226
228
resolve ( obj ) ;
227
229
} ) ;
228
230
} ) ;
229
231
230
232
const deletePromise = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
231
- informer . on ( DELETE , ( obj : V1Namespace ) => {
233
+ informer . on ( 'delete' , ( obj ? : V1Namespace ) => {
232
234
resolve ( obj ) ;
233
235
} ) ;
234
236
} ) ;
@@ -310,7 +312,7 @@ describe('ListWatchCache', () => {
310
312
311
313
let count = 0 ;
312
314
const changePromise = new Promise < boolean > ( ( resolve : ( V1Namespace ) => void ) => {
313
- informer . on ( CHANGE , ( obj : V1Namespace ) => {
315
+ informer . on ( 'change' , ( obj ? : V1Namespace ) => {
314
316
count ++ ;
315
317
if ( count == 3 ) {
316
318
resolve ( true ) ;
@@ -372,13 +374,13 @@ describe('ListWatchCache', () => {
372
374
expect ( pathOut ) . to . equal ( '/some/path' ) ;
373
375
374
376
const addPromise = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
375
- informer . on ( ADD , ( obj : V1Namespace ) => {
377
+ informer . on ( 'add' , ( obj ? : V1Namespace ) => {
376
378
resolve ( obj ) ;
377
379
} ) ;
378
380
} ) ;
379
381
380
382
const addPromise2 = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
381
- informer . on ( ADD , ( obj : V1Namespace ) => {
383
+ informer . on ( 'add' , ( obj ? : V1Namespace ) => {
382
384
resolve ( obj ) ;
383
385
} ) ;
384
386
} ) ;
@@ -442,9 +444,9 @@ describe('ListWatchCache', () => {
442
444
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn , false ) ;
443
445
444
446
const addObjects : V1Namespace [ ] = [ ] ;
445
- informer . on ( ADD , ( obj : V1Namespace ) => addObjects . push ( obj ) ) ;
447
+ informer . on ( 'add' , ( obj ? : V1Namespace ) => addObjects . push ( obj ! ) ) ;
446
448
const updateObjects : V1Namespace [ ] = [ ] ;
447
- informer . on ( UPDATE , ( obj : V1Namespace ) => updateObjects . push ( obj ) ) ;
449
+ informer . on ( 'update' , ( obj ? : V1Namespace ) => updateObjects . push ( obj ! ) ) ;
448
450
449
451
informer . start ( ) ;
450
452
await promise ;
@@ -518,11 +520,11 @@ describe('ListWatchCache', () => {
518
520
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn , false ) ;
519
521
520
522
const addObjects : V1Namespace [ ] = [ ] ;
521
- informer . on ( ADD , ( obj : V1Namespace ) => addObjects . push ( obj ) ) ;
523
+ informer . on ( 'add' , ( obj ? : V1Namespace ) => addObjects . push ( obj ! ) ) ;
522
524
const updateObjects : V1Namespace [ ] = [ ] ;
523
- informer . on ( UPDATE , ( obj : V1Namespace ) => updateObjects . push ( obj ) ) ;
525
+ informer . on ( 'update' , ( obj ? : V1Namespace ) => updateObjects . push ( obj ! ) ) ;
524
526
const deleteObjects : V1Namespace [ ] = [ ] ;
525
- informer . on ( DELETE , ( obj : V1Namespace ) => deleteObjects . push ( obj ) ) ;
527
+ informer . on ( 'delete' , ( obj ? : V1Namespace ) => deleteObjects . push ( obj ! ) ) ;
526
528
informer . start ( ) ;
527
529
await promise ;
528
530
const [ pathOut , , , doneHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
@@ -716,29 +718,29 @@ describe('ListWatchCache', () => {
716
718
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
717
719
718
720
const addedList1 : V1Namespace [ ] = [ ] ;
719
- const addToList1Fn = function ( obj : V1Namespace ) {
720
- addedList1 . push ( obj ) ;
721
+ const addToList1Fn = function ( obj ? : V1Namespace ) {
722
+ addedList1 . push ( obj ! ) ;
721
723
} ;
722
724
const addedList2 : V1Namespace [ ] = [ ] ;
723
- const addToList2Fn = function ( obj : V1Namespace ) {
724
- addedList2 . push ( obj ) ;
725
+ const addToList2Fn = function ( obj ? : V1Namespace ) {
726
+ addedList2 . push ( obj ! ) ;
725
727
} ;
726
728
727
729
informer . start ( ) ;
728
730
729
731
await watchCalled ;
730
732
const [ , , watchHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
731
733
732
- informer . on ( ADD , addToList1Fn ) ;
733
- informer . on ( ADD , addToList2Fn ) ;
734
+ informer . on ( 'add' , addToList1Fn ) ;
735
+ informer . on ( 'add' , addToList2Fn ) ;
734
736
735
737
watchHandler ( 'ADDED' , {
736
738
metadata : {
737
739
name : 'name1' ,
738
740
} as V1ObjectMeta ,
739
741
} as V1Namespace ) ;
740
742
741
- informer . off ( ADD , addToList2Fn ) ;
743
+ informer . off ( 'add' , addToList2Fn ) ;
742
744
743
745
watchHandler ( 'ADDED' , {
744
746
metadata : {
@@ -775,20 +777,20 @@ describe('ListWatchCache', () => {
775
777
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
776
778
777
779
const addedList : V1Namespace [ ] = [ ] ;
778
- const addToListFn = function ( obj : V1Namespace ) {
779
- addedList . push ( obj ) ;
780
+ const addToListFn = function ( obj ? : V1Namespace ) {
781
+ addedList . push ( obj ! ) ;
780
782
} ;
781
783
const removeSelf = function ( ) {
782
- informer . off ( ADD , removeSelf ) ;
784
+ informer . off ( 'add' , removeSelf ) ;
783
785
} ;
784
786
785
787
informer . start ( ) ;
786
788
787
789
await watchCalled ;
788
790
const [ , , watchHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
789
791
790
- informer . on ( ADD , removeSelf ) ;
791
- informer . on ( ADD , addToListFn ) ;
792
+ informer . on ( 'add' , removeSelf ) ;
793
+ informer . on ( 'add' , addToListFn ) ;
792
794
793
795
watchHandler ( 'ADDED' , {
794
796
metadata : {
@@ -863,12 +865,12 @@ describe('ListWatchCache', () => {
863
865
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
864
866
865
867
const addedList1 : V1Namespace [ ] = [ ] ;
866
- const addToList1Fn = function ( obj : V1Namespace ) {
867
- addedList1 . push ( obj ) ;
868
+ const addToList1Fn = function ( obj ? : V1Namespace ) {
869
+ addedList1 . push ( obj ! ) ;
868
870
} ;
869
871
const addedList2 : V1Namespace [ ] = [ ] ;
870
- const addToList2Fn = function ( obj : V1Namespace ) {
871
- addedList2 . push ( obj ) ;
872
+ const addToList2Fn = function ( obj ? : V1Namespace ) {
873
+ addedList2 . push ( obj ! ) ;
872
874
} ;
873
875
874
876
informer . start ( ) ;
@@ -877,9 +879,9 @@ describe('ListWatchCache', () => {
877
879
const [ , , watchHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
878
880
879
881
let adds = 0 ;
880
- informer . on ( ADD , ( ) => adds ++ ) ;
881
- informer . on ( ADD , addToList1Fn ) ;
882
- informer . on ( ADD , addToList2Fn ) ;
882
+ informer . on ( 'add' , ( ) => adds ++ ) ;
883
+ informer . on ( 'add' , addToList1Fn ) ;
884
+ informer . on ( 'add' , addToList2Fn ) ;
883
885
884
886
watchHandler ( 'ADDED' , {
885
887
metadata : {
@@ -888,7 +890,7 @@ describe('ListWatchCache', () => {
888
890
} as V1ObjectMeta ,
889
891
} as V1Namespace ) ;
890
892
891
- informer . off ( ADD , addToList2Fn ) ;
893
+ informer . off ( 'add' , addToList2Fn ) ;
892
894
893
895
watchHandler ( 'ADDED' , {
894
896
metadata : {
@@ -1010,7 +1012,7 @@ describe('ListWatchCache', () => {
1010
1012
await promise ;
1011
1013
1012
1014
let errorEmitted = false ;
1013
- cache . on ( ERROR , ( ) => ( errorEmitted = true ) ) ;
1015
+ cache . on ( 'error' , ( ) => ( errorEmitted = true ) ) ;
1014
1016
1015
1017
const [ , , , doneHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
1016
1018
@@ -1106,7 +1108,104 @@ describe('delete items', () => {
1106
1108
] ;
1107
1109
const pods : V1Pod [ ] = [ ] ;
1108
1110
1109
- deleteItems ( listA , listB , [ ( obj : V1Pod ) => pods . push ( obj ) ] ) ;
1111
+ deleteItems ( listA , listB , [ ( obj ? : V1Pod ) => pods . push ( obj ! ) ] ) ;
1110
1112
expect ( pods ) . to . deep . equal ( expected ) ;
1111
1113
} ) ;
1114
+
1115
+ it ( 'should call the connect handler' , async ( ) => {
1116
+ const fakeWatch = mock . mock ( Watch ) ;
1117
+ const listObj = {
1118
+ metadata : {
1119
+ resourceVersion : '12345' ,
1120
+ } as V1ListMeta ,
1121
+ items : [ ] ,
1122
+ } as V1NamespaceList ;
1123
+
1124
+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
1125
+ response : http . IncomingMessage ;
1126
+ body : V1NamespaceList ;
1127
+ } > {
1128
+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > (
1129
+ ( resolve , reject ) => {
1130
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
1131
+ } ,
1132
+ ) ;
1133
+ } ;
1134
+ const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn , false ) ;
1135
+ const connectPromise = new Promise < boolean > ( ( resolve : ( boolean ) => void ) => {
1136
+ informer . on ( 'connect' , ( obj ?: V1Namespace ) => {
1137
+ resolve ( true ) ;
1138
+ } ) ;
1139
+ } ) ;
1140
+ informer . start ( ) ;
1141
+
1142
+ expect ( connectPromise ) . to . eventually . be . true ;
1143
+ } ) ;
1144
+
1145
+ it ( 'does calls connect after a restart after an error' , async ( ) => {
1146
+ const fakeWatch = mock . mock ( Watch ) ;
1147
+ const list : V1Pod [ ] = [
1148
+ {
1149
+ metadata : {
1150
+ name : 'name1' ,
1151
+ namespace : 'ns1' ,
1152
+ } as V1ObjectMeta ,
1153
+ } as V1Pod ,
1154
+ {
1155
+ metadata : {
1156
+ name : 'name2' ,
1157
+ namespace : 'ns2' ,
1158
+ } as V1ObjectMeta ,
1159
+ } as V1Pod ,
1160
+ ] ;
1161
+ const listObj = {
1162
+ metadata : {
1163
+ resourceVersion : '12345' ,
1164
+ } as V1ListMeta ,
1165
+ items : list ,
1166
+ } as V1NamespaceList ;
1167
+
1168
+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
1169
+ response : http . IncomingMessage ;
1170
+ body : V1NamespaceList ;
1171
+ } > {
1172
+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > (
1173
+ ( resolve , reject ) => {
1174
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
1175
+ } ,
1176
+ ) ;
1177
+ } ;
1178
+ let promise = new Promise ( ( resolve ) => {
1179
+ mock . when (
1180
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1181
+ ) . thenCall ( ( ) => {
1182
+ resolve ( new FakeRequest ( ) ) ;
1183
+ } ) ;
1184
+ } ) ;
1185
+
1186
+ const cache = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
1187
+ await promise ;
1188
+
1189
+ let errorEmitted = false ;
1190
+ cache . on ( 'error' , ( ) => ( errorEmitted = true ) ) ;
1191
+
1192
+ const [ , , , doneHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
1193
+
1194
+ const error = new Error ( 'testing' ) ;
1195
+ await doneHandler ( error ) ;
1196
+
1197
+ mock . verify (
1198
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1199
+ ) . once ( ) ;
1200
+ expect ( errorEmitted ) . to . equal ( true ) ;
1201
+
1202
+ const connectPromise = new Promise < boolean > ( ( resolve : ( boolean ) => void ) => {
1203
+ cache . on ( 'connect' , ( obj ?: V1Namespace ) => {
1204
+ resolve ( true ) ;
1205
+ } ) ;
1206
+ } ) ;
1207
+ cache . start ( ) ;
1208
+
1209
+ expect ( connectPromise ) . to . eventually . be . true ;
1210
+ } ) ;
1112
1211
} ) ;
0 commit comments