fix(test): Use synctest to deflake timer-based tests.#21957
fix(test): Use synctest to deflake timer-based tests.#21957
Conversation
The race detector adds 5-10x overhead to memory operations. Tests with tight absolute timing margins (100ms windows) fail intermittently under -race or CI CPU pressure. - promwrapper: ceiling 700ms → 2000ms (600ms sleep + race overhead) - transmission: widen upper bounds by 200ms (100ms windows → 300ms) Fixes: CORE-2382
|
✅ No conflicts with other open PRs targeting |
|
/rerun |
|
Closing — widening timing margins is a band-aid. The test still uses absolute timing assertions that are inherently flaky under load. A proper fix would either remove the upper-bound assertions or test relative ordering instead of absolute windows. |
…verhead" This reverts commit 4bb8e05.
|
I see you updated files related to
|
There was a problem hiding this comment.
Pull request overview
Risk Rating: LOW — primarily test-only changes; one small production change (mutable cache defaults) that can affect behavior if unintentionally modified.
This PR deflakes timer-based tests by switching them to Go’s testing/synctest so timing assertions are deterministic under -race and CI load.
Changes:
- Use
testing/synctestinTestPlugin_GetLatenciesto avoid real-time sleeps and tighten duration assertions. - Use
testing/synctestinTestScheduledExecutionStrategy_LocalDONto stabilize timer-based scheduling assertions. - Make promwrapper cache cleanup defaults overridable from tests (by changing cache defaults from
consttovarand overridingdefaultCleanupIntervalin the test).
Scrupulous human review areas:
core/services/ocr2/plugins/promwrapper/plugin.go: introduction of mutable package-level defaults (defaultExpiration,defaultCleanupInterval) and how/where they can be mutated.core/services/ocr2/plugins/promwrapper/plugin_test.go: global override ofdefaultCleanupIntervaland ensuring it’s restored and properly scoped to the synctest run.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| core/services/ocr2/plugins/promwrapper/plugin.go | Makes cache default timings mutable to allow tests to disable go-cache GC under synctest. |
| core/services/ocr2/plugins/promwrapper/plugin_test.go | Wraps latency test in synctest and disables cache GC to avoid synctest incompatibility. |
| core/capabilities/transmission/local_target_capability_test.go | Uses synctest for deterministic schedule/timer assertions. |
Comments suppressed due to low confidence (1)
core/services/ocr2/plugins/promwrapper/plugin.go:26
- Only
defaultCleanupIntervalneeds to be overridable for synctest; turningdefaultExpirationinto a mutable package-level variable is unnecessary and increases the surface for accidental mutation. Consider keepingdefaultExpirationas aconstand definingdefaultCleanupIntervalas a separatevar(or exposing it via a constructor/option for tests).
// var instead of const to make it overridable in tests
var (
// defaultExpiration is the default expiration time for cache items.
defaultExpiration = 30 * time.Minute
// defaultCleanupInterval is the default interval for cache cleanup.
defaultCleanupInterval = 5 * time.Minute
)
|





Summary
Use synctest in TestPlugin_GetLatencies and TestScheduledExecutionStrategy_LocalDON.
Root Cause
The race detector adds 5-10x overhead to memory operations. Tests with absolute timing assertions and only 100ms margins fail intermittently under
-raceor CI CPU pressure.Test plan
TestScheduledExecutionStrategy_LocalDONpasses with-count=50 -racelocallyFixes: CORE-2382