diff --git a/src/main/usage.ts b/src/main/usage.ts index a44bbcc..b918ff2 100644 --- a/src/main/usage.ts +++ b/src/main/usage.ts @@ -28,19 +28,23 @@ while ($true) { ` /** Asks a background PowerShell for CPU time and memory of some processes. */ +/** how long an answer from a PowerShell that is already running may take */ +export const ANSWER_TIMEOUT_MS = 10_000 +/** + * for the first answer of a new PowerShell, which also waits for it to start. That alone can take + * over 10 s on a busy or slow PC, and with the normal timeout every new PowerShell was killed + * before its first answer, so usage never showed up there at all + */ +export const START_TIMEOUT_MS = 30_000 + export class PowerShellQuery implements ProcessQuery { private ps: ChildProcessWithoutNullStreams | null = null private buf = '' private waiting: ((rows: ProcRow[]) => void) | null = null constructor( - private readonly timeoutMs = 10_000, - /** - * for the first question of a new PowerShell, which also waits for it to start. That alone can - * take over 10 s on a busy or slow PC, and with the normal timeout every new PowerShell was - * killed before its first answer, so usage never showed up there at all - */ - private readonly startTimeoutMs = 30_000 + private readonly timeoutMs = ANSWER_TIMEOUT_MS, + private readonly startTimeoutMs = START_TIMEOUT_MS ) {} query(pids: number[]): Promise { diff --git a/tests/usage.test.ts b/tests/usage.test.ts index 021f8a7..37066af 100644 --- a/tests/usage.test.ts +++ b/tests/usage.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' import type { VmUsage } from '@shared/ipc' -import { cpuPercent, PowerShellQuery, UsageSampler, type ProcessQuery, type ProcRow } from '../src/main/usage' +import { ANSWER_TIMEOUT_MS, cpuPercent, PowerShellQuery, START_TIMEOUT_MS, UsageSampler, type ProcessQuery, type ProcRow } from '../src/main/usage' describe('cpuPercent', () => { it('is zero on the first sample', () => { @@ -96,9 +96,19 @@ describe('UsageSampler', () => { }) }) +/** + * The app replaces a new PowerShell that hasn't answered within START_TIMEOUT_MS, which is right + * for the app: a desktop that slow has something wrong with it and the next sample tries again. A + * cold GitHub Windows runner does go over it often enough to fail a run, and these tests are about + * the answers rather than that timeout, so they allow three times as long. + */ +const START_MS = START_TIMEOUT_MS * 3 +/** longer than START_MS, so a slow start fails on the assertion and not on the test timing out */ +const TEST_MS = START_MS + 30_000 + describe.skipIf(process.platform !== 'win32')('PowerShellQuery', () => { it('reads CPU time and memory of a real process', async () => { - const q = new PowerShellQuery() + const q = new PowerShellQuery(ANSWER_TIMEOUT_MS, START_MS) try { const rows = await q.query([process.pid]) expect(rows).toHaveLength(1) @@ -110,20 +120,20 @@ describe.skipIf(process.platform !== 'win32')('PowerShellQuery', () => { } finally { q.close() } - }, 60_000) + }, TEST_MS) it('gives a new PowerShell longer to start than an answer may take', async () => { // 1 ms is far too short for any answer, but the first one only has to beat the start timeout - const q = new PowerShellQuery(1, 30_000) + const q = new PowerShellQuery(1, START_MS) try { expect(await q.query([process.pid])).toHaveLength(1) } finally { q.close() } - }, 60_000) + }, TEST_MS) it('a closed PowerShell exiting late does not answer the next question', async () => { - const q = new PowerShellQuery() + const q = new PowerShellQuery(ANSWER_TIMEOUT_MS, START_MS) try { await q.query([process.pid]) // starts a PowerShell q.close() // killed, its exit event comes a bit later @@ -132,7 +142,8 @@ describe.skipIf(process.platform !== 'win32')('PowerShellQuery', () => { } finally { q.close() } - }, 90_000) + // two cold starts in a row + }, TEST_MS * 2) it('gives up on a PowerShell that does not answer in time', async () => { const q = new PowerShellQuery(1, 1) // no PowerShell starts in 1 ms