Skip to content

Commit

Permalink
Fix the retryCount option retrying an incorrect amount (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
MunyuShizumi committed Jan 7, 2024
1 parent 634cb2b commit 917ab12
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
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 @@ -480,7 +480,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

0 comments on commit 917ab12

Please sign in to comment.