Skip to content

Commit ad56a41

Browse files
authored
Merge pull request kubernetes-client#324 from benbuzbee/benbuz/async
Make Informer and ListWatch start method async which resolves after the first list
2 parents 0053514 + 3643688 commit ad56a41

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
2828
}
2929
}
3030

31-
public start() {
32-
this.doneHandler(null);
31+
public async start(): Promise<void> {
32+
await this.doneHandler(null);
3333
}
3434

3535
public on(verb: string, cb: ObjectCallback<T>) {

src/cache_test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,44 @@ describe('ListWatchCache', () => {
656656

657657
expect(addedList.length).to.equal(1);
658658
});
659+
it('should resolve start promise after seeding the cache', async () => {
660+
const fakeWatch = mock.mock(Watch);
661+
const list: V1Namespace[] = [
662+
{
663+
metadata: {
664+
name: 'name1',
665+
} as V1ObjectMeta,
666+
} as V1Namespace,
667+
{
668+
metadata: {
669+
name: 'name2',
670+
} as V1ObjectMeta,
671+
} as V1Namespace,
672+
];
673+
const listObj = {
674+
metadata: {
675+
resourceVersion: '12345',
676+
} as V1ListMeta,
677+
items: list,
678+
} as V1NamespaceList;
679+
680+
const listFn: ListPromise<V1Namespace> = function(): Promise<{
681+
response: http.IncomingMessage;
682+
body: V1NamespaceList;
683+
}> {
684+
return new Promise<{ response: http.IncomingMessage; body: V1NamespaceList }>((resolve) => {
685+
// setImmediate will defer the resolve to the next message loop to keep the list from being immediately available
686+
setImmediate(() => {
687+
resolve({ response: {} as http.IncomingMessage, body: listObj });
688+
});
689+
});
690+
};
691+
const cache = new ListWatch('/some/path', mock.instance(fakeWatch), listFn, false);
692+
const startPromise: Promise<void> = cache.start();
693+
expect(cache.list().length).to.equal(0);
694+
await startPromise;
695+
expect(cache.list().length).to.equal(2);
696+
});
659697
});
660698

661699
describe('delete items', () => {

src/informer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const DELETE: string = 'delete';
1919
export interface Informer<T> {
2020
on(verb: string, fn: ObjectCallback<T>);
2121
off(verb: string, fn: ObjectCallback<T>);
22-
start();
22+
start(): Promise<void>;
2323
}
2424

2525
export function makeInformer<T>(

0 commit comments

Comments
 (0)