From d21b86d53a3c4c42e41f8374e537c721251a00d2 Mon Sep 17 00:00:00 2001 From: Lukasz Szaszkiewicz Date: Thu, 14 Mar 2024 10:44:51 +0100 Subject: [PATCH] apiserver/storage/cacher: decrease running time of TestCacheWatcherDrainingNoBookmarkAfterResourceVersionReceived before: go test -v -race -count 1 -run ^TestCacheWatcherDrainingNoBookmarkAfterResourceVersionReceived$ ok k8s.io/apiserver/pkg/storage/cacher 3.792s after: go test -v -race -count 1 -run ^TestCacheWatcherDrainingNoBookmarkAfterResourceVersionReceived$ ok k8s.io/apiserver/pkg/storage/cacher 1.783s --- .../pkg/storage/cacher/cache_watcher_test.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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