@@ -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 } from './informer' ;
12
+ import { ADD , UPDATE , DELETE , ERROR , ListPromise , CHANGE } from './informer' ;
13
13
14
14
use ( chaiAsPromised ) ;
15
15
@@ -265,6 +265,81 @@ describe('ListWatchCache', () => {
265
265
] ) ;
266
266
} ) ;
267
267
268
+ it ( 'should handle change events correctly' , async ( ) => {
269
+ const fakeWatch = mock . mock ( Watch ) ;
270
+ const list : V1Namespace [ ] = [
271
+ {
272
+ metadata : {
273
+ name : 'name1' ,
274
+ } as V1ObjectMeta ,
275
+ } as V1Namespace ,
276
+ {
277
+ metadata : {
278
+ name : 'name2' ,
279
+ } as V1ObjectMeta ,
280
+ } as V1Namespace ,
281
+ ] ;
282
+ const listObj = {
283
+ metadata : {
284
+ resourceVersion : '12345' ,
285
+ } as V1ListMeta ,
286
+ items : list ,
287
+ } as V1NamespaceList ;
288
+
289
+ const listFn : ListPromise < V1Namespace > = function ( ) : Promise < {
290
+ response : http . IncomingMessage ;
291
+ body : V1NamespaceList ;
292
+ } > {
293
+ return new Promise < { response : http . IncomingMessage ; body : V1NamespaceList } > (
294
+ ( resolve , reject ) => {
295
+ resolve ( { response : { } as http . IncomingMessage , body : listObj } ) ;
296
+ } ,
297
+ ) ;
298
+ } ;
299
+ const promise = new Promise ( ( resolve ) => {
300
+ mock . when (
301
+ fakeWatch . watch ( mock . anything ( ) , mock . anything ( ) , mock . anything ( ) , mock . anything ( ) ) ,
302
+ ) . thenCall ( ( ) => {
303
+ resolve ( new FakeRequest ( ) ) ;
304
+ } ) ;
305
+ } ) ;
306
+ const informer = new ListWatch ( '/some/path' , mock . instance ( fakeWatch ) , listFn ) ;
307
+ await promise ;
308
+ const [ pathOut , , watchHandler ] = mock . capture ( fakeWatch . watch ) . last ( ) ;
309
+ expect ( pathOut ) . to . equal ( '/some/path' ) ;
310
+
311
+ let count = 0 ;
312
+ const changePromise = new Promise < boolean > ( ( resolve : ( V1Namespace ) => void ) => {
313
+ informer . on ( CHANGE , ( obj : V1Namespace ) => {
314
+ count ++ ;
315
+ if ( count == 3 ) {
316
+ resolve ( true ) ;
317
+ }
318
+ } ) ;
319
+ } ) ;
320
+
321
+ watchHandler ( 'ADDED' , {
322
+ metadata : {
323
+ name : 'name3' ,
324
+ } as V1ObjectMeta ,
325
+ } as V1Namespace ) ;
326
+
327
+ watchHandler ( 'MODIFIED' , {
328
+ metadata : {
329
+ name : 'name3' ,
330
+ resourceVersion : 'baz' ,
331
+ } as V1ObjectMeta ,
332
+ } as V1Namespace ) ;
333
+
334
+ watchHandler ( 'DELETED' , {
335
+ metadata : {
336
+ name : 'name2' ,
337
+ } as V1ObjectMeta ,
338
+ } as V1Namespace ) ;
339
+
340
+ expect ( changePromise ) . to . eventually . be . true ;
341
+ } ) ;
342
+
268
343
it ( 'should perform work as an informer with multiple handlers' , async ( ) => {
269
344
const fakeWatch = mock . mock ( Watch ) ;
270
345
const listObj = {
0 commit comments