Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/renderers/testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,12 @@ test('cards never overlap while scrubbing', async ({page, inspector}) => {
`'after'`: one held pane of side by side or swipe, as a host presents it at
progress 0 or 1), `width`, and `height` (a fixed frame height the frame fills,
as a tab or pane gives one; by default the frame follows the height the
renderer asks for). It resolves once the renderer has painted.
renderer asks for). It resolves once the renderer has painted. In WebKit,
which keeps every removed frame until its page navigates, it reloads the stage
page after it has started 8 frames, so a spec that loads many scenarios in one
page stays bounded in memory; page state from before is gone. `checkScenario`
likewise runs WebKit's checks three at a time, each batch in a fresh stage
page.
- The handle: `frame` (the renderer document), `iframe`, `subject`,
`seek(progress, options?)` (resolves after the renderer acknowledges that
exact progress), `scrub(progresses, check)`, `select(id)`, `hover(id)`,
Expand Down
3 changes: 2 additions & 1 deletion packages/renderers/testing/playwright/bounded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type {StageState} from '../src/types.js'
* runner, so a request still unanswered after this is stuck: a renderer that never acknowledged a paint (`seek` waits
* for it with no bound of its own), or a page that no longer runs. The bound is in the test process because the page's
* own timers can't fire in the second case: WebKit runs the sandboxed renderer frame in the host page's process,
* so a renderer stuck in its document stops the host's event loop with it.
* so a renderer stuck in its document, or a web process exhausted by the frames WebKit keeps (`framesPerStagePage`),
* stops the host's event loop with it.
*/
export const stageRequestTimeoutMs = 90_000

Expand Down
52 changes: 52 additions & 0 deletions packages/renderers/testing/playwright/frames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type {Frame, Page} from '@playwright/test'

/**
* The renderer frames a stage page starts before WebKit gets a fresh one. Every load that doesn't reuse its frame
* starts a new one, and WebKit keeps each frame the page has removed, with its document and its script heap, until
* the page itself navigates: a few megabytes for a small renderer, tens for a large graph. A spec that loads scenario
* after scenario in one page grows the web process (which also runs the sandboxed frames) by that much per load until
* it stalls collecting garbage or the system kills it, which shows as a stage that reports ready and then a page whose
* event loop never runs again, and then a closed target (githubnext/projections#151).
*/
export const framesPerStagePage = 8

/**
* The built-in checks `checkScenario` runs in one request in WebKit: they start their frames inside the page, two to
* five each, where the stage page can't start over, so it starts over before each batch that follows any frame.
*/
export const checksPerStageRequest = 3

/** The page events `framesSinceNavigation` follows, and what it reads from the page. */
export type FramePage = Pick<Page, 'on' | 'mainFrame' | 'context'>

const counts = new WeakMap<FramePage, {frames: number}>()

/** The frames attached in `page` since its main frame last navigated, counted from the first call for that page. */
export function framesSinceNavigation(page: FramePage): number {
let count = counts.get(page)
if (!count) {
const counted = {frames: 0}
page.on('frameattached', () => {
counted.frames++
})
page.on('framenavigated', (frame: Frame) => {
if (frame === page.mainFrame()) counted.frames = 0
})
counts.set(page, counted)
count = counted
}
return count.frames
}

/** Whether `page`'s engine keeps the frames it removed until the page navigates (WebKit's does). */
export function keepsRemovedFrames(page: FramePage): boolean {
return page.context().browser()?.browserType().name() === 'webkit'
}

/**
* Whether the stage in `page` should start over in a fresh document before it starts more renderer frames: once it
* has started `limit` since it navigated.
*/
export function stageNeedsFreshPage(page: FramePage, limit = framesPerStagePage): boolean {
return framesSinceNavigation(page) >= Math.max(1, limit) && keepsRemovedFrames(page)
}
27 changes: 24 additions & 3 deletions packages/renderers/testing/playwright/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {FilterPayload, RendererOptions} from '@githubnext/projections-rende
import type {FixturesInspector, InspectorOptions} from './server.js'
import {startInspector} from './server.js'
import {bounded, StageRecord} from './bounded.js'
import {checksPerStageRequest, framesPerStagePage, framesSinceNavigation, keepsRemovedFrames, stageNeedsFreshPage} from './frames.js'
import {allChecks} from '../src/checks/catalog.js'
import type {
CheckId,
CheckResult,
Expand Down Expand Up @@ -50,9 +52,14 @@ export function scenarioKeys(path: string): string[] {
return (document.scenarios ?? []).map(scenario => `${source}/${scenario.id}`)
}

async function stagePage(page: Page, inspector: FixturesInspector): Promise<void> {
/** Opens the stage page, or starts it over in WebKit once it has started `frames` renderer frames. */
async function stagePage(page: Page, inspector: FixturesInspector, frames = framesPerStagePage): Promise<void> {
if (!page.url().startsWith(inspector.page({view: 'stage'}).split('#')[0]!)) {
framesSinceNavigation(page)
await page.goto(inspector.page({view: 'stage'}))
} else if (stageNeedsFreshPage(page, frames)) {
// A fresh document releases the frames WebKit kept (see `framesPerStagePage`).
await page.reload()
}
await page.waitForFunction(
() => document.documentElement.dataset.projectionsStage === 'ready' || document.documentElement.dataset.projectionsStage === 'failed',
Expand Down Expand Up @@ -214,8 +221,22 @@ export async function checkScenario(
scenario: string,
options: {checks?: CheckId[]} = {},
): Promise<CheckResult[]> {
await stagePage(page, inspector)
return page.evaluate(([scenario, options]) => window.projectionsStage.runChecks(scenario, options), [scenario, options] as const)
// The checks start their frames in the page, out of `stagePage`'s reach: in WebKit they run a few at a time, and
// the page starts over between them (one suite for a large graph would otherwise keep over a gigabyte of frames).
const ids = options.checks ?? allChecks
const batch = keepsRemovedFrames(page) ? checksPerStageRequest : ids.length
const results: CheckResult[] = []
for (let start = 0; start < ids.length; start += batch) {
await stagePage(page, inspector, 1)
const checks = ids.slice(start, start + batch)
results.push(
...(await page.evaluate(([scenario, options]) => window.projectionsStage.runChecks(scenario, options), [
scenario,
{...options, checks},
] as const)),
)
}
return results
}

/** The failures in check results, one line each, for assertion messages. */
Expand Down
37 changes: 37 additions & 0 deletions packages/renderers/testing/test/helpers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -668,3 +668,40 @@ test('a stage request the page never answers fails, naming it, the scenario and
/status ready, renderer graph, diff, progress 1, painted 1, asked for 1217px; it last answered state/.test(error.message),
)
})

/** A page that emits Playwright's frame events, in an engine named `engine`. */
function fakePage(engine) {
const listeners = {frameattached: [], framenavigated: []}
const main = {name: 'main'}
return {
main,
on: (event, listener) => listeners[event].push(listener),
mainFrame: () => main,
context: () => ({browser: () => ({browserType: () => ({name: () => engine})})}),
emit: (event, frame) => listeners[event].forEach(listener => listener(frame)),
}
}

test('a WebKit stage page starts over once it has started framesPerStagePage frames since it navigated', async () => {
const {framesPerStagePage, framesSinceNavigation, stageNeedsFreshPage} = await import('../dist/playwright/frames.js')
const webkit = fakePage('webkit')
assert.equal(framesSinceNavigation(webkit), 0)
for (let index = 0; index < framesPerStagePage - 1; index++) webkit.emit('frameattached', {})
// A renderer frame navigating (to its opened document) is not the page navigating.
webkit.emit('framenavigated', {})
assert.equal(stageNeedsFreshPage(webkit), false)
webkit.emit('frameattached', {})
assert.equal(stageNeedsFreshPage(webkit), true)
webkit.emit('framenavigated', webkit.main)
assert.equal(framesSinceNavigation(webkit), 0)
assert.equal(stageNeedsFreshPage(webkit), false)
// Between check batches, any frame is one too many.
assert.equal(stageNeedsFreshPage(webkit, 1), false)
webkit.emit('frameattached', {})
assert.equal(stageNeedsFreshPage(webkit, 1), true)

const chromium = fakePage('chromium')
framesSinceNavigation(chromium)
for (let index = 0; index < framesPerStagePage * 4; index++) chromium.emit('frameattached', {})
assert.equal(stageNeedsFreshPage(chromium), false)
})
Loading