Skip to content

chore: replace real time.Sleep with clockwork in unit tests#2627

Merged
chmouel merged 1 commit intotektoncd:mainfrom
chmouel:fix-slow-unit-tests-clockwork
Apr 1, 2026
Merged

chore: replace real time.Sleep with clockwork in unit tests#2627
chmouel merged 1 commit intotektoncd:mainfrom
chmouel:fix-slow-unit-tests-clockwork

Conversation

@chmouel
Copy link
Copy Markdown
Member

@chmouel chmouel commented Mar 31, 2026

Summary

Fixes #2594

  • Inject clockwork.FakeClock into Gitea and GitHub provider structs so retry/backoff loops use a fake clock in tests instead of real time.Sleep/time.After
  • Reduce LLM timeout test mock handler sleep from 2s to 200ms (context timeout is 100ms, so timeout still triggers correctly)
  • Total savings: ~10-13s per test run across 4 slow tests

Changes

Area Before After Approach
Gitea retry (createStatusCommit) ~4.5s ~0.01s Added clock field + getClock() lazy helper to Provider
GitHub backoff (getPullRequestsWithCommit) ~3-7s ~0.01s Same pattern, added clock field to Provider
Gemini TestAnalyzeTimeout ~2s ~0.2s Reduced mock sleep duration
OpenAI TestAnalyzeTimeout ~2s ~0.2s Reduced mock sleep duration

Test plan

  • go test ./pkg/provider/gitea/ — all pass
  • go test ./pkg/provider/github/ — all pass
  • go test ./pkg/llm/providers/gemini/ — all pass
  • go test ./pkg/llm/providers/openai/ — all pass
  • golangci-lint run — 0 issues
  • Pre-commit hooks — all pass

🤖 Generated with Claude Code

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the clockwork library to the Gitea and GitHub providers to facilitate deterministic testing of retry and backoff logic. By replacing direct time package calls with a clock interface, tests can now use a fake clock to advance time instantly. Additionally, the PR reduces sleep durations in LLM provider tests to improve execution speed. The review feedback identifies a thread-safety issue in the lazy initialization of the clock field within both the Gitea and GitHub provider implementations, recommending a change to avoid potential data races when the clock is not explicitly set.

@chmouel chmouel force-pushed the fix-slow-unit-tests-clockwork branch from da2cf2f to 6fcd382 Compare March 31, 2026 13:54
Inject clockwork.FakeClock into Gitea and GitHub provider retry/backoff
loops so tests don't wait on real sleeps (~10s saved). Reduce LLM
timeout test mock sleeps from 2s to 200ms.

Fixes tektoncd#2594

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
@chmouel chmouel force-pushed the fix-slow-unit-tests-clockwork branch from 6fcd382 to 49d8cbf Compare March 31, 2026 20:50
@chmouel chmouel merged commit 02a9f6a into tektoncd:main Apr 1, 2026
20 of 21 checks passed
@chmouel chmouel deleted the fix-slow-unit-tests-clockwork branch April 1, 2026 10:14
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