Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 11 additions & 7 deletions src/main/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcRow[]> {
Expand Down
25 changes: 18 additions & 7 deletions tests/usage.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading