diff --git a/staging/src/k8s.io/client-go/tools/watch/retrywatcher_test.go b/staging/src/k8s.io/client-go/tools/watch/retrywatcher_test.go index cd57e51c52..bd45dd49d4 100644 --- a/staging/src/k8s.io/client-go/tools/watch/retrywatcher_test.go +++ b/staging/src/k8s.io/client-go/tools/watch/retrywatcher_test.go @@ -33,6 +33,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/diff" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/tools/cache" "k8s.io/klog" @@ -549,9 +550,17 @@ func TestRetryWatcher(t *testing.T) { break } - counter := atomic.LoadUint32(atomicCounter) - if counter != tc.watchCount { + var counter uint32 + // We always count with the last watch reestablishing which is imminent but still a race. + // We will wait for the last watch to reestablish to avoid it. + err = wait.PollImmediate(10*time.Millisecond, 10*time.Second, func() (done bool, err error) { + counter = atomic.LoadUint32(atomicCounter) + return counter == tc.watchCount, nil + }) + if err == wait.ErrWaitTimeout { t.Errorf("expected %d watcher starts, but it has started %d times", tc.watchCount, counter) + } else if err != nil { + t.Fatal(err) } if !reflect.DeepEqual(tc.expected, got) {