Skip to content

Commit c36b9a7

Browse files
committed
test(sandbox): time the unchanged-CLI warm path on a controlled clock
1 parent e005ac1 commit c36b9a7

2 files changed

Lines changed: 28 additions & 14 deletions

File tree

‎apps/sim/lib/execution/remote-sandbox/phase-timing.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface SandboxPhaseTimer {
2424
start(phase: SandboxPhase): () => void
2525
}
2626

27+
/** Creates a timer whose callbacks record rounded milliseconds; unfinished phases stay absent. */
2728
export function createSandboxPhaseTimer(): SandboxPhaseTimer {
2829
const timings: SandboxPhaseTimings = {}
2930
return {

‎apps/sim/lib/execution/remote-sandbox/session-sandbox.test.ts‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -823,29 +823,42 @@ describe('session sandbox lease', () => {
823823

824824
it('attributes a warm shell call’s wall time to the phase that spent it', async () => {
825825
const { handle } = fakeSandbox('phase-timing')
826+
mockFindSessionSandbox.mockResolvedValue(handle)
827+
const request = {
828+
code: 'true',
829+
envs: {},
830+
timeoutMs: 10_000,
831+
session: { key: 'phase-timing', cli: CLI },
832+
}
833+
await executeShellInSandbox(request)
834+
835+
let nowMs = 0
836+
const clock = vi.spyOn(performance, 'now').mockImplementation(() => nowMs)
837+
const writes = vi.fn(handle.writeFile.bind(handle))
838+
handle.writeFile = writes
826839
const runCommand = handle.runCommand.bind(handle)
827840
handle.runCommand = async (command, options) => {
828-
await sleep(options.envs?.SIM_CLI_PATH ? 30 : 60)
841+
nowMs += options.envs?.SIM_CLI_PATH ? 30 : 60
829842
return runCommand(command, options)
830843
}
831844
mockFindSessionSandbox.mockImplementation(async () => {
832-
await sleep(40)
845+
nowMs += 40
833846
return handle
834847
})
835848

836-
const result = await executeShellInSandbox({
837-
code: 'true',
838-
envs: {},
839-
timeoutMs: 10_000,
840-
session: { key: 'phase-timing', cli: CLI },
841-
})
849+
try {
850+
const result = await executeShellInSandbox(request)
842851

843-
const phases = result.phaseMs ?? {}
844-
expect(result.sandboxSession).toBe('reused')
845-
expect(phases.lookup).toBeGreaterThanOrEqual(38)
846-
expect(phases.cli).toBeGreaterThanOrEqual(28)
847-
expect(phases.run).toBeGreaterThanOrEqual(58)
848-
expect(phases.lease).toBeGreaterThanOrEqual((phases.lookup ?? 0) + (phases.cli ?? 0))
852+
const phases = result.phaseMs ?? {}
853+
expect(result.sandboxSession).toBe('reused')
854+
expect(writes).not.toHaveBeenCalled()
855+
expect(phases.lookup).toBe(40)
856+
expect(phases.cli).toBe(30)
857+
expect(phases.run).toBe(60)
858+
expect(phases.lease).toBeGreaterThanOrEqual(70)
859+
} finally {
860+
clock.mockRestore()
861+
}
849862
})
850863

851864
it('pins shell CLI resolution ahead of caller environment paths', async () => {

0 commit comments

Comments
 (0)