Skip to content

Commit 54bb0b9

Browse files
committed
Test if watch callback errors are not ignored
1 parent a9d16b3 commit 54bb0b9

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/watch_test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,67 @@ describe('Watch', () => {
364364
expect(receivedObjects).to.deep.equal([obj.object]);
365365
});
366366

367+
it('should not ignore handler errors', async () => {
368+
const kc = new KubeConfig();
369+
Object.assign(kc, fakeConfig);
370+
const fakeRequestor = mock(DefaultRequest);
371+
const watch = new Watch(kc, instance(fakeRequestor));
372+
373+
const obj = {
374+
type: 'MODIFIED',
375+
object: {
376+
baz: 'blah',
377+
},
378+
};
379+
380+
const fakeRequest = new FakeRequest();
381+
fakeRequest.pipe = function(stream) {
382+
stream.write(JSON.stringify(obj) + '\n');
383+
stream.emit('close');
384+
};
385+
386+
when(fakeRequestor.webRequest(anything())).thenReturn(fakeRequest);
387+
388+
const path = '/some/path/to/object';
389+
390+
const receivedTypes: string[] = [];
391+
const receivedObjects: string[] = [];
392+
let doneCalled = false;
393+
let doneErr: any;
394+
let catchCalled = false;
395+
let catchErr: any;
396+
397+
const handlerError = new Error('handler error');
398+
399+
await watch.watch(
400+
path,
401+
{},
402+
(recievedType: string, recievedObject: string) => {
403+
receivedTypes.push(recievedType);
404+
receivedObjects.push(recievedObject);
405+
throw handlerError;
406+
},
407+
(err: any) => {
408+
doneCalled = true;
409+
doneErr = err;
410+
},
411+
).catch((err) => {
412+
catchCalled = true;
413+
catchErr = err;
414+
});
415+
416+
verify(fakeRequestor.webRequest(anything()));
417+
418+
expect(receivedTypes).to.deep.equal([obj.type]);
419+
expect(receivedObjects).to.deep.equal([obj.object]);
420+
421+
expect(doneCalled).to.equal(false);
422+
expect(doneErr).to.be.undefined;
423+
424+
expect(catchCalled).to.equal(true);
425+
expect(catchErr).to.equal(handlerError);
426+
});
427+
367428
it('should throw on empty config', () => {
368429
const kc = new KubeConfig();
369430
const watch = new Watch(kc);

0 commit comments

Comments
 (0)