|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * Render the CDN starter page in a browser, and report what went wrong. |
| 5 | + * |
| 6 | + * One render loads the page until its network goes idle, then reads the first |
| 7 | + * button: a page that throws while rendering still goes idle with an empty |
| 8 | + * root, so the render itself is asserted, not only the error streams. A page |
| 9 | + * with a request that never finishes never goes idle, so its navigation times |
| 10 | + * out: that is an unclean render like any other, reported with the requests |
| 11 | + * still in flight when the wait gave up, so the output names the asset that |
| 12 | + * hung instead of ending the run on a stack trace. renderUntilClean retries |
| 13 | + * an unclean render once, so a blip at a CDN is not a red build, and fits |
| 14 | + * every attempt inside one deadline, so a page that hangs on each attempt |
| 15 | + * still gets its report printed before the job running it is stopped. It |
| 16 | + * hands every retry the attempt it discards, and uncleanFindings names what |
| 17 | + * made that attempt unclean, so a blip that clears on the retry still shows |
| 18 | + * in the log. Only an expected absence (a wait that ran out) becomes a value; |
| 19 | + * every other error throws. |
| 20 | + */ |
| 21 | + |
| 22 | +/** Playwright's error for a navigation whose wait condition never came. */ |
| 23 | +function isTimeout(error) { |
| 24 | + return error?.name === 'TimeoutError'; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Load `url` once in a browser from `launch`, and collect its console errors, |
| 29 | + * page errors and failed requests, and the first button's text (null when no |
| 30 | + * button rendered). A navigation that times out comes back with `timedOut` |
| 31 | + * and the URLs still in flight (`pendingRequests`, in request order); any |
| 32 | + * other navigation error throws. With `budgetMs`, the whole attempt fits in |
| 33 | + * it: the navigation waits no longer than the budget, and the button no |
| 34 | + * longer than what is left of it. |
| 35 | + * |
| 36 | + * @param {string} url |
| 37 | + * @param {{ |
| 38 | + * launch: () => Promise<import('playwright').Browser>, |
| 39 | + * navigationTimeoutMs?: number, |
| 40 | + * buttonTimeoutMs?: number, |
| 41 | + * budgetMs?: number, |
| 42 | + * now?: () => number, |
| 43 | + * }} options |
| 44 | + */ |
| 45 | +export async function renderPage( |
| 46 | + url, |
| 47 | + { |
| 48 | + launch, |
| 49 | + navigationTimeoutMs = 90_000, |
| 50 | + buttonTimeoutMs = 15_000, |
| 51 | + budgetMs, |
| 52 | + now = Date.now, |
| 53 | + }, |
| 54 | +) { |
| 55 | + const startedAt = now(); |
| 56 | + /** |
| 57 | + * `limitMs`, cut to what is left of the budget. Never 0: Playwright reads a |
| 58 | + * timeout of 0 as no timeout at all. |
| 59 | + */ |
| 60 | + const withinBudget = limitMs => |
| 61 | + budgetMs == null |
| 62 | + ? limitMs |
| 63 | + : Math.max(1, Math.min(limitMs, budgetMs - (now() - startedAt))); |
| 64 | + const consoleErrors = []; |
| 65 | + const pageErrors = []; |
| 66 | + const failedRequests = []; |
| 67 | + /** Requests the page made that have neither finished nor failed. */ |
| 68 | + const inFlight = new Set(); |
| 69 | + |
| 70 | + const browser = await launch(); |
| 71 | + try { |
| 72 | + const page = await browser.newPage(); |
| 73 | + page.on('console', m => { |
| 74 | + if (m.type() === 'error') consoleErrors.push(m.text().slice(0, 300)); |
| 75 | + }); |
| 76 | + page.on('pageerror', e => pageErrors.push(String(e).slice(0, 300))); |
| 77 | + page.on('request', r => inFlight.add(r)); |
| 78 | + page.on('requestfinished', r => inFlight.delete(r)); |
| 79 | + page.on('requestfailed', r => { |
| 80 | + inFlight.delete(r); |
| 81 | + failedRequests.push(`${r.url()} :: ${r.failure()?.errorText}`); |
| 82 | + }); |
| 83 | + page.on('response', r => { |
| 84 | + if (r.status() >= 400) failedRequests.push(`${r.status()} ${r.url()}`); |
| 85 | + }); |
| 86 | + |
| 87 | + try { |
| 88 | + await page.goto(url, { |
| 89 | + waitUntil: 'networkidle', |
| 90 | + timeout: withinBudget(navigationTimeoutMs), |
| 91 | + }); |
| 92 | + } catch (error) { |
| 93 | + if (!isTimeout(error)) { |
| 94 | + throw error; |
| 95 | + } |
| 96 | + return { |
| 97 | + consoleErrors, |
| 98 | + pageErrors, |
| 99 | + failedRequests, |
| 100 | + timedOut: true, |
| 101 | + pendingRequests: [...inFlight].map(r => r.url()), |
| 102 | + button: null, |
| 103 | + }; |
| 104 | + } |
| 105 | + let button; |
| 106 | + try { |
| 107 | + button = await page |
| 108 | + .getByRole('button') |
| 109 | + .first() |
| 110 | + .textContent({timeout: withinBudget(buttonTimeoutMs)}); |
| 111 | + } catch (error) { |
| 112 | + if (!isTimeout(error)) { |
| 113 | + throw error; |
| 114 | + } |
| 115 | + // No button within the wait: the page rendered nothing to press. |
| 116 | + button = null; |
| 117 | + } |
| 118 | + |
| 119 | + return { |
| 120 | + consoleErrors, |
| 121 | + pageErrors, |
| 122 | + failedRequests, |
| 123 | + timedOut: false, |
| 124 | + pendingRequests: [], |
| 125 | + button, |
| 126 | + }; |
| 127 | + } finally { |
| 128 | + await browser.close(); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Everything that made `result` unclean, one line each: the timeout and the |
| 134 | + * requests still in flight, each console error, page error and failed |
| 135 | + * request, and a missing button on a page that finished loading. Empty for a |
| 136 | + * clean render. |
| 137 | + */ |
| 138 | +export function uncleanFindings(result) { |
| 139 | + return [ |
| 140 | + ...(result.timedOut ? ['navigation timed out'] : []), |
| 141 | + ...result.pendingRequests.map(url => `still in flight: ${url}`), |
| 142 | + ...result.consoleErrors.map(entry => `console error: ${entry}`), |
| 143 | + ...result.pageErrors.map(entry => `page error: ${entry}`), |
| 144 | + ...result.failedRequests.map(entry => `failed request: ${entry}`), |
| 145 | + ...(!result.timedOut && !result.button ? ['rendered no button'] : []), |
| 146 | + ]; |
| 147 | +} |
| 148 | + |
| 149 | +/** Whether a render went idle with nothing wrong and a button on the page. */ |
| 150 | +export function isCleanRender(result) { |
| 151 | + return ( |
| 152 | + !result.timedOut && |
| 153 | + result.consoleErrors.length === 0 && |
| 154 | + result.pageErrors.length === 0 && |
| 155 | + result.failedRequests.length === 0 && |
| 156 | + Boolean(result.button) |
| 157 | + ); |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Run `render` until one comes back clean, at most `attempts` times, and |
| 162 | + * return the last result. With a `deadline` (in `now()`'s clock), each attempt |
| 163 | + * is handed `budgetMs`, an equal share of the time left (so the retry gets |
| 164 | + * whatever the first attempt left unused), and no attempt starts once the |
| 165 | + * deadline has passed. `onRetry(attempt, leftMs, discarded)` hears each |
| 166 | + * retry, with the unclean attempt it replaces. |
| 167 | + * |
| 168 | + * @template {{timedOut: boolean, consoleErrors: string[], pageErrors: string[], failedRequests: string[], button: string | null}} R |
| 169 | + * @param {(attempt: {budgetMs?: number}) => Promise<R>} render |
| 170 | + * @param {{ |
| 171 | + * attempts?: number, |
| 172 | + * deadline?: number, |
| 173 | + * now?: () => number, |
| 174 | + * onRetry?: (attempt: number, leftMs: number | undefined, discarded: R) => void, |
| 175 | + * }} [options] |
| 176 | + * @returns {Promise<R>} |
| 177 | + */ |
| 178 | +export async function renderUntilClean( |
| 179 | + render, |
| 180 | + {attempts = 2, deadline, now = Date.now, onRetry} = {}, |
| 181 | +) { |
| 182 | + const leftMs = () => (deadline == null ? undefined : deadline - now()); |
| 183 | + let result; |
| 184 | + for (let attempt = 1; attempt <= attempts; attempt++) { |
| 185 | + const left = leftMs(); |
| 186 | + result = await render({ |
| 187 | + budgetMs: |
| 188 | + left == null ? undefined : Math.floor(left / (attempts - attempt + 1)), |
| 189 | + }); |
| 190 | + if (isCleanRender(result) || attempt === attempts) break; |
| 191 | + const remaining = leftMs(); |
| 192 | + if (remaining != null && remaining <= 0) break; |
| 193 | + onRetry?.(attempt, remaining, result); |
| 194 | + } |
| 195 | + return result; |
| 196 | +} |
0 commit comments