Skip to content

[WIP] Refactor unit tests to avoid real time.Sleep#2595

Closed
Copilot wants to merge 1 commit intomainfrom
copilot/refactor-slow-unit-tests
Closed

[WIP] Refactor unit tests to avoid real time.Sleep#2595
Copilot wants to merge 1 commit intomainfrom
copilot/refactor-slow-unit-tests

Conversation

Copy link

Copilot AI commented Mar 19, 2026

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original prompt

This section details on the original issue you should resolve

<issue_title>chore: unit tests use real time.Sleep causing slow test suite</issue_title>
<issue_description>## 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.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@linux-foundation-easycla
Copy link

CLA Not Signed

@chmouel chmouel closed this Mar 19, 2026
@chmouel chmouel deleted the copilot/refactor-slow-unit-tests branch March 19, 2026 07:35
Copilot AI requested a review from chmouel March 19, 2026 07:35
Copilot stopped work on behalf of chmouel due to an error March 19, 2026 07:35
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.

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

2 participants