Skip to content

Commit db74ad4

Browse files
committed
Fix things, add more tests.
1 parent 2cd82e3 commit db74ad4

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

src/cache_test.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,94 @@ describe('ListWatchCache', () => {
305305
resolve();
306306
});
307307
});
308-
console.log('calling done again.');
309308
doneHandler(null);
310309
await promise;
311310
expect(addObjects).to.deep.equal(list);
312311
expect(updateObjects).to.deep.equal(list);
313312
});
314313

314+
it('should perform work as an informer with initial list and delete after', async () => {
315+
const fakeWatch = mock.mock(Watch);
316+
const list: V1Namespace[] = [
317+
{
318+
metadata: {
319+
name: 'name1',
320+
} as V1ObjectMeta,
321+
} as V1Namespace,
322+
{
323+
metadata: {
324+
name: 'name2',
325+
} as V1ObjectMeta,
326+
} as V1Namespace,
327+
];
328+
const list2: V1Namespace[] = [
329+
{
330+
metadata: {
331+
name: 'name1',
332+
} as V1ObjectMeta,
333+
} as V1Namespace,
334+
];
335+
const listObj = {
336+
metadata: {
337+
resourceVersion: '12345',
338+
} as V1ListMeta,
339+
items: list,
340+
} as V1NamespaceList;
341+
342+
const listFn: ListPromise<V1Namespace> = function(): Promise<{
343+
response: http.IncomingMessage;
344+
body: V1NamespaceList;
345+
}> {
346+
return new Promise<{ response: http.IncomingMessage; body: V1NamespaceList }>(
347+
(resolve, reject) => {
348+
resolve({ response: {} as http.IncomingMessage, body: listObj });
349+
},
350+
);
351+
};
352+
let promise = new Promise((resolve) => {
353+
mock.when(
354+
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
355+
).thenCall(() => {
356+
resolve();
357+
});
358+
});
359+
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn, false);
360+
361+
const addObjects: V1Namespace[] = [];
362+
informer.on(ADD, (obj: V1Namespace) => addObjects.push(obj));
363+
const updateObjects: V1Namespace[] = [];
364+
informer.on(UPDATE, (obj: V1Namespace) => updateObjects.push(obj));
365+
const deleteObjects: V1Namespace[] = [];
366+
informer.on(DELETE, (obj: V1Namespace) => deleteObjects.push(obj));
367+
informer.start();
368+
await promise;
369+
const [pathOut, , , doneHandler] = mock.capture(fakeWatch.watch).last();
370+
371+
expect(pathOut).to.equal('/some/path');
372+
expect(addObjects).to.deep.equal(list);
373+
expect(updateObjects).to.deep.equal([]);
374+
375+
promise = new Promise((resolve) => {
376+
mock.when(
377+
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
378+
).thenCall(() => {
379+
resolve();
380+
});
381+
});
382+
listObj.items = list2;
383+
doneHandler(null);
384+
await promise;
385+
expect(addObjects).to.deep.equal(list);
386+
expect(updateObjects).to.deep.equal(list2);
387+
expect(deleteObjects).to.deep.equal([
388+
{
389+
metadata: {
390+
name: 'name2',
391+
} as V1ObjectMeta,
392+
} as V1Namespace,
393+
]);
394+
});
395+
315396
it('should perform namespace caching', async () => {
316397
const fakeWatch = mock.mock(Watch);
317398
const list: V1Pod[] = [

0 commit comments

Comments
 (0)