test: make desktop orphan-reap deadline checks deterministic

This commit is contained in:
Teknium
2026-09-07 17:02:05 -07:00
parent 5280fe9987
commit aa1a546a22
2 changed files with 22 additions and 5 deletions
@@ -178,16 +178,19 @@ test('startup reap preserves failed stops for the next launch', async () => {
assert.deepEqual(parseBackendOwnership(store.value()), [entry])
})
test('startup reap stops at the deadline and preserves the unprocessed records', async () => {
test('startup reap stops at the deadline and preserves the unprocessed records', async ({ onTestFinished }) => {
// Advance only when the first probe completes, never with runner scheduling.
const now = vi.spyOn(Date, 'now').mockReturnValue(0)
onTestFinished(() => now.mockRestore())
const first = ownershipEntry({ pid: 60 })
const second = ownershipEntry({ pid: 61 })
const store = memoryStore(stored([first, second]))
const stop = vi.fn()
const ownership = createOwnership(store, {
// Each probe is slow enough to blow a 1ms budget after the first entry.
// Exhaust the budget exactly at the boundary after processing one entry.
matchesIdentity: async () => {
await new Promise(resolve => setTimeout(resolve, 10))
now.mockReturnValue(1)
return false
},
@@ -201,7 +204,9 @@ test('startup reap stops at the deadline and preserves the unprocessed records',
assert.deepEqual(parseBackendOwnership(store.value()), [second])
})
test('startup reap preserves would-be-reaped records when the budget runs out', async () => {
test('startup reap preserves would-be-reaped records when the budget runs out', async ({ onTestFinished }) => {
const now = vi.spyOn(Date, 'now').mockReturnValue(0)
onTestFinished(() => now.mockRestore())
const first = ownershipEntry({ pid: 62 })
const second = ownershipEntry({ pid: 63 })
const store = memoryStore(stored([first, second]))
@@ -209,7 +214,7 @@ test('startup reap preserves would-be-reaped records when the budget runs out',
const ownership = createOwnership(store, {
matchesIdentity: async () => {
await new Promise(resolve => setTimeout(resolve, 10))
now.mockReturnValue(1)
return true
},
+12
View File
@@ -0,0 +1,12 @@
// Replay scheduling pressure without changing Date.now() results:
// NODE_OPTIONS="--require=$PWD/evals/desktop-reap-clock-pressure.cjs" \
// npm run test:desktop:platforms -w apps/desktop -- electron/backend-ownership.test.ts
// The old 1ms fixtures can expire before their first record is processed.
const now = Date.now.bind(Date)
const gate = new Int32Array(new SharedArrayBuffer(4))
Date.now = function () {
if (new Error().stack.includes('/electron/backend-ownership.ts:')) {
Atomics.wait(gate, 0, 0, 5)
}
return now()
}