Skip to content

chore: unit tests use real time.Sleep causing slow test suite #2594

@chmouel

Description

@chmouel

Summary

Analysis of the unit test suite revealed several tests using real time.Sleep calls that are artificially inflating test suite duration (~10+ seconds total). These should be refactored to use injectable clocks or context-aware blocking.

Slow Tests Identified

Duration Test Location
~4.5s TestProviderCreateStatusCommitRetryOnTransientError pkg/provider/gitea/gitea.go:303
~2.0s × 2 TestAnalyzeTimeout (gemini + openai) pkg/llm/providers/gemini/client_test.go:466, pkg/llm/providers/openai/client_test.go:482
~3.0s TestGetPullRequestsWithCommit pkg/provider/github/parse_payload_test.go

Root Causes and Suggested Fixes

1. Gitea retry sleep (pkg/provider/gitea/gitea.go:303)

The retry loop sleeps time.Duration(i+1) * 500ms between attempts. In tests, this real sleep accumulates to ~4.5s.

Fix: Make the sleep duration injectable (e.g., via a field on the provider struct or a function variable), defaulting to 500ms in production but overridable to 0 or 1ms in tests.

2. LLM timeout mock handlers

pkg/llm/providers/gemini/client_test.go:466 and pkg/llm/providers/openai/client_test.go:482 use time.Sleep(2 * time.Second) inside HTTP mock server handlers to simulate slow responses for timeout tests.

Fix: Replace time.Sleep(2s) with a context-aware block and reduce the timeout threshold so a much shorter sleep (e.g., 100ms) triggers the timeout:

select {
case <-r.Context().Done():
    return
case <-time.After(100 * time.Millisecond):
}

3. TestGetPullRequestsWithCommit HTTP overhead

This test has ~3s of HTTP mock overhead. Worth investigating whether responses can be served more efficiently or whether the test structure can be simplified.

Impact

Fixing the top two issues alone would save ~8.5s per test run. With parallel test execution, actual wall-clock savings depend on scheduling, but reducing artificial sleeps always improves test suite reliability and speed.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions