Skip to content

tests: speed up timer-based unit tests#1738

Merged
Bobronium merged 2 commits into
mainfrom
dryers-compress-ninepins
Jun 10, 2026
Merged

tests: speed up timer-based unit tests#1738
Bobronium merged 2 commits into
mainfrom
dryers-compress-ninepins

Conversation

@rosetta-livekit-bot

@rosetta-livekit-bot rosetta-livekit-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • use Vitest fake timers in existing timer-heavy unit tests instead of waiting on wall-clock time
  • speed up generation tool cleanup, connection pool expiry/abort/prewarm, and utility timeout/pending-state tests
  • keep existing assertions only; no new tests added

Ported from livekit/agents#5980's virtual-time approach where applicable. I also checked other real-time waits and left process/socket/real-stream deadlock guards on real timers because fake timers would mask the behavior under test.

Benchmarks

Command: pnpm exec vitest run agents/src/connection_pool.test.ts agents/src/utils.test.ts agents/src/voice/generation_tools.test.ts --reporter=dot

  • Before (parent commit): Vitest duration 8.36s, test time 9.18s
  • After: Vitest duration 2.26s, test time 540ms
  • Slowest converted test: generation_tools long tool cleanup path went from ~6s to ~30ms in verbose output

Testing

  • pnpm exec vitest run agents/src/connection_pool.test.ts agents/src/utils.test.ts agents/src/voice/generation_tools.test.ts --reporter=verbose
  • pnpm exec vitest run agents/src/connection_pool.test.ts agents/src/utils.test.ts agents/src/voice/generation_tools.test.ts --reporter=dot
  • pnpm exec vitest run agents/src
  • pnpm exec prettier --check agents/src/connection_pool.test.ts agents/src/utils.test.ts agents/src/voice/generation_tools.test.ts

@changeset-bot

changeset-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 8516783

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@rosetta-livekit-bot rosetta-livekit-bot Bot requested a review from Bobronium June 9, 2026 09:29

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@Bobronium Bobronium left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asked Claude to measure the speed, looks like a real improvement! Props to @rosetta-livekit-bot 👏

Metric main (real timers) PR #1738 (fake timers) Speedup
Test execution time 6.03 s ~43 ms ~140×
Wall-clock total ~7.72 s ~1.75 s ~4.4×
rest of the tokens

All 9 tests still pass with identical assertions in both versions.

Why

The single test should not abort tool when preamble forwarders are cleaned up was the entire cost. It used real delay() calls that the test had to actually wait out — dominated by a tool with await delay(6000), plus a 100 ms wait and a 5 s cancelAndWait budget. The PR swaps those real waits for Vitest fake timers (vi.useFakeTimers() + vi.advanceTimersByTimeAsync(...)), so the 6 s tool delay resolves instantly in virtual time. Test compute collapses from 6030 ms to ~43 ms.

Caveats

  • The wall-clock improvement is "only" ~4.4× because ~1.7 s of fixed overhead remains (transform 0.86 s, import 1.3 s, setup 0.22 s) — Vite/esbuild compilation and module loading that fake timers can't touch. So the headline number depends on whether you measure the tests line (~140×) or total Duration (~4.4×).
  • The other 8 tests were already fast; the win is entirely concentrated in that one timing-heavy test.
  • This is a strictly test-only change (+11/-3, one file) — no production code or behavior affected.

Bottom line: the speedup is real and substantial.

@Bobronium Bobronium requested a review from toubatbrian June 10, 2026 10:45
@rosetta-livekit-bot rosetta-livekit-bot Bot changed the title tests: speed up generation tools test tests: speed up timer-based unit tests Jun 10, 2026
@Bobronium Bobronium merged commit 05c7eab into main Jun 10, 2026
9 checks passed
@Bobronium Bobronium deleted the dryers-compress-ninepins branch June 10, 2026 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant