作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
测试的目的是模拟获取不同数据源的并行请求。我为每个请求引入了人工延迟,并在一段时间后返回一个带有标识数字的简单字符串,以查看数据是否已从缓存中加载(500 毫秒内的请求)。因此,对于 500 毫秒内加载的数据,输出应为“A1B1”,否则,在 500 毫秒后,应为“A2B2”,依此类推。
// index.test.js
const { wait } = require('./util/wait.js');
const { requestAandB, requestBandC } = require('./index.js');
test('Test cache timings ', () => Promise.all([
// send two requests in parallel after 0 ms (immediately)
wait(0).then(() => Promise.all([
expect(requestAandB()).resolves.toEqual('A1B1'),
expect(requestBandC()).resolves.toEqual('B1C1'),
])),
// send two requests in parallel after 480 ms
wait(480).then(() => Promise.all([
expect(requestAandB()).resolves.toEqual('A1B1'),
expect(requestBandC()).resolves.toEqual('B1C1'),
])),
// send two requests in parallel after 520 ms
wait(520).then(() => Promise.all([
expect(requestAandB()).resolves.toEqual('A2B2'),
expect(requestBandC()).resolves.toEqual('B2C2'),
])),
]));
这就是我模拟数据加载的方式
// get-data.js
async function mockLoading(str) {
// introduce some latency
const waitDuration = Math.round(Math.random() * (WAIT_MAX - WAIT_MIN)) + WAIT_MIN;
await wait(waitDuration);
// create or increase counter every time the function is being called
counters[str] = counters[str] === undefined ? 1 : counters[str] + 1;
return str + counters[str];
}
module.exports = {
loadDataA: async () => mockLoading('A'),
loadDataB: async () => mockLoading('B'),
loadDataC: async () => mockLoading('C'),
}
最后,
requestAandB
方法的实现和
requestBandC
在测试文件中导入:
const { loadDataA, loadDataB, loadDataC } = require('./model/get-data.js');
const all = Promise.all([loadDataA(), loadDataB(), loadDataC()])
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
async function requestAandB() {
const temp = await all
await delay(Math.random() * 510)
return temp.filter((_, i) => i < 2).join("")
}
async function requestBandC() {
const temp = await all
await delay(Math.random() * 510)
return temp.filter((_, i) => i > 0).join("")
}
module.exports = { requestAandB, requestBandC }
对数据“A1B1”、“B1C1”的测试很好,但由于延迟(在
mockLoading
函数中)总是高于 500 毫秒阈值,因此我无法获得之后返回的数据的正确结果。实际上,“A2B2”和“B2C2”总是失败。
最佳答案
看起来您的第一个测试也无法正常工作。
So for data loaded within 500ms the output should be "A1B1", else, after 500ms, it should be "A2B2" and so on.
Tests for the data "A1B1", "B1C1" are fine, but because the latency (in the mockLoading function) is always above the 500ms threshold, I am not able to get the right results for data returned after that. In effect, "A2B2" and "B2C2" always fail.
requestAandB
和
requestBandC
, 因为使用
Math.random()
可以给你随机数,并可能随机失败测试。所以试试这个方法:
test('Test cache timings ', () =>
Promise.all([
// send two requests in parallel after 0 ms (immediately)
expect(requestAandB(0)).resolves.toEqual('A1B1'),
expect(requestBandC(0)).resolves.toEqual('B1C1'),
// send two requests in parallel after 480 ms
expect(requestAandB(480)).resolves.toEqual('A1B1'),
expect(requestBandC(480)).resolves.toEqual('B1C1'),
// send two requests in parallel after 520 ms
expect(requestAandB(520)).resolves.toEqual('A2B2'),
expect(requestBandC(520)).resolves.toEqual('B2C2'),
// send two requests in parallel after 360 ms
expect(requestAandB(360)).resolves.toEqual('A1B1'),
expect(requestBandC(360)).resolves.toEqual('B1C1'),
// send two requests in parallel after 750 ms
expect(requestAandB(750)).resolves.toEqual('A2B2'),
expect(requestBandC(750)).resolves.toEqual('B2C2'),
]));
index.js
const { loadDataA, loadDataB, loadDataC } = require('./get-data.js');
async function requestAandB(time) {
const temp = await Promise.all([
loadDataA(time),
loadDataB(time),
loadDataC(time),
]); // don't use global Promise outside of this function
return temp.filter((_, i) => i < 2).join('');
}
async function requestBandC(time) {
const temp = await Promise.all([
loadDataA(time),
loadDataB(time),
loadDataC(time),
]); // don't use global Promise outside of this function
return temp.filter((_, i) => i > 0).join('');
}
module.exports = { requestAandB, requestBandC };
获取数据.js
// get-data.js
const { wait } = require('./wait.js');
async function mockLoading(time, str) {
await wait(time);
// here is the logic if time is less then 500ms then append 1 else append 2
let label = str;
if (Math.ceil(time / 500) <= 1) {
label += '1';
} else {
label += '2';
}
return label;
}
module.exports = {
loadDataA: async time => mockLoading(time, 'A'),
loadDataB: async time => mockLoading(time, 'B'),
loadDataC: async time => mockLoading(time, 'C'),
};
关于javascript - 如何在 JEST 中测试并行的模拟数据请求,同时以 500 毫秒的阈值模拟缓存响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68030700/
我是一名优秀的程序员,十分优秀!