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 #528 (retry count 1 less than retry limit) #547

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/core/Ky.ts
Expand Up @@ -208,7 +208,7 @@ export class Ky {
protected _calculateRetryDelay(error: unknown) {
this._retryCount++;

if (this._retryCount < this._options.retry.limit && !(error instanceof TimeoutError)) {
if (this._retryCount <= this._options.retry.limit && !(error instanceof TimeoutError)) {
if (error instanceof HTTPError) {
if (!this._options.retry.statusCodes.includes(error.response.status)) {
return 0;
Expand Down
2 changes: 1 addition & 1 deletion test/browser.ts
Expand Up @@ -479,7 +479,7 @@ browserTest('retry with body', [chromium, webkit], async (t: ExecutionContext, p
page.evaluate(async (url: string) => window.ky(`${url}/test`, {
body: 'foo',
method: 'PUT',
retry: 2,
retry: 1,
}), server.url),
{message: /HTTPError: Request failed with status code 502 Bad Gateway/},
);
Expand Down
4 changes: 2 additions & 2 deletions test/hooks.ts
Expand Up @@ -427,7 +427,7 @@ test('beforeRetry hook is called even if the error has no response', async t =>

const text = await ky
.get(server.url, {
retry: 2,
retry: 1,
async fetch(request) {
if (requestCount === 0) {
requestCount++;
Expand Down Expand Up @@ -473,7 +473,7 @@ test('beforeRetry hook with parseJson and error.response.json()', async t => {

const json = await ky
.get(server.url, {
retry: 2,
retry: 1,
parseJson(text) {
t.is(text, 'text');
return {awesome: true};
Expand Down
30 changes: 15 additions & 15 deletions test/retry.ts
Expand Up @@ -15,7 +15,7 @@ test('network error', async t => {
server.get('/', (_request, response) => {
requestCount++;

if (requestCount === defaultRetryCount) {
if (requestCount === defaultRetryCount + 1) {
response.end(fixture);
} else {
response.status(99_999).end();
Expand All @@ -34,7 +34,7 @@ test('status code 500', async t => {
server.get('/', (_request, response) => {
requestCount++;

if (requestCount === defaultRetryCount) {
if (requestCount === defaultRetryCount + 1) {
response.end(fixture);
} else {
response.sendStatus(500);
Expand All @@ -53,7 +53,7 @@ test('only on defined status codes', async t => {
server.get('/', (_request, response) => {
requestCount++;

if (requestCount === defaultRetryCount) {
if (requestCount === defaultRetryCount + 1) {
response.end(fixture);
} else {
response.sendStatus(400);
Expand All @@ -72,7 +72,7 @@ test('not on POST', async t => {
server.post('/', (_request, response) => {
requestCount++;

if (requestCount === defaultRetryCount) {
if (requestCount === defaultRetryCount + 1) {
response.end(fixture);
} else {
response.sendStatus(500);
Expand All @@ -93,7 +93,7 @@ test('respect 413 Retry-After', async t => {
server.get('/', (_request, response) => {
requestCount++;

if (requestCount === defaultRetryCount) {
if (requestCount === defaultRetryCount + 1) {
response.end((Date.now() - lastTried413access).toString());
} else {
response.writeHead(413, {
Expand All @@ -115,7 +115,7 @@ test('respect 413 Retry-After with timestamp', async t => {
const server = await createHttpTestServer({bodyParser: false});
server.get('/', (_request, response) => {
requestCount++;
if (requestCount === defaultRetryCount) {
if (requestCount === defaultRetryCount + 1) {
response.end((Date.now() - lastTried413access).toString());
} else {
// @NOTE we need to round up to the next second due to http-date resolution
Expand All @@ -129,7 +129,7 @@ test('respect 413 Retry-After with timestamp', async t => {

const result = await ky(server.url).text();
t.true(Number(result) >= retryAfterOn413 * 1000);
t.is(requestCount, 2);
t.is(requestCount, 3);

await server.close();
});
Expand All @@ -151,7 +151,7 @@ test('doesn\'t retry on 413 without Retry-After header', async t => {
await server.close();
});

test.failing('respect number of retries', async t => {
test('respect number of retries', async t => {
let requestCount = 0;

const server = await createHttpTestServer();
Expand Down Expand Up @@ -207,7 +207,7 @@ test('respect retry methods', async t => {
await t.throwsAsync(
ky(server.url, {
retry: {
limit: 3,
limit: 2,
methods: ['get'],
},
}).text(),
Expand Down Expand Up @@ -251,7 +251,7 @@ test('respect maxRetryAfter', async t => {
await t.throwsAsync(
ky(server.url, {
retry: {
limit: 5,
limit: 4,
maxRetryAfter: 2000,
},
}).text(),
Expand All @@ -273,7 +273,7 @@ test('retry - can provide retry as number', async t => {
response.sendStatus(408);
});

await t.throwsAsync(ky(server.url, {retry: 5}).text(), {
await t.throwsAsync(ky(server.url, {retry: 4}).text(), {
message: /Request Timeout/,
});
t.is(requestCount, 5);
Expand Down Expand Up @@ -347,7 +347,7 @@ test('does retry on 408 with methods provided as array', async t => {
await t.throwsAsync(
ky(server.url, {
retry: {
limit: 4,
limit: 3,
methods: ['get'],
},
}).text(),
Expand All @@ -373,7 +373,7 @@ test('does retry on 408 with statusCodes provided as array', async t => {
await t.throwsAsync(
ky(server.url, {
retry: {
limit: 4,
limit: 3,
statusCodes: [408],
},
}).text(),
Expand Down Expand Up @@ -443,14 +443,14 @@ test('throws when retry.statusCodes is not an array', async t => {
});

test('respect maximum backoff', async t => {
const retryCount = 5;
const retryCount = 4;
let requestCount = 0;

const server = await createHttpTestServer();
server.get('/', (_request, response) => {
requestCount++;

if (requestCount === retryCount) {
if (requestCount === retryCount + 1) {
response.end(fixture);
} else {
response.sendStatus(500);
Expand Down