client-go informers: replace time.Sleep with callback

While time.Sleep is what the test needs, maybe an arbitrary hook invocation is
more acceptable in the production code because it is more general.

Kubernetes-commit: 2ec0305d728bf5ce8f8df314a18e71aa120a00cf
This commit is contained in:
Patrick Ohly
2026-01-27 14:47:37 +01:00
committed by Kubernetes Publisher
parent 889b95a769
commit 3590eb7f48
2 changed files with 13 additions and 8 deletions

View File

@@ -895,15 +895,15 @@ func (p *sharedProcessor) distribute(obj interface{}, sync bool) {
}
}
// sharedProcessorRunDelay is used in a synctest bubble to achieve a certain ordering of
// steps in different goroutines.
var sharedProcessorRunDelay atomic.Pointer[time.Duration]
// sharedProcessorRunHook can be used inside tests to execute additional code
// at the start of sharedProcessor.run.
var sharedProcessorRunHook atomic.Pointer[func()]
func (p *sharedProcessor) run(ctx context.Context) {
func() {
delay := sharedProcessorRunDelay.Load()
if delay != nil {
time.Sleep(*delay)
hook := sharedProcessorRunHook.Load()
if hook != nil {
(*hook)()
}
// Changing listenersStarted needs a write lock.
p.listenersLock.Lock()

View File

@@ -245,8 +245,13 @@ func testListenerResyncPeriods(t *testing.T, startupDelay time.Duration) {
t.Logf("%s: %s", delta, msg)
}
sharedProcessorRunDelay.Store(&startupDelay)
defer sharedProcessorRunDelay.Store(nil)
if startupDelay > 0 {
hook := func() {
time.Sleep(startupDelay)
}
sharedProcessorRunHook.Store(&hook)
defer sharedProcessorRunHook.Store(nil)
}
// source simulates an apiserver object endpoint.
source := newFakeControllerSource(t)