Skip to content

Commit

Permalink
Merge pull request #396 from brendandburns/validate
Browse files Browse the repository at this point in the history
Fix watch to emit an error twice on error. Add tests.
  • Loading branch information
k8s-ci-robot authored Jan 24, 2020
2 parents fbd3c1f + e4ac479 commit 568a853
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ export class Watch {
// ignore parse errors
}
});
let errOut: Error | null = null;
stream.on('error', (err) => {
errOut = err;
done(err);
});
stream.on('close', () => done(null));
stream.on('close', () => done(errOut));

const req = this.requestImpl.webRequest(requestOptions, (error, response, body) => {
if (error) {
Expand Down
9 changes: 6 additions & 3 deletions src/watch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe('Watch', () => {
pipe: (stream) => {
stream.write(JSON.stringify(obj1) + '\n');
stream.emit('error', errIn);
stream.emit('close');
},
};

Expand All @@ -140,7 +141,7 @@ describe('Watch', () => {
const receivedTypes: string[] = [];
const receivedObjects: string[] = [];
let doneCalled = false;
let doneErr: any;
let doneErr: any[] = [];

await watch.watch(
path,
Expand All @@ -151,7 +152,7 @@ describe('Watch', () => {
},
(err: any) => {
doneCalled = true;
doneErr = err;
doneErr.push(err);
},
);

Expand All @@ -168,7 +169,9 @@ describe('Watch', () => {
expect(receivedObjects).to.deep.equal([obj1.object]);

expect(doneCalled).to.equal(true);
expect(doneErr).to.deep.equal(errIn);
expect(doneErr.length).to.equal(2);
expect(doneErr[0]).to.deep.equal(errIn);
expect(doneErr[1]).to.deep.equal(errIn);
});

it('should handle server side close correctly', async () => {
Expand Down

0 comments on commit 568a853

Please sign in to comment.