Skip to content

Commit

Permalink
fix: 优化test异常逻辑,增加超时提示
Browse files Browse the repository at this point in the history
  • Loading branch information
颜谦 committed Jan 14, 2022
1 parent a8c7e8c commit 5792cb1
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ async function onHome(name, browser) {

async function onTest(target) {
const registries = await getRegistries();
const timeout = 5000;

if (target && await isRegistryNotFound(target)) {
return exit();
Expand All @@ -240,12 +241,21 @@ async function onTest(target) {
const results = await Promise.all(Object.keys(sources).map(async name => {
const { registry } = sources[name];
const start = Date.now();
const response = await fetch(registry + 'nrm', { timeout: 5000 });
let status = '';
let isTimeout = false;
try {
const response = await fetch(registry + 'nrm', { timeout });
status = response.ok;
} catch (error) {
status = false;
isTimeout = error.type === 'request-timeout';
}
return {
name,
registry,
success: response.ok,
success: status,
time: Date.now() - start,
isTimeout
};
}));

Expand All @@ -254,13 +264,14 @@ async function onTest(target) {
const messages = [];
const currentRegistry = await getCurrentRegistry();
const errorMsg = chalk.red(' (Fetch error, if this is your private registry, please ignore)');
const timeoutMsg = chalk.yellow(` (Fetch timeout over ${timeout} ms)`);
const length = Math.max(...Object.keys(sources).map(key => key.length)) + 3;
results.forEach(({ registry, success, time, name }) => {
results.forEach(({ registry, success, time, name, isTimeout }) => {
const isFastest = time === fastest;
const prefix = registry === currentRegistry ? chalk.green('* ') : ' ';
let suffix = (isFastest && !target ? chalk.bgGreenBright(time + ' ms') : time + ' ms');
let suffix = (isFastest && !target) ? chalk.bgGreenBright(time + ' ms') : isTimeout ? 'timeout' : `${time} ms`;
if (!success) {
suffix += errorMsg;
suffix += isTimeout ? timeoutMsg : errorMsg;
}
messages.push(prefix + name + geneDashLine(name, length) + suffix);
});
Expand Down

0 comments on commit 5792cb1

Please sign in to comment.