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

Fix default retry option value when specifying a number #809

Merged
merged 4 commits into from Jun 21, 2019

Conversation

zero1five
Copy link
Contributor

Fixes: #806

Checklist

  • I have read the documentation.

@zero1five
Copy link
Contributor Author

test('works when defaults.options.retry is not an object', withServer, async (t, server, got) => {
	server.get('/', handler413);

	const instance = got.extend({
		retry: 2
	});

	const {retryCount} = await instance({
		throwHttpErrors: false
	});
	t.is(retryCount, 0);
});

Failed test case made me feel a little confused. 🙇‍♂️

Copy link
Collaborator

@szmarczak szmarczak left a comment

Choose a reason for hiding this comment

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

This doesn't work for custom instances

@zero1five
Copy link
Contributor Author

I'll check back later, I think I'm missing something very important.

@zero1five
Copy link
Contributor Author

zero1five commented May 27, 2019

@szmarczak hi, I studied the test carefully, Before this pr, retryCount didn't seem to be running properly.

got/test/retry.ts

Lines 277 to 289 in f77db99

test('works when defaults.options.retry is not an object', withServer, async (t, server, got) => {
server.get('/', handler413);
const instance = got.extend({
retry: 2
});
const {retryCount} = await instance({
throwHttpErrors: false
});
t.is(retryCount, 0);
});

new instance created with extend should include the 413 statusCodes (if the default statusCodes is not modified).this use case was previously successful because it did not include 413( empty Set).

Maybe we should remove 413 out of the default status code?

@sindresorhus
Copy link
Owner

Hey, sorry, we just merged #797 which caused a lot of merge conflicts. Would you be able to fix the conflicts?

}
} else {
options.retry = {
..._innerDefaults.options.retry,
Copy link
Collaborator

@szmarczak szmarczak Jun 17, 2019

Choose a reason for hiding this comment

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

This line (91) seem to be useless.

source/index.ts Outdated Show resolved Hide resolved

if (retry !== false) {
options.retry = _innerDefaults.options.retry;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this line really needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, in #806 default configure are missing.

@zero1five zero1five force-pushed the fix/options-retry branch 2 times, most recently from ce54af1 to c568f09 Compare June 18, 2019 00:52
@zero1five
Copy link
Contributor Author

@szmarczak Hi, I have made some modifications, Would you please have a look at it again?

All test cases so far have only one failure, which is here, I'm not sure if there's any historical reason for it. Can you take a look

Thanks!

@zero1five
Copy link
Contributor Author

CI:

  1 test failed
  2 tests skipped
  retry.ts  works when defaults.options.retry is not an object
  /home/travis/build/sindresorhus/got/test/retry.ts:287
   286:   });                 
   287:   t.is(retryCount, 0);
   288: });                   
  Difference:
  - 2
  + 0

@sindresorhus
Copy link
Owner

Yeah, it's unclear to me why it was previously testing against 0. @szmarczak ?

@sindresorhus sindresorhus changed the title retry: fix default value when enter a number Fix default retry option value when specifying a number Jun 20, 2019
@szmarczak
Copy link
Collaborator

It was introduced by 10d22b7, before the Retry-After functionality. It was testing against 0 because at that time it wasn't retrying on 413.

Looks like a bug now and it should be tested against 2.

@szmarczak
Copy link
Collaborator

Ah, I get it. Using the retry option with got.extend has been broken, so it was passing. This PR fixes that. I'll review this PR soon.

Copy link
Collaborator

@szmarczak szmarczak left a comment

Choose a reason for hiding this comment

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

This PR doesn't change anything at all. The merge logic is broken, not the preNormalizeArguments. I think it should preNormalize all sources before merging.

source/index.ts Outdated
mutableDefaults: false
};

const got = create(defaults);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's leave the defaults here. No need to create a new file.

statusCodes: new Set(),
errorCodes: new Set(),
maxRetryAfter: undefined
...defaultOptions.options.retry as RetryOption,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is wrong. It should be defaults.retry so we get to the same point as before.

source/merge.ts Outdated
return iterate(options);
}
};
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to move this out because if we are going to pre-normalize all options before merging, we need to avoid require collisions (self-requiring files).

@szmarczak

This comment has been minimized.

@szmarczak

This comment has been minimized.

@szmarczak
Copy link
Collaborator

Renaming options.retry to options.retry.retries should fix everything...

source/merge.ts Outdated
const mergedOptions = merge({} as T & {hooks: Partial<Hooks>}, ...sources.map(source => source || {}));
export function mergeOptions<T extends Options>(...sources: T[]): T & { hooks: Partial<Hooks> } {
sources = sources.map(source => {
if (source) {
Copy link
Owner

Choose a reason for hiding this comment

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

I would invert this an do an early return, to reduce nesting.

source/merge.ts Outdated
export function mergeOptions<T extends Options>(...sources: T[]): T & { hooks: Partial<Hooks> } {
sources = sources.map(source => {
if (source) {
if (!is.object(source.retry)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Same here.

source/merge.ts Outdated
return {};
}) as T[];

const mergedOptions = merge({} as T & { hooks: Partial<Hooks> }, ...sources);
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
const mergedOptions = merge({} as T & { hooks: Partial<Hooks> }, ...sources);
const mergedOptions = merge({} as T & {hooks: Partial<Hooks>}, ...sources);

source/merge.ts Outdated
@@ -42,8 +42,25 @@ export default function merge<Target extends Record<string, unknown>, Source ext
return target as Target & Source;
}

export function mergeOptions<T extends Options>(...sources: T[]): T & {hooks: Partial<Hooks>} {
const mergedOptions = merge({} as T & {hooks: Partial<Hooks>}, ...sources.map(source => source || {}));
export function mergeOptions<T extends Options>(...sources: T[]): T & { hooks: Partial<Hooks> } {
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
export function mergeOptions<T extends Options>(...sources: T[]): T & { hooks: Partial<Hooks> } {
export function mergeOptions<T extends Options>(...sources: T[]): T & {hooks: Partial<Hooks>} {

@sindresorhus sindresorhus merged commit 77e4c49 into sindresorhus:master Jun 21, 2019
sindresorhus pushed a commit that referenced this pull request Jun 21, 2019
Co-authored-by: Szymon Marczak <sz.marczak@gmail.com>
szmarczak added a commit to szmarczak/got that referenced this pull request Jun 25, 2019
…us#809)

Co-authored-by: Szymon Marczak <sz.marczak@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using retry:<Number> in extend removes retry defaults
3 participants