Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split watch error and watch done handlers. #555

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
private resourceVersion: string;
private readonly indexCache: { [key: string]: T[] } = {};
private readonly callbackCache: { [key: string]: Array<ObjectCallback<T>> } = {};
private stopped: boolean;

public constructor(
private readonly path: string,
Expand All @@ -24,13 +25,19 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
this.callbackCache[DELETE] = [];
this.callbackCache[ERROR] = [];
this.resourceVersion = '';
this.stopped = true;
if (autoStart) {
this.doneHandler(null);
this.start();
}
}

public async start(): Promise<void> {
await this.doneHandler(null);
this.stopped = false;
await this.doneHandler();
}

public stop(): void {
this.stopped = true;
}

public on(verb: string, cb: ObjectCallback<T>): void {
Expand Down Expand Up @@ -72,9 +79,15 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
return this.resourceVersion;
}

private async doneHandler(err: any): Promise<any> {
private async errorHandler(err: any): Promise<void> {
if (err) {
this.callbackCache[ERROR].forEach((elt: ObjectCallback<T>) => elt(err));
}
this.stopped = true;
}

private async doneHandler(): Promise<any> {
if (this.stopped) {
return;
}
// TODO: Don't always list here for efficiency
Expand All @@ -90,6 +103,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
{ resourceVersion: list.metadata!.resourceVersion },
this.watchHandler.bind(this),
this.doneHandler.bind(this),
this.errorHandler.bind(this),
);
}

Expand Down
84 changes: 72 additions & 12 deletions src/cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ describe('ListWatchCache', () => {
};
const promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(() => {
resolve();
});
Expand Down Expand Up @@ -146,7 +152,13 @@ describe('ListWatchCache', () => {
};
const promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(() => {
resolve();
});
Expand Down Expand Up @@ -227,7 +239,13 @@ describe('ListWatchCache', () => {
};
const promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(() => {
resolve();
});
Expand Down Expand Up @@ -298,7 +316,13 @@ describe('ListWatchCache', () => {
};
let promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(() => {
resolve();
});
Expand All @@ -320,12 +344,18 @@ describe('ListWatchCache', () => {

promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(() => {
resolve();
});
});
doneHandler(null);
doneHandler();
await promise;
expect(addObjects).to.deep.equal(list);
expect(updateObjects).to.deep.equal(list);
Expand Down Expand Up @@ -371,7 +401,13 @@ describe('ListWatchCache', () => {
};
let promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(() => {
resolve();
});
Expand All @@ -394,13 +430,19 @@ describe('ListWatchCache', () => {

promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(() => {
resolve();
});
});
listObj.items = list2;
doneHandler(null);
doneHandler();
await promise;
expect(addObjects).to.deep.equal(list);
expect(updateObjects).to.deep.equal(list2);
Expand Down Expand Up @@ -448,7 +490,13 @@ describe('ListWatchCache', () => {
};
const promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(() => {
resolve();
});
Expand Down Expand Up @@ -568,7 +616,13 @@ describe('ListWatchCache', () => {
};
const watchCalled = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(resolve);
});
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn);
Expand Down Expand Up @@ -627,7 +681,13 @@ describe('ListWatchCache', () => {
};
const watchCalled = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
fakeWatch.watch(
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
mock.anything(),
),
).thenCall(resolve);
});
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn);
Expand Down
23 changes: 11 additions & 12 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export class Watch {
path: string,
queryParams: any,
callback: (phase: string, apiObj: any, watchObj?: any) => void,
done: (err: any) => void,
done: () => void,
error: (err: any) => void,
): Promise<any> {
const cluster = this.config.getCurrentCluster();
if (!cluster) {
Expand Down Expand Up @@ -76,20 +77,18 @@ export class Watch {
// ignore parse errors
}
});
let errOut: Error | null = null;
stream.on('error', (err) => {
errOut = err;
done(err);
});
stream.on('close', () => done(errOut));
stream.on('error', error);
stream.on('close', done);

const req = this.requestImpl.webRequest(requestOptions, (error, response, body) => {
if (error) {
done(error);
const req = this.requestImpl.webRequest(requestOptions, (err, response, body) => {
if (err) {
error(err);
done();
} else if (response && response.statusCode !== 200) {
done(new Error(response.statusMessage));
error(new Error(response.statusMessage));
done();
} else {
done(null);
done();
}
});
req.pipe(stream);
Expand Down
33 changes: 17 additions & 16 deletions src/watch_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect } from 'chai';
import request = require('request');
import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers';
import { anyFunction, anything, capture, instance, mock, reset, verify, when } from 'ts-mockito';

import { KubeConfig } from './config';
Expand Down Expand Up @@ -60,8 +59,10 @@ describe('Watch', () => {
path,
{},
(phase: string, obj: string) => {},
(err: any) => {
() => {
doneCalled = true;
},
(err: any) => {
doneErr = err;
},
);
Expand Down Expand Up @@ -130,8 +131,10 @@ describe('Watch', () => {
receivedTypes.push(phase);
receivedObjects.push(obj);
},
(err: any) => {
() => {
doneCalled = true;
},
(err: any) => {
doneErr = err;
},
);
Expand All @@ -153,7 +156,7 @@ describe('Watch', () => {
doneCallback(null, null, null);

expect(doneCalled).to.equal(true);
expect(doneErr).to.equal(null);
expect(doneErr).to.be.undefined;

const errIn = { error: 'err' };
doneCallback(errIn, null, null);
Expand Down Expand Up @@ -198,10 +201,8 @@ describe('Watch', () => {
receivedTypes.push(phase);
receivedObjects.push(obj);
},
(err: any) => {
doneCalled = true;
doneErr.push(err);
},
() => (doneCalled = true),
(err: any) => doneErr.push(err),
);

verify(fakeRequestor.webRequest(anything(), anyFunction()));
Expand All @@ -217,9 +218,8 @@ describe('Watch', () => {
expect(receivedObjects).to.deep.equal([obj1.object]);

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

it('should handle server side close correctly', async () => {
Expand Down Expand Up @@ -258,10 +258,8 @@ describe('Watch', () => {
receivedTypes.push(phase);
receivedObjects.push(obj);
},
(err: any) => {
doneCalled = true;
doneErr = err;
},
() => (doneCalled = true),
(err: any) => (doneErr = err),
);

verify(fakeRequestor.webRequest(anything(), anyFunction()));
Expand All @@ -277,7 +275,7 @@ describe('Watch', () => {
expect(receivedObjects).to.deep.equal([obj1.object]);

expect(doneCalled).to.equal(true);
expect(doneErr).to.be.null;
expect(doneErr).to.be.undefined;
});

it('should ignore JSON parse errors', async () => {
Expand Down Expand Up @@ -317,6 +315,9 @@ describe('Watch', () => {
() => {
/* ignore */
},
() => {
/* ignore */
},
);

verify(fakeRequestor.webRequest(anything(), anyFunction()));
Expand All @@ -332,7 +333,7 @@ describe('Watch', () => {
const kc = new KubeConfig();
const watch = new Watch(kc);

const promise = watch.watch('/some/path', {}, () => {}, () => {});
const promise = watch.watch('/some/path', {}, () => {}, () => {}, () => {});
expect(promise).to.be.rejected;
});
});