|
| 1 | +import type { APIResponse, Page, Route } from '@playwright/test'; |
| 2 | + |
| 3 | +const DEFAULT_HOSTS = [/raw\.githubusercontent\.com/]; |
| 4 | + |
| 5 | +interface RetryRemoteFixturesOptions { |
| 6 | + /** Total attempts (including the first) before giving up. Default 4. */ |
| 7 | + attempts?: number; |
| 8 | + /** Host patterns to intercept. Default: raw.githubusercontent.com. */ |
| 9 | + hosts?: RegExp[]; |
| 10 | + /** Per-attempt fetch timeout in ms. Default 20000. */ |
| 11 | + perAttemptTimeoutMs?: number; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * DICOM fixtures for the loader examples are fetched over HTTP from |
| 16 | + * raw.githubusercontent.com — most images from the cornerstone3D repo, the |
| 17 | + * TG18 set from the external OHIF/viewer-testdata repo. Under the parallel |
| 18 | + * Playwright workers on the self-hosted runner, GitHub raw intermittently |
| 19 | + * rate-limits (429) or drops these requests. The loader issues a single |
| 20 | + * XMLHttpRequest per image with no retry (see |
| 21 | + * packages/dicomImageLoader/src/imageLoader/internal/xhrRequest.ts), so one |
| 22 | + * bad response fails the whole image load and surfaces as a flaky |
| 23 | + * `waitForImageRendered` timeout. |
| 24 | + * |
| 25 | + * Intercept those requests and retry them with exponential backoff from the |
| 26 | + * Node side via `route.fetch`, then replay the successful response to the |
| 27 | + * browser. Retries happen off the browser's single-shot XHR, the product code |
| 28 | + * is untouched, and the example's public URLs are left as-is so the deployed |
| 29 | + * docs demo is unaffected. Range requests are preserved because `route.fetch` |
| 30 | + * forwards the original request (headers included). |
| 31 | + * |
| 32 | + * Install in a spec's `beforeEach` BEFORE navigating to the example. |
| 33 | + */ |
| 34 | +export async function retryRemoteFixtures( |
| 35 | + page: Page, |
| 36 | + options: RetryRemoteFixturesOptions = {} |
| 37 | +): Promise<void> { |
| 38 | + const { |
| 39 | + attempts = 4, |
| 40 | + hosts = DEFAULT_HOSTS, |
| 41 | + perAttemptTimeoutMs = 20000, |
| 42 | + } = options; |
| 43 | + |
| 44 | + await page.route( |
| 45 | + (url) => hosts.some((host) => host.test(url.href)), |
| 46 | + async (route: Route) => { |
| 47 | + let lastResponse: APIResponse | undefined; |
| 48 | + |
| 49 | + for (let attempt = 0; attempt < attempts; attempt++) { |
| 50 | + try { |
| 51 | + const response = await route.fetch({ timeout: perAttemptTimeoutMs }); |
| 52 | + const status = response.status(); |
| 53 | + |
| 54 | + // Only 429 and 5xx are transient; anything else (2xx, 3xx, 4xx |
| 55 | + // other than 429) is a real answer we should replay immediately. |
| 56 | + if (status !== 429 && status < 500) { |
| 57 | + await route.fulfill({ response }); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + lastResponse = response; |
| 62 | + } catch { |
| 63 | + // Network error / timeout: fall through to backoff and retry. |
| 64 | + } |
| 65 | + |
| 66 | + // Backoff after every failed attempt except when we are about to give |
| 67 | + // up, so the caller's render-gate budget is not spent needlessly. |
| 68 | + if (attempt < attempts - 1) { |
| 69 | + const backoffMs = 500 * 2 ** attempt; |
| 70 | + await new Promise((resolve) => setTimeout(resolve, backoffMs)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Retries exhausted. Replay the last transient response if we have one, |
| 75 | + // otherwise let the request proceed so the real network error surfaces. |
| 76 | + if (lastResponse) { |
| 77 | + await route.fulfill({ response: lastResponse }); |
| 78 | + } else { |
| 79 | + await route.continue(); |
| 80 | + } |
| 81 | + } |
| 82 | + ); |
| 83 | +} |
0 commit comments