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)