diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cache_watcher_test.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cache_watcher_test.go index 1a59615ce39..3660717e856 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cache_watcher_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cache_watcher_test.go @@ -499,10 +499,22 @@ func TestCacheWatcherDrainingNoBookmarkAfterResourceVersionReceived(t *testing.T w = newCacheWatcher(0, filter, forget, storage.APIObjectVersioner{}, time.Now(), true, schema.GroupResource{Resource: "pods"}, "") w.setBookmarkAfterResourceVersion(10) go w.processInterval(context.Background(), intervalFromEvents(initEvents), 0) - if w.add(&watchCacheEvent{Object: &v1.Pod{}}, time.NewTimer(1*time.Second)) { + + // get an event so that + // we know the w.processInterval + // has been scheduled, and + // it will be blocked on + // sending the other event + // to the result chan + <-w.ResultChan() + + // now, once we know, the processInterval + // is waiting add another event that will time out + // and start the cleanup process + if w.add(&watchCacheEvent{Object: &v1.Pod{}}, time.NewTimer(10*time.Millisecond)) { t.Fatal("expected the add method to fail") } - if err := wait.PollImmediate(1*time.Second, 5*time.Second, func() (bool, error) { + if err := wait.PollUntilContextTimeout(context.Background(), 100*time.Millisecond, 5*time.Second, true, func(_ context.Context) (bool, error) { lock.RLock() defer lock.RUnlock() return count == 2, nil