Skip to content

Commit 70b86fa

Browse files
committed
test: synchronize Apple deadline journal regression
1 parent 9c61f5a commit 70b86fa

1 file changed

Lines changed: 48 additions & 19 deletions

File tree

‎packages/platform-apple/src/core/__tests__/runner-host-deadline-journal.test.ts‎

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { RunnerCommandAccounting, type RunnerSession } from '../../runner/runner
77
import { IOS_SIMULATOR } from '../../runner/__tests__/device-fixtures.ts';
88
import { startFakeRunnerServer } from '../../runner/__tests__/fake-runner-server.ts';
99
import { installAppleRunnerTestHost, loadAppleRunnerHost } from '../../runner/test-host.ts';
10-
import { test } from 'vitest';
10+
import { test, vi } from 'vitest';
1111

1212
test('a daemon deadline follows the ordinary canceled Apple runner journal drain', async () => {
1313
installAppleRunnerTestHost(await loadAppleRunnerHost());
@@ -18,6 +18,19 @@ test('a daemon deadline follows the ordinary canceled Apple runner journal drain
1818
{ kind: 'ok', data: { lifecycleState: 'completed' } },
1919
],
2020
});
21+
vi.useFakeTimers({ toFake: ['Date'] });
22+
const realSetTimeout = globalThis.setTimeout;
23+
let fireDeadline: (() => void) | undefined;
24+
const timerSpy = vi
25+
.spyOn(globalThis, 'setTimeout')
26+
.mockImplementation((callback, delay, ...args) => {
27+
if (fireDeadline === undefined) {
28+
assert.equal(delay, 25);
29+
fireDeadline = () => callback(...args);
30+
return realSetTimeout(() => {}, delay);
31+
}
32+
return realSetTimeout(callback, delay, ...args);
33+
});
2134
try {
2235
const session: RunnerSession = {
2336
sessionId: `deadline-journal:${runner.port}`,
@@ -33,25 +46,39 @@ test('a daemon deadline follows the ordinary canceled Apple runner journal drain
3346
};
3447
const requestId = `deadline-journal:${runner.port}`;
3548

49+
const deadlineResult = withRequestDeadline(requestId, 25, async () => {
50+
await executeRunnerExchange(
51+
IOS_SIMULATOR,
52+
session,
53+
{
54+
command: 'longPress',
55+
x: 10,
56+
y: 10,
57+
durationMs: 5_000,
58+
appBundleId: 'com.example.app',
59+
},
60+
undefined,
61+
10_000,
62+
async () => {},
63+
getRequestSignal(requestId),
64+
);
65+
return { ok: true };
66+
});
67+
deadlineResult.catch(() => {});
68+
69+
const giveUpAtMs = performance.now() + 2_000;
70+
while (!runner.requests.some((request) => request.command === 'longPress')) {
71+
if (performance.now() >= giveUpAtMs) {
72+
throw new Error('The fake runner did not admit longPress before the deadline advanced.');
73+
}
74+
await new Promise<void>((resolve) => setImmediate(resolve));
75+
}
76+
assert.ok(fireDeadline);
77+
vi.setSystemTime(Date.now() + 25);
78+
fireDeadline();
79+
3680
await assert.rejects(
37-
withRequestDeadline(requestId, 25, async () => {
38-
await executeRunnerExchange(
39-
IOS_SIMULATOR,
40-
session,
41-
{
42-
command: 'longPress',
43-
x: 10,
44-
y: 10,
45-
durationMs: 5_000,
46-
appBundleId: 'com.example.app',
47-
},
48-
undefined,
49-
10_000,
50-
async () => {},
51-
getRequestSignal(requestId),
52-
);
53-
return { ok: true };
54-
}),
81+
deadlineResult,
5582
(error: unknown) =>
5683
error instanceof AppError && error.details?.reason === 'daemon_request_timeout',
5784
);
@@ -66,6 +93,8 @@ test('a daemon deadline follows the ordinary canceled Apple runner journal drain
6693
);
6794
assert.equal(session.commandCharges.hasOutstandingCharges, false);
6895
} finally {
96+
timerSpy.mockRestore();
97+
vi.useRealTimers();
6998
await runner.close();
7099
}
71100
});

0 commit comments

Comments
 (0)