Skip to content
Merged
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
28 changes: 26 additions & 2 deletions packages/runtime-host/src/__tests__/event-wait-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { mkdtemp, rm, readFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { test, type TestContext } from 'node:test';
import { setTimeout as delay } from 'node:timers/promises';
import {
EVENT_WAIT_LIMITS,
eventWaitDeliveryKey,
Expand Down Expand Up @@ -55,9 +56,12 @@ async function settle() {
for (let n = 0; n < 40; n++) await new Promise<void>((resolve) => setImmediate(resolve));
}
async function eventually(check: () => Promise<boolean> | boolean) {
for (let n = 0; n < 100; n++) {
// Host I/O needs wall-clock time; immediate turns can exhaust before it completes.
// Keep the injected observation clock fixed so timeout/Retry-After assertions remain exact.
const deadline = performance.now() + 5_000;
while (performance.now() < deadline) {
if (await check()) return;
await settle();
await delay(5);
}
assert.fail('Condition did not settle without advancing the observation clock');
}
Expand Down Expand Up @@ -94,6 +98,26 @@ class Clock implements EventWaitClock {
await settle();
}
}
test('eventually waits for delayed host work without advancing the observation clock', async () => {
const clock = new Clock();
let observationTimerRan = false;
clock.schedule(() => {
observationTimerRan = true;
}, 1);
let completed = false;
const work = delay(200).then(() => {
completed = true;
});
try {
await eventually(() => completed);
assert.equal(clock.now(), 1_000);
assert.equal(observationTimerRan, false);
assert.equal(clock.timers.size, 1);
} finally {
await work;
}
});

const satisfied = (key = 'event-1'): EventWaitObservationResult => ({
kind: 'satisfied',
receiptKey: key,
Expand Down