@@ -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 { ADD , UPDATE , DELETE , ERROR , ListPromise , CHANGE , CONNECT } from './informer' ;
13
13
14
14
use ( chaiAsPromised ) ;
15
15
@@ -44,7 +44,7 @@ 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
+ expect ( ( ) => lw . on ( verb , ( obj ? : V1Namespace ) => { } ) ) . to . throw ( `Unknown verb: ${ verb } ` ) ;
48
48
} ) ;
49
49
50
50
it ( 'should perform basic caching' , async ( ) => {
@@ -216,19 +216,19 @@ describe('ListWatchCache', () => {
216
216
expect ( pathOut ) . to . equal ( '/some/path' ) ;
217
217
218
218
const addPromise = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
219
- informer . on ( ADD , ( obj : V1Namespace ) => {
219
+ informer . on ( ADD , ( obj ? : V1Namespace ) => {
220
220
resolve ( obj ) ;
221
221
} ) ;
222
222
} ) ;
223
223
224
224
const updatePromise = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
225
- informer . on ( UPDATE , ( obj : V1Namespace ) => {
225
+ informer . on ( UPDATE , ( obj ? : V1Namespace ) => {
226
226
resolve ( obj ) ;
227
227
} ) ;
228
228
} ) ;
229
229
230
230
const deletePromise = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
231
- informer . on ( DELETE , ( obj : V1Namespace ) => {
231
+ informer . on ( DELETE , ( obj ? : V1Namespace ) => {
232
232
resolve ( obj ) ;
233
233
} ) ;
234
234
} ) ;
@@ -310,7 +310,7 @@ describe('ListWatchCache', () => {
310
310
311
311
let count = 0 ;
312
312
const changePromise = new Promise < boolean > ( ( resolve : ( V1Namespace ) => void ) => {
313
- informer . on ( CHANGE , ( obj : V1Namespace ) => {
313
+ informer . on ( CHANGE , ( obj ? : V1Namespace ) => {
314
314
count ++ ;
315
315
if ( count == 3 ) {
316
316
resolve ( true ) ;
@@ -372,13 +372,13 @@ describe('ListWatchCache', () => {
372
372
expect ( pathOut ) . to . equal ( '/some/path' ) ;
373
373
374
374
const addPromise = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
375
- informer . on ( ADD , ( obj : V1Namespace ) => {
375
+ informer . on ( ADD , ( obj ? : V1Namespace ) => {
376
376
resolve ( obj ) ;
377
377
} ) ;
378
378
} ) ;
379
379
380
380
const addPromise2 = new Promise < V1Namespace > ( ( resolve : ( V1Namespace ) => void ) => {
381
- informer . on ( ADD , ( obj : V1Namespace ) => {
381
+ informer . on ( ADD , ( obj ? : V1Namespace ) => {
382
382
resolve ( obj ) ;
383
383
} ) ;
384
384
} ) ;
@@ -442,9 +442,9 @@ describe('ListWatchCache', () => {
442
442
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn , false ) ;
443
443
444
444
const addObjects : V1Namespace [ ] = [ ] ;
445
- informer . on ( ADD , ( obj : V1Namespace ) => addObjects . push ( obj ) ) ;
445
+ informer . on ( ADD , ( obj ? : V1Namespace ) => addObjects . push ( obj ! ) ) ;
446
446
const updateObjects : V1Namespace [ ] = [ ] ;
447
- informer . on ( UPDATE , ( obj : V1Namespace ) => updateObjects . push ( obj ) ) ;
447
+ informer . on ( UPDATE , ( obj ? : V1Namespace ) => updateObjects . push ( obj ! ) ) ;
448
448
449
449
informer . start ( ) ;
450
450
await promise ;
@@ -518,11 +518,11 @@ describe('ListWatchCache', () => {
518
518
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn , false ) ;
519
519
520
520
const addObjects : V1Namespace [ ] = [ ] ;
521
- informer . on ( ADD , ( obj : V1Namespace ) => addObjects . push ( obj ) ) ;
521
+ informer . on ( ADD , ( obj ? : V1Namespace ) => addObjects . push ( obj ! ) ) ;
522
522
const updateObjects : V1Namespace [ ] = [ ] ;
523
- informer . on ( UPDATE , ( obj : V1Namespace ) => updateObjects . push ( obj ) ) ;
523
+ informer . on ( UPDATE , ( obj ? : V1Namespace ) => updateObjects . push ( obj ! ) ) ;
524
524
const deleteObjects : V1Namespace [ ] = [ ] ;
525
- informer . on ( DELETE , ( obj : V1Namespace ) => deleteObjects . push ( obj ) ) ;
525
+ informer . on ( DELETE , ( obj ? : V1Namespace ) => deleteObjects . push ( obj ! ) ) ;
526
526
informer . start ( ) ;
527
527
await promise ;
528
528
const [ pathOut , , , doneHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
@@ -716,12 +716,12 @@ describe('ListWatchCache', () => {
716
716
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
717
717
718
718
const addedList1 : V1Namespace [ ] = [ ] ;
719
- const addToList1Fn = function ( obj : V1Namespace ) {
720
- addedList1 . push ( obj ) ;
719
+ const addToList1Fn = function ( obj ? : V1Namespace ) {
720
+ addedList1 . push ( obj ! ) ;
721
721
} ;
722
722
const addedList2 : V1Namespace [ ] = [ ] ;
723
- const addToList2Fn = function ( obj : V1Namespace ) {
724
- addedList2 . push ( obj ) ;
723
+ const addToList2Fn = function ( obj ? : V1Namespace ) {
724
+ addedList2 . push ( obj ! ) ;
725
725
} ;
726
726
727
727
informer . start ( ) ;
@@ -775,8 +775,8 @@ describe('ListWatchCache', () => {
775
775
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
776
776
777
777
const addedList : V1Namespace [ ] = [ ] ;
778
- const addToListFn = function ( obj : V1Namespace ) {
779
- addedList . push ( obj ) ;
778
+ const addToListFn = function ( obj ? : V1Namespace ) {
779
+ addedList . push ( obj ! ) ;
780
780
} ;
781
781
const removeSelf = function ( ) {
782
782
informer . off ( ADD , removeSelf ) ;
@@ -863,12 +863,12 @@ describe('ListWatchCache', () => {
863
863
const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
864
864
865
865
const addedList1 : V1Namespace [ ] = [ ] ;
866
- const addToList1Fn = function ( obj : V1Namespace ) {
867
- addedList1 . push ( obj ) ;
866
+ const addToList1Fn = function ( obj ? : V1Namespace ) {
867
+ addedList1 . push ( obj ! ) ;
868
868
} ;
869
869
const addedList2 : V1Namespace [ ] = [ ] ;
870
- const addToList2Fn = function ( obj : V1Namespace ) {
871
- addedList2 . push ( obj ) ;
870
+ const addToList2Fn = function ( obj ? : V1Namespace ) {
871
+ addedList2 . push ( obj ! ) ;
872
872
} ;
873
873
874
874
informer . start ( ) ;
@@ -1106,7 +1106,104 @@ describe('delete items', () => {
1106
1106
] ;
1107
1107
const pods : V1Pod [ ] = [ ] ;
1108
1108
1109
- deleteItems ( listA , listB , [ ( obj : V1Pod ) => pods . push ( obj ) ] ) ;
1109
+ deleteItems ( listA , listB , [ ( obj ? : V1Pod ) => pods . push ( obj ! ) ] ) ;
1110
1110
expect ( pods ) . to . deep . equal ( expected ) ;
1111
1111
} ) ;
1112
+
1113
+ it ( 'should call the connect handler' , async ( ) => {
1114
+ const fakeWatch = mock . mock ( Watch ) ;
1115
+ const listObj = {
1116
+ metadata : {
1117
+ resourceVersion : '12345' ,
1118
+ } as V1ListMeta ,
1119
+ items : [ ] ,
1120
+ } as V1NamespaceList ;
1121
+
1122
+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
1123
+ response : http . IncomingMessage ;
1124
+ body : V1NamespaceList ;
1125
+ } > {
1126
+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > (
1127
+ ( resolve , reject ) => {
1128
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
1129
+ } ,
1130
+ ) ;
1131
+ } ;
1132
+ const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn , false ) ;
1133
+ const connectPromise = new Promise < boolean > ( ( resolve : ( boolean ) => void ) => {
1134
+ informer . on ( CONNECT , ( obj ?: V1Namespace ) => {
1135
+ resolve ( true ) ;
1136
+ } ) ;
1137
+ } ) ;
1138
+ informer . start ( ) ;
1139
+
1140
+ expect ( connectPromise ) . to . eventually . be . true ;
1141
+ } ) ;
1142
+
1143
+ it ( 'does calls connect after a restart after an error' , async ( ) => {
1144
+ const fakeWatch = mock . mock ( Watch ) ;
1145
+ const list : V1Pod [ ] = [
1146
+ {
1147
+ metadata : {
1148
+ name : 'name1' ,
1149
+ namespace : 'ns1' ,
1150
+ } as V1ObjectMeta ,
1151
+ } as V1Pod ,
1152
+ {
1153
+ metadata : {
1154
+ name : 'name2' ,
1155
+ namespace : 'ns2' ,
1156
+ } as V1ObjectMeta ,
1157
+ } as V1Pod ,
1158
+ ] ;
1159
+ const listObj = {
1160
+ metadata : {
1161
+ resourceVersion : '12345' ,
1162
+ } as V1ListMeta ,
1163
+ items : list ,
1164
+ } as V1NamespaceList ;
1165
+
1166
+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
1167
+ response : http . IncomingMessage ;
1168
+ body : V1NamespaceList ;
1169
+ } > {
1170
+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > (
1171
+ ( resolve , reject ) => {
1172
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
1173
+ } ,
1174
+ ) ;
1175
+ } ;
1176
+ let promise = new Promise ( ( resolve ) => {
1177
+ mock . when (
1178
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1179
+ ) . thenCall ( ( ) => {
1180
+ resolve ( new FakeRequest ( ) ) ;
1181
+ } ) ;
1182
+ } ) ;
1183
+
1184
+ const cache = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
1185
+ await promise ;
1186
+
1187
+ let errorEmitted = false ;
1188
+ cache . on ( ERROR , ( ) => ( errorEmitted = true ) ) ;
1189
+
1190
+ const [ , , , doneHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
1191
+
1192
+ const error = new Error ( 'testing' ) ;
1193
+ await doneHandler ( error ) ;
1194
+
1195
+ mock . verify (
1196
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
1197
+ ) . once ( ) ;
1198
+ expect ( errorEmitted ) . to . equal ( true ) ;
1199
+
1200
+ const connectPromise = new Promise < boolean > ( ( resolve : ( boolean ) => void ) => {
1201
+ cache . on ( CONNECT , ( obj ?: V1Namespace ) => {
1202
+ resolve ( true ) ;
1203
+ } ) ;
1204
+ } ) ;
1205
+ cache . start ( ) ;
1206
+
1207
+ expect ( connectPromise ) . to . eventually . be . true ;
1208
+ } ) ;
1112
1209
} ) ;
0 commit comments