Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | 40x 40x 233346x 233346x 233346x 939x 939x 939x 939x 939x 939x 237462x 237462x 237462x 237443x 237443x 939x 232408x 232408x 725267x 725267x 725267x 317923x 232408x 939x 2613559x 102x 233139x 1x 4322x 940x 940x 940x 940x 940x 933x 5x 940x 940x 233346x 233346x 232408x 232408x 201x 201x 1x 200x 232207x 232207x 232207x 1x 232206x 938x 940x 940x 939x | import { setEngineScheduler, type EngineScheduler } from '../engine-clock.ts';
/**
* Test support: a virtual clock the engine can run a play on (`setEngineScheduler`). Time only moves when the play has
* nothing left to do but wait: {@link VirtualClock.run} lets every pending microtask settle, then jumps to the earliest
* timer and fires it. Timers fire exactly when due, in the order they are due (then in the order they were set), so a
* play on this clock is deterministic and takes no longer than its CPU time.
*/
export interface VirtualClock extends EngineScheduler {
/**
* Runs `play` on this clock until it settles, and resolves or rejects as it does. The engine reads its time from the
* clock only while a run is going on.
*/
run<T>(play: () => Promise<T>): Promise<T>;
/** Calls `callback` at virtual time `atMs` (now if that has passed). */
at(atMs: number, callback: () => void): () => void;
}
export interface VirtualClockOptions {
/** `wallClockNow()` at virtual time 0. */
wallClockOriginMs?: number;
/** A run that reaches this virtual time fails, so a play that never ends cannot hang the test. Default 10 minutes. */
limitMs?: number;
}
interface VirtualTimer {
atMs: number;
seq: number;
callback: () => void;
}
/** Real turns of the event loop a run waits for something outside the clock (a module load, say) before it fails. */
const MAX_IDLE_TURNS = 200;
const hostTimers = globalThis as {
setImmediate?: (callback: () => void) => unknown;
};
/** One real turn of the event loop: every pending microtask has run by the time it resolves. */
function nextTurn(): Promise<void> {
return new Promise((resolve) => {
if (typeof hostTimers.setImmediate === 'function') {
hostTimers.setImmediate(resolve);
} else E{
setTimeout(resolve, 0);
}
});
}
export function createVirtualClock(options: VirtualClockOptions = {}): VirtualClock {
const wallClockOriginMs = options.wallClockOriginMs ?? 1_700_000_000_000;
const limitMs = options.limitMs ?? 600_000;
let nowMs = 0;
let nextSeq = 0;
const timers: VirtualTimer[] = [];
const schedule = (atMs: number, callback: () => void): (() => void) => {
const timer: VirtualTimer = { atMs: Math.max(nowMs, atMs), seq: nextSeq++, callback };
timers.push(timer);
return () => {
const index = timers.indexOf(timer);
if (index >= 0) timers.splice(index, 1);
};
};
const takeNextTimer = (): VirtualTimer | undefined => {
let best = -1;
for (let index = 0; index < timers.length; index += 1) {
const timer = timers[index]!;
const current = timers[best];
if (
current === undefined ||
timer.atMs < current.atMs ||
(timer.atMs === current.atMs && timer.seq < current.seq)
) {
best = index;
}
}
return best < 0 ? undefined : timers.splice(best, 1)[0];
};
const clock: VirtualClock = {
now: () => nowMs,
wallClockNow: () => wallClockOriginMs + nowMs,
setTimeout: (callback, ms) => schedule(nowMs + Math.max(0, Number.isFinite(ms) ? ms : 0), callback),
setImmediate: (callback) => schedule(nowMs, callback),
at: (atMs, callback) => schedule(atMs, callback),
run: async (play) => {
setEngineScheduler(clock);
try {
let settled = false;
const result = play();
result.then(
() => {
settled = true;
},
() => {
settled = true;
},
);
let idleTurns = 0;
while (!settled) {
await nextTurn();
if (settled) break;
const timer = takeNextTimer();
if (timer === undefined) {
idleTurns += 1;
if (idleTurns > MAX_IDLE_TURNS) {
throw new Error('The play is waiting, but nothing is scheduled on the virtual clock.');
}
continue;
}
idleTurns = 0;
nowMs = Math.max(nowMs, timer.atMs);
if (nowMs > limitMs) {
throw new Error(`The play ran past ${limitMs} ms of virtual time.`);
}
timer.callback();
}
return await result;
} finally {
timers.length = 0;
setEngineScheduler(undefined);
}
},
};
return clock;
}
|