Prove dell'interfaccia: la risposta in ritardo si trattiene (issue #8)
APageLeftWhileLoadingLeavesNothingBehind apriva Attività e tornava alla Libreria con un setTimeout di 200 ms, poi aspettava la risposta in ritardo di /api/alerts. Su una macchina lenta, a 200 ms Attività non aveva ancora caricato activity.js e day-strip.js: la pagina veniva lasciata prima di montarsi, /api/alerts non partiva mai e l'attesa scadeva dopo 30 s, come nell'esecuzione 22 della CI. Con Chrome rallentato 20 volte (Emulation.setCPUThrottlingRate, solo in locale) fallisce 3 volte su 3 con lo stesso messaggio e la stessa riga, e in 4 giri su 5 i due moduli arrivano dopo il ritorno alla Libreria (fra 212 e 549 ms). ABrowserPageLeftWhileLoadingNeverTakesTheBrowser aveva la stessa finestra, di 150 ms, e a 20x falliva anche lei. Il prodotto non c'entra. UiBrowser.Requests prende il posto di FetchDelays: trattiene le richieste scelte finché la prova non le rilascia, conta quelle partite e quelle finite (dopo la continuazione della pagina e, se lo legge, dopo il corpo della risposta) e registra ogni richiesta della pagina. Le due prove aspettano che la pagina abbia chiesto la risposta, la lasciano, la rilasciano e controllano quando la pagina ne ha finito: nessun timer rimasto, niente di Attività sulla Libreria, nessun browser preso né vista aperta. Allo stesso modo la risposta vecchia della Libreria e le tessere in ritardo di «In evidenza» arrivano sempre dopo il disegno nuovo, invece che dopo 600 o 700 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7a70b464f3
commit
248e9f74ce
3 files changed
+124
-46
No files matched your search
@@ -16,25 +16,89 @@ namespace Febe.Tests.Web.Support;
|
||||
internal sealed class UiBrowser : IAsyncDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Installed before the page scripts: <c>window.__delays</c> holds { match, ms } rules that hold back a matching
|
||||
/// fetch, so that a test can make one answer arrive late (stale answers, slow pages); <c>window.__done[match]</c>
|
||||
/// counts the held-back requests that are over (answered, failed or aborted).
|
||||
/// Installed before the page scripts: what the page asks of the server, and answers that come when the test says.
|
||||
/// <list type="bullet">
|
||||
/// <item><c>window.__asked</c>: every request the page started, in order («GET /api/connection», «POST
|
||||
/// /api/connection/begin», «WS /api/vista»); <c>window.__sockets</c>: its WebSockets.</item>
|
||||
/// <item><c>window.__busy()</c>: the requests not over yet. A fetch is over once the page's own continuation of
|
||||
/// its answer has run, and the body it reads there (as api.js does) has arrived; a WebSocket once it is open or
|
||||
/// closed. See <see cref="WaitForQuietAsync"/>.</item>
|
||||
/// <item><c>window.__hold(match)</c> holds back every fetch whose address contains match until
|
||||
/// <c>window.__release(match)</c>, also one the page aborts meanwhile; <c>window.__started[match]</c> counts the
|
||||
/// held-back requests that started, <c>window.__done[match]</c> those that are over.</item>
|
||||
/// </list>
|
||||
/// An answer is late because the test holds it, never because of a delay the page might outlast on a slow machine:
|
||||
/// the test waits until the page has asked (<c>__started</c>), does what has to come first, releases it, and
|
||||
/// checks once the page is done with it (<c>__done</c>).
|
||||
/// </summary>
|
||||
public const string FetchDelays = """
|
||||
public const string Requests = """
|
||||
(() => {
|
||||
const original = window.fetch.bind(window);
|
||||
window.__delays = [];
|
||||
const fetchNow = window.fetch.bind(window);
|
||||
const later = window.setTimeout.bind(window);
|
||||
const NativeSocket = window.WebSocket;
|
||||
const holds = [];
|
||||
let busy = 0;
|
||||
const count = (counts, key) => { counts[key] = (counts[key] || 0) + 1; };
|
||||
const pathOf = (url) => { const u = new URL(url, location.href); return u.pathname + u.search; };
|
||||
window.__asked = [];
|
||||
window.__sockets = [];
|
||||
window.__started = {};
|
||||
window.__done = {};
|
||||
window.__busy = () => busy;
|
||||
window.__hold = (match) => {
|
||||
let release;
|
||||
holds.push({ match, gate: new Promise((resolve) => { release = resolve; }), release: () => release() });
|
||||
return true;
|
||||
};
|
||||
window.__release = (match) => {
|
||||
const hold = holds.find((h) => h.match === match);
|
||||
if (!hold) throw new Error(`nessuna richiesta trattenuta per ${match}`);
|
||||
hold.release();
|
||||
return true;
|
||||
};
|
||||
window.fetch = async (input, init) => {
|
||||
const url = typeof input === 'string' ? input : input.url;
|
||||
const rule = window.__delays.find((d) => url.includes(d.match));
|
||||
if (!rule) return original(input, init);
|
||||
const url = input instanceof Request ? input.url : String(input);
|
||||
const method = ((init && init.method) || (input instanceof Request ? input.method : 'GET')).toUpperCase();
|
||||
const hold = holds.find((h) => url.includes(h.match));
|
||||
window.__asked.push(`${method} ${pathOf(url)}`);
|
||||
if (hold) count(window.__started, hold.match);
|
||||
busy += 1;
|
||||
let read = Promise.resolve();
|
||||
try {
|
||||
await new Promise((resolve) => setTimeout(resolve, rule.ms));
|
||||
return await original(input, init);
|
||||
if (hold) await hold.gate;
|
||||
const response = await fetchNow(input, init);
|
||||
for (const name of ['json', 'text', 'blob', 'arrayBuffer']) {
|
||||
const body = response[name].bind(response);
|
||||
response[name] = () => {
|
||||
const reading = body();
|
||||
read = reading.then(() => {}, () => {});
|
||||
return reading;
|
||||
};
|
||||
}
|
||||
return response;
|
||||
} finally {
|
||||
// After the page's own continuation of this answer has run.
|
||||
setTimeout(() => { window.__done[rule.match] = (window.__done[rule.match] || 0) + 1; }, 50);
|
||||
// The page's continuation of this answer runs before the first timer and, if it reads the body, the
|
||||
// continuation of that read runs before the second.
|
||||
later(() => read.then(() => later(() => {
|
||||
busy -= 1;
|
||||
if (hold) count(window.__done, hold.match);
|
||||
})));
|
||||
}
|
||||
};
|
||||
window.WebSocket = class extends NativeSocket {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
window.__asked.push(`WS ${pathOf(this.url)}`);
|
||||
window.__sockets.push(this);
|
||||
busy += 1;
|
||||
let connecting = true;
|
||||
const over = () => {
|
||||
if (!connecting) return;
|
||||
connecting = false;
|
||||
busy -= 1;
|
||||
};
|
||||
this.addEventListener('open', over);
|
||||
this.addEventListener('close', over);
|
||||
}
|
||||
};
|
||||
})();
|
||||
@@ -247,6 +311,17 @@ internal sealed class UiBrowser : IAsyncDisposable
|
||||
public Task AddInitScriptAsync(string source) =>
|
||||
SendAsync("Page.addScriptToEvaluateOnNewDocument", new JsonObject { ["source"] = source });
|
||||
|
||||
/// <summary>
|
||||
/// Waits until no request of the page is in flight (<see cref="Requests"/>): every command it sent has reached the
|
||||
/// server and been answered, and whatever the page did with the answers, a new request included, has been done. The
|
||||
/// moment to check that something did not happen, instead of a pause a slow machine may not wait out.
|
||||
/// </summary>
|
||||
public Task WaitForQuietAsync(string what) => WaitAsync("window.__busy() === 0", what);
|
||||
|
||||
/// <summary>The requests the page started so far (<see cref="Requests"/>) whose «METHOD path» contains the text.</summary>
|
||||
public Task<string[]> AskedAsync(string match) =>
|
||||
EvalAsync<string[]>($"window.__asked.filter((r) => r.includes({JsonSerializer.Serialize(match)}))");
|
||||
|
||||
/// <summary>Loads an address of the interface and waits until the frame and the first page are drawn.</summary>
|
||||
public async Task OpenAsync(Uri address)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user