Skip to content

Commit

Permalink
Merge pull request #273 from DoubleData/feature/respect-abort-signal
Browse files Browse the repository at this point in the history
Feature/respect abort signal
  • Loading branch information
mindhells committed Jun 4, 2024
2 parents 703df66 + ad9bffb commit 30c50ef
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
59 changes: 58 additions & 1 deletion spec/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import http from 'http';
import nock from 'nock';
import axios, { AxiosError, isAxiosError } from 'axios';
import axios, { AxiosError, CanceledError, isAxiosError } from 'axios';
import axiosRetry, {
isNetworkError,
isSafeRequestError,
Expand Down Expand Up @@ -326,6 +326,63 @@ describe('axiosRetry(axios, { retries, retryCondition })', () => {
.catch(done.fail);
});
});

describe('when request is aborted', () => {
it('should clear timeout if started', (done) => {
const client = axios.create();
setupResponses(client, [
() => nock('http://example.com').get('/test').reply(429),
() => nock('http://example.com').get('/test').reply(429)
]);
axiosRetry(client, {
retries: 1,
retryCondition: () => true,
retryDelay: () => 10000
});
const abortController = new AbortController();
client
.get('http://example.com/test', { signal: abortController.signal })
.then(
() => done.fail(),
(error) => {
expect(error).toBeInstanceOf(CanceledError);
expect(new Date().getTime() - timeStart).toBeLessThan(300);
done();
}
)
.catch(done.fail);
setTimeout(() => abortController.abort(), 200);
const timeStart = new Date().getTime();
});

it('should not start a timeout if not started yet', (done) => {
const client = axios.create();
setupResponses(client, [
() => nock('http://example.com').get('/test').reply(429),
() => nock('http://example.com').get('/test').reply(429)
]);
axiosRetry(client, {
retries: 1,
retryCondition: () => true,
retryDelay: () => 10000,
onRetry: async () => new Promise((resolve) => setTimeout(resolve, 100))
});
const abortController = new AbortController();
client
.get('http://example.com/test', { signal: abortController.signal })
.then(
() => done.fail(),
(error) => {
expect(error).toBeInstanceOf(CanceledError);
expect(new Date().getTime() - timeStart).toBeLessThan(300);
done();
}
)
.catch(done.fail);
setTimeout(() => abortController.abort(), 50);
const timeStart = new Date().getTime();
});
});
});

describe('when it does NOT satisfy the retry condition', () => {
Expand Down
17 changes: 15 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,21 @@ async function handleRetry(
}
config.transformRequest = [(data) => data];
await onRetry(currentState.retryCount, error, config);
return new Promise((resolve) => {
setTimeout(() => resolve(axiosInstance(config)), delay);
if (config.signal?.aborted) {
return Promise.resolve(axiosInstance(config));
}
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => resolve(axiosInstance(config)), delay);
if (config.signal?.addEventListener) {
config.signal.addEventListener(
'abort',
() => {
clearTimeout(timeout);
resolve(axiosInstance(config));
},
{ once: true }
);
}
});
}

Expand Down

0 comments on commit 30c50ef

Please sign in to comment.