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

Refactor retry #27

Merged
merged 2 commits into from
Apr 28, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 0 additions & 170 deletions __test__/AxiosRequestTemplate.retry.test.ts

This file was deleted.

3 changes: 3 additions & 0 deletions __test__/AxiosRequestTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ describe('AxiosRequestTemplate', () => {
cache: {
enable: true,
},
retry: {},
},
requestConfig: {
method: 'get',
Expand All @@ -313,6 +314,7 @@ describe('AxiosRequestTemplate', () => {
{
customConfig: {
cache: {},
retry: {},
},
requestConfig: {
headers: {
Expand All @@ -326,6 +328,7 @@ describe('AxiosRequestTemplate', () => {
{
customConfig: {
cache: {},
retry: {},
},
requestConfig: {
data: {},
Expand Down
142 changes: 78 additions & 64 deletions __test__/retry.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import axios from 'axios';
import { AxiosRequestTemplate, CustomConfig } from '../src';
import { AxiosRequestTemplate, Cache } from '../src';
import { mockAxiosResponse, sleep } from './utils';

jest.mock('axios');
const map = new Map<string, Function>();
let times = 0;
const timesMap = new Map<string, number>();
const mockCreate = () => {
return function ({ cancelToken, url }) {
return function (requestConfig) {
const { cancelToken, url, method, data, headers, params } = requestConfig;
const key = JSON.stringify({ url, method, params, data, headers });
const times = timesMap.get(key) || 0;
return new Promise((res, rej) => {
timesMap.set(key, times + 1);
map.set(cancelToken, (msg?: string) => {
rej({ message: msg });
});
if (url === '/config') {
if (times === 3) {
setTimeout(() => res(mockAxiosResponse(requestConfig, requestConfig)));
return;
}
}
if (url === '3') {
if (times === 3) {
setTimeout(() => {
res({ code: 200, data: {}, msg: 'success' });
res(mockAxiosResponse(requestConfig, { code: 200, data: {}, msg: 'success' }));
});
return;
} else {
times++;
}
}
setTimeout(() => {
if (times > 0) {
rej('times * ' + times);
return;
}
rej('404');
});
});
Expand All @@ -39,61 +52,14 @@ const mockCreate = () => {
(axios as any).create.mockImplementation(mockCreate);
(axios as any).isCancel = (value: any) => typeof value === 'object' && 'message' in value;

describe('retry', () => {
class RetryTemp<CC extends CustomConfig> extends AxiosRequestTemplate<CC> {
protected handleRetry(ctx) {
const { customConfig, clearSet } = ctx;
if (customConfig.retry === undefined || customConfig.retry < 1) return;
const maxTimex = customConfig.retry;
let status: 'running' | 'stop' = 'running';
let times = 0;
const clear = () => {
status = 'stop';
};
if (customConfig.tag) {
this.tagCancelMap.get(customConfig.tag)?.push(clear);
}
this.cancelerSet.add(clear);
clearSet.add(clear);
ctx.retry = () => {
return new Promise((res, rej) => {
const handle = () => {
if (times >= maxTimex || status === 'stop') {
return rej('times * ' + times);
}
times++;
this.execRequest({ ...ctx }).then(
(data) => {
res(data);
},
() => {
handle();
},
);
};
handle();
});
};
}

protected beforeRequest(ctx) {
super.beforeRequest(ctx);
this.handleRetry(ctx);
}

protected handleError(ctx, e): any {
const { customConfig } = ctx;
if (customConfig.retry === undefined || axios.isCancel(e)) return super.handleError(ctx, e);
return ctx.retry();
}
}
const get = new RetryTemp().methodFactory('get');
describe('AxiosRequestTemplate retry', () => {
const get = new AxiosRequestTemplate().methodFactory('get');
test('base', async () => {
expect.assertions(4);
// expect.assertions(4);
const list = [
get<{ username: string; id: number }>('/user'),
get<{ username: string; id: number }>('/user', {}, { retry: 2 }),
get<{ username: string; id: number }>('/user', {}, { retry: 10 }),
get<{ username: string; id: number }>('/user', { key: 1 }),
get<{ username: string; id: number }>('/user', { key: 2 }, { retry: 2 }),
get<{ username: string; id: number }>('/user', { key: 3 }, { retry: { times: 10 } }),
];
const res = await Promise.allSettled(list);
expect(res).toEqual([
Expand All @@ -112,18 +78,18 @@ describe('retry', () => {
]);

try {
await get<{ username: string; id: number }>('/user');
await get<{ username: string; id: number }>('/user', { key: 4 });
} catch (e) {
expect(e).toBe('404');
}

try {
await get<{ username: string; id: number }>('/user', {}, { retry: 2 });
await get<{ username: string; id: number }>('/user', { key: 5 }, { retry: 2 });
} catch (e) {
expect(e).toBe('times * 2');
}
try {
await get<{ username: string; id: number }>('/user', {}, { retry: 10 });
await get<{ username: string; id: number }>('/user', { key: 6 }, { retry: 10 });
} catch (e) {
expect(e).toBe('times * 10');
}
Expand All @@ -139,13 +105,61 @@ describe('retry', () => {
} catch (e) {
expect(e).toBe('times * 2');
}
times = 0;
const res = await get<{ username: string; id: number }>('3', {}, { tag: 'cancel', retry: 3 });
expect(res).toEqual({ code: 200, data: {}, msg: 'success' });
});

test('cache&retry,有retry时不要用缓存', async () => {
const req = new AxiosRequestTemplate();
const c = new Cache();
const originSet = c.set;
const mockSet = jest.fn((...args: any) => originSet.apply(c, args));
c.set = mockSet;
const originGet = c.get;
const mockGet = jest.fn((...args: any) => originGet.apply(c, args));
c.get = mockGet;
(req as any).cache = c;
(req as any).afterRequest = () => void 0;

const get = req.methodFactory('get');

const res = await get('/config', { test: 1 }, { retry: 10, cache: true });
delete (res as any).cancelToken;
expect(res).toEqual({
method: 'get',
url: '/config',
params: { test: 1 },
});
expect(mockGet.mock.calls.length).toBe(1);
expect(mockSet.mock.calls.length).toBe(4);
});
describe('immediate', () => {
test('use', async () => {
let res: any;
const p = get('/user', { use: 1 }, { retry: { times: 1, immediate: true, interval: 1000 } });
p.catch((r) => (res = r));

await sleep(0);
expect(res).toBeUndefined();

await sleep(10);
expect(res).toBe('times * 1');
});
test('unused', async () => {
let res: any;
const p = get('/user', { use: 2 }, { retry: { times: 1, immediate: false, interval: 50 } });
p.catch((r) => (res = r));

await sleep(20);
expect(res).toBeUndefined();

await sleep(50);
expect(res).toBe('times * 1');
});
});

describe('cancel', () => {
const req = new RetryTemp();
const req = new AxiosRequestTemplate();
const get = req.methodFactory('get');
test('cancelAll', async () => {
expect.assertions(1);
Expand Down
Loading