From 3590eb7f48f83a523c516d9048b98d545316130c Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Tue, 27 Jan 2026 14:47:37 +0100 Subject: [PATCH] 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 --- tools/cache/shared_informer.go | 12 ++++++------ tools/cache/shared_informer_test.go | 9 +++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/tools/cache/shared_informer.go b/tools/cache/shared_informer.go index 5c4413aa0..aa5c693db 100644 --- a/tools/cache/shared_informer.go +++ b/tools/cache/shared_informer.go @@ -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() diff --git a/tools/cache/shared_informer_test.go b/tools/cache/shared_informer_test.go index ee6ba21b3..0b40315a8 100644 --- a/tools/cache/shared_informer_test.go +++ b/tools/cache/shared_informer_test.go @@ -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)