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

feat: add timeout to options and use it in the reqOpts #455

Merged
merged 4 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface ServiceOptions extends GoogleAuthOptions {
promise?: PromiseConstructor;
email?: string;
token?: string;
timeout?: number; // http.request.options.timeout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-fenster I'm curious - do we expose a gRPC option for timeout in the GAPIC libraries?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly, but GAPIC libraries can pass any gRPC option by prefixing it with grpc. and it will be passed all the way down to gRPC.

}

export class Service {
Expand All @@ -84,6 +85,7 @@ export class Service {
authClient: GoogleAuth;
private getCredentials: {};
readonly apiEndpoint: string;
timeout: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be optional? Or are we setting it to a default value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am referencing this below


/**
* Service is a base class, meant to be inherited from by a "service," like
Expand All @@ -103,6 +105,7 @@ export class Service {
constructor(config: ServiceConfig, options: ServiceOptions = {}) {
this.baseUrl = config.baseUrl;
this.apiEndpoint = config.apiEndpoint;
this.timeout = options.timeout!;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be nervous using the ! here because it looks like it can actually be undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If options.timeout is undefined the default value for timeout will be used later and not swapped with undefined here

this.globalInterceptors = arrify(options.interceptors_!);
this.interceptors = [];
this.packageJson = config.packageJson;
Expand Down Expand Up @@ -179,7 +182,7 @@ export class Service {
reqOpts: DecorateRequestOptions | StreamRequestOptions,
callback?: BodyResponseCallback
): void | r.Request {
reqOpts = extend(true, {}, reqOpts);
reqOpts = extend(true, {}, reqOpts, {timeout: this.timeout});
const isAbsoluteUrl = reqOpts.uri.indexOf('http') === 0;
const uriComponents = [this.baseUrl];

Expand Down
32 changes: 32 additions & 0 deletions test/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ describe('Service', () => {
assert.strictEqual(service.apiEndpoint, CONFIG.apiEndpoint);
});

it('should default the timeout to undefined', () => {
assert.strictEqual(service.timeout, undefined);
});

it('should localize the timeout', () => {
const timeout = 10000;
const options = extend({}, OPTIONS, {timeout});
const service = new Service(fakeCfg, options);
assert.strictEqual(service.timeout, timeout);
});

it('should localize the getCredentials method', () => {
function getCredentials() {}

Expand Down Expand Up @@ -334,6 +345,27 @@ describe('Service', () => {
service.request_(reqOpts, assert.ifError);
});

it('should not set timeout', done => {
service.makeAuthenticatedRequest = (reqOpts_: DecorateRequestOptions) => {
assert.strictEqual(reqOpts_.timeout, undefined);
done();
};
service.request_(reqOpts, assert.ifError);
});

it('should set reqOpt.timeout', done => {
const timeout = 10000;
const config = extend({}, CONFIG);
const options = extend({}, OPTIONS, {timeout});
const service = new Service(config, options);

service.makeAuthenticatedRequest = (reqOpts_: DecorateRequestOptions) => {
assert.strictEqual(reqOpts_.timeout, timeout);
done();
};
service.request_(reqOpts, assert.ifError);
});

it('should add the User Agent', done => {
const userAgent = 'user-agent/0.0.0';

Expand Down