You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The TestMetricsRefresh test in pod_metrics_test.go was flaky due to a
race condition. The `StopRefreshLoop` method would signal the metrics
refresh goroutine to stop but did not wait for its actual termination.
If the test updated the mock metrics client immediately after calling
`StopRefreshLoop`, the refresh goroutine could, in rare cases, perform
a final metrics fetch with the new data before fully exiting. This
resulted in the test asserting against unexpected metric values.
This commit resolves the issue by making `StopRefreshLoop` a synchronous
operation. A `sync.WaitGroup` is now used:
- `wg.Add(1)` is called before the refresh goroutine starts.
- `defer wg.Done()` is used within the refresh goroutine to signal
completion.
- `wg.Wait()` is called in `StopRefreshLoop` to block until the
goroutine has fully terminated.
- `stopOnce` is used to ensure the `done` channel is only closed once
(for idempotency and protection against concurrent calls).
This change ensures that when `StopRefreshLoop` returns, the refresh
goroutine is guaranteed to have stopped, eliminating the race condition.
0 commit comments