diff --git a/tests/upload_retry_test.mjs b/tests/upload_retry_test.mjs index 7db393b..8eecb44 100644 --- a/tests/upload_retry_test.mjs +++ b/tests/upload_retry_test.mjs @@ -17,6 +17,7 @@ let calls; let delays; let fetches; let onTimeout; +let scanStates = []; globalThis.setTimeout = (fn, delay) => { delays.push(delay); @@ -58,7 +59,7 @@ const makeApi = completeSteps => { if (step instanceof Error) throw step; return step ?? {}; } - if (path === '/uploads/u1') return {scan_state: 'clean'}; + if (path === '/uploads/u1') return {scan_state: scanStates.length ? scanStates.shift() : 'clean'}; throw new Error('caminho inesperado: ' + path); }; }; @@ -198,4 +199,25 @@ const run = async (completeSteps, {abortAfter} = {}) => { } } +// The antivirus takes one file at a time for every customer: a file waiting +// behind others is still waited for, past the old two and a half minutes +// (Sentry DTF-WEB-2E), polling every 5 s after the first 150. +{ + scanStates = Array(250).fill('pending'); + const {result, error} = await run([{}]); + assert.equal(error, undefined); + assert.equal(result, 'u1'); + const waits = delays.filter(d => d === 1000 || d === 5000); + assert.ok(waits.filter(d => d === 1000).length === 150 && waits.includes(5000)); +} +// A scan the server never finishes ends with the message, after its 15 minutes. +{ + scanStates = Array(5000).fill('pending'); + const {error} = await run([{}]); + assert.match(error?.message || '', /Verificação de segurança pendente/); + const waited = delays.filter(d => d === 1000 || d === 5000).reduce((t, d) => t + d, 0); + assert.ok(waited >= 900000 && waited < 910000, 'waited ' + waited); + scanStates = []; +} + console.log('upload retry checks passed'); diff --git a/web/upload.js b/web/upload.js index 6ea27f8..d34124a 100644 --- a/web/upload.js +++ b/web/upload.js @@ -69,15 +69,23 @@ async function sendUpload(file, {api, prefix='/uploads', startPath=prefix, progr if(!id){state=await withRetry(options=>api(startPath,{name:file.name,size:file.size},options));id=state.id;if(resume)localStorage.setItem(key,id);} async function waitForScan(){ onBytes(file.size); - // The antivirus streams the whole file: about a second per 2 MB, and at - // least two and a half minutes. - const attempts=Math.max(150,Math.ceil(file.size/2e6)); - for(let attempt=0;attemptsetTimeout(resolve,1000)); + const pause=waited<150000?1000:5000; + await new Promise(resolve=>setTimeout(resolve,pause)); + waited+=pause; } throw new Error('Verificação de segurança pendente. Tente novamente para consultar o resultado.'); }