mirror of
https://github.com/kubernetes/client-go.git
synced 2026-02-21 23:53:16 +00: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. Kubernetes-commit: 2ec0305d728bf5ce8f8df314a18e71aa120a00cf
This commit is contained in:
committed by
Kubernetes Publisher
parent
889b95a769
commit
3590eb7f48
12
tools/cache/shared_informer.go
vendored
12
tools/cache/shared_informer.go
vendored
@@ -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()
|
||||
|
||||
9
tools/cache/shared_informer_test.go
vendored
9
tools/cache/shared_informer_test.go
vendored
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user