Open yu0winter opened 4 months ago
Test Code :
const request = require("request");
const fs = require("fs");
const fse = require('fs-extra');
const crypto = require("crypto");
const filemd5 = (file) => {
var buffer = fs.readFileSync(file);
return crypto.createHash('md5').update(buffer).digest('hex');
// return md5(buffer);
};
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function testMethod() {
let errorList = [];
let md5ErrorList = [];
try {
// 上传文件,并立刻下载,测试下载问题
const fileUrl = "TODO";
const fileSize = "TODO";
const md5 = "TODO";
for (let i = 0; i < 1000; i++) {
let fileUrl = result.data;
let downloadFilePath = `TODO`;
result = await download(fileUrl, downloadFilePath);
// sleep 100ms wil be ok
await sleep(100)
if (result.code != 0) {
throw result;
}
let fail = false
var info = fs.statSync(downloadFilePath);
if (fileSize != info.size) {
console.error(`文件大小不一致: ${fileSize} vs ${info.size} `);
errorList.push(`[${i}]文件大小不一致: ${fileSize} vs ${info.size} `)
fail = true;
}
let newMd5 = filemd5(downloadFilePath);
if (md5 != newMd5) {
console.error(`[${i}]md5不一致: ${md5} vs ${newMd5} `);
md5ErrorList.push(`[${i}]md5不一致: ${md5} vs ${newMd5} `)
fail = true;
}
// if (fail) {
// throw("失败")
// }
}
console.info("=========== 完成 ===========")
console.log("文件大小:" + errorList.length + "\n" + JSON.stringify(errorList, null, '\t'));
console.log("MD5:" + md5ErrorList.length + "\n" + JSON.stringify(md5ErrorList, null, '\t'));
} catch (error) {
console.error(error);
}
}
Summary
Download a file with request.pipe ,the file may be defect. I try 1000 time, and 60 ~ 80 of them were defect. But when I try add a 100ms sleep, all of time will be ok. Guess the request will end when the steam does not end.
Simplest Example to Reproduce
Expected Behavior
try 1000 time , and expect all right.
Current Behavior
60 ~ 80 of 1000 time were defect.
Possible Solution
Add a 100ms sleep, all of time will be ok. So Guess the request will end when the steam does not end.
Context
Your Environment