mirror of
https://github.com/kubernetes/kubernetes.git
synced 2026-05-28 04:04:39 -04:00
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.
This commit is contained in:
parent
e6ef79b2f6
commit
2ec0305d72
2 changed files with 13 additions and 8 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue