Skip to content
Merged
Changes from 1 commit
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
18 changes: 15 additions & 3 deletions src/react/components/ui/code-block.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,27 @@ async function settle(): Promise<void> {
* `clipboard.writeText` falls back to `execCommand`, and only once that returns
* false does the failed state get set. That chain is several ticks long, so on
* a loaded machine the assertion could run against the pre-failure render. Poll
* for the state the test is actually about instead of guessing a tick count.
* for the state the test is actually about, against a wall-clock deadline
* rather than an iteration count: under load each poll costs more, so a fixed
* number of attempts is an arbitrary proxy for how long the test is willing to
* wait.
*/
const WAIT_FOR_TIMEOUT_MS = 2_000;

async function waitFor(predicate: () => boolean, description: string): Promise<void> {
for (let attempt = 0; attempt < 100; attempt += 1) {
const deadline = Date.now() + WAIT_FOR_TIMEOUT_MS;
Comment thread
kojiwakayama marked this conversation as resolved.
Outdated

for (;;) {
flushSync(() => {});
if (predicate()) return;
// Checked after the predicate, so a state that lands exactly on the
// deadline still counts, and before the sleep, so a failed final check
// does not pay for a tick it will never use.
if (Date.now() >= deadline) {
throw new Error(`Timed out after ${WAIT_FOR_TIMEOUT_MS}ms waiting for ${description}`);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}
await new Promise((resolve) => setTimeout(resolve, 1));
}
throw new Error(`Timed out waiting for ${description}`);
}

/**
Expand Down