Skip to content

Commit

Permalink
#1 暴露队列对象
Browse files Browse the repository at this point in the history
  • Loading branch information
cnwhy committed Oct 11, 2023
1 parent 693d914 commit 1f6c411
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ let axios1 = axios.create({
retryIsJump: true //是否立即重试, 否则将在请求队列尾部插入重试请求
}
});

let {stop, start, clear} = axios1.requestQueue; // 暴露内部的队列对像,可以使用 stop start clear 等 API;
```

> 更多 `queueOptions` 配制可参看[这里](https://github.com/cnwhy/promise-queue-plus#queuepushpromisefun-args-options)
Expand All @@ -48,4 +50,4 @@ axios.get('https://www.google.com',{
```js
const aec = require('axios-extra/create');
const axios = aec(requeir('axios'), 5, { retry: 5 });
```
```
5 changes: 5 additions & 0 deletions create.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ function proxyAxios(queue, axios) {
return create(attr.call(this, options), maxConcurrent, queueOptions);
};
}
if (property === "requestQueue") {
// const { start, stop, clear } = queue;
// return { start, stop, clear };
return queue;
}
return attr;
}
});
Expand Down
84 changes: 75 additions & 9 deletions test/axios_other.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,88 @@
import test from 'ava';
import axios from '../index';
import test from "ava";
import axios from "../index";
const testUrl = "https://www.baidu.com";

test('maxConcurrent 不能小于等于零', t => {
test("maxConcurrent 不能小于等于零", (t) => {
t.plan(4);
t.throws(() => {
axios.defaults.maxConcurrent = 0;
});
t.throws(function(){
t.throws(function () {
let axios1 = axios.create({
maxConcurrent: 0
maxConcurrent: 0,
});
})
});
t.throws(() => {
axios.defaults.maxConcurrent = -1;
});
t.throws(function(){
t.throws(function () {
let axios1 = axios.create({
maxConcurrent: -1
maxConcurrent: -1,
});
})
});
});

test("调用Queue API stop", async (t) => {
t.plan(1);
const axios1 = axios.create({ maxConcurrent: 1 });
let k = Promise.all([
axios1.get(testUrl).then(t.pass, t.fail)
]);
axios1.get(testUrl).then(t.fail, t.fail);
axios1.get(testUrl).then(t.fail, t.fail);
axios1.get(testUrl).then(t.fail, t.fail);
axios1.requestQueue.stop();
return k;
});

test("调用Queue API start", async (t) => {
t.plan(4);
const axios1 = axios.create({ maxConcurrent: 1 });
let k = Promise.all([
axios1.get(testUrl).then(t.pass, t.fail),
axios1.get(testUrl).then(t.pass, t.fail),
axios1.get(testUrl).then(t.pass, t.fail),
axios1.get(testUrl).then(t.pass, t.fail),
]);

axios1.requestQueue.stop();
setTimeout(() => {
axios1.requestQueue.start();
}, 100)
return k;
});

test("调用Queue API clear", async (t) => {
t.plan(4);
const axios1 = axios.create({ maxConcurrent: 1 });
let k = Promise.all([
axios1.get(testUrl).then(
(data) => {
t.pass();
},
(err) => {
t.fail();
}
),
axios1.get(testUrl).then(
t.fail,
(err) => {
t.is(err, "runClear");
}
),
axios1.get(testUrl).then(
t.fail,
(err) => {
t.is(err, "runClear");
}
),
axios1.get(testUrl).then(
t.fail,
(err) => {
t.is(err, "runClear");
}
),
]);
axios1.requestQueue.clear('runClear');
return k;
});
4 changes: 2 additions & 2 deletions test/testFn.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function teset(test, axios, defConcurrent = 10, defRetry = 0) {
const testUrl = 'https://cnodejs.org/api/v1/topics';
const testUrl = 'https://www.baidu.com';
function test_retry(retry){
return (t)=>{
t.plan(Math.max(2,retry + 2));
Expand All @@ -23,7 +23,7 @@ module.exports = function teset(test, axios, defConcurrent = 10, defRetry = 0) {
test.serial('axios原功能测试', async t => {
let data = await axios(testUrl).then(res => res.data);
// console.log(1);
t.is(data.success, true);
t.pass()
});

test.serial('用axios.defaults.maxConcurrent检查默认并发 ', async t => {
Expand Down

0 comments on commit 1f6c411

Please sign in to comment.