From 7682b7427171e16cc8db060ef3171cd191ff7192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Tyczy=C5=84ski?= Date: Thu, 31 Oct 2024 12:33:04 +0100 Subject: [PATCH] Fix TestCacherDontMissEventsOnReinitialization test --- .../pkg/storage/cacher/cacher_whitebox_test.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go index fa7257f216d..12b1e9e8a4c 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go @@ -899,6 +899,7 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) { case 1: podList.ListMeta = metav1.ListMeta{ResourceVersion: "10"} default: + t.Errorf("unexpected list call: %d", listCalls) err = fmt.Errorf("unexpected list call") } listCalls++ @@ -921,8 +922,11 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) { for i := 12; i < 18; i++ { w.Add(makePod(i)) } - w.Stop() + // Keep the watch open to avoid another reinitialization, + // but register it for cleanup. + t.Cleanup(func() { w.Stop() }) default: + t.Errorf("unexpected watch call: %d", watchCalls) err = fmt.Errorf("unexpected watch call") } watchCalls++ @@ -944,7 +948,6 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - errCh := make(chan error, concurrency) for i := 0; i < concurrency; i++ { go func() { defer wg.Done() @@ -968,11 +971,11 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) { } rv, err := strconv.Atoi(object.(*example.Pod).ResourceVersion) if err != nil { - errCh <- fmt.Errorf("incorrect resource version: %v", err) + t.Errorf("incorrect resource version: %v", err) return } if prevRV != -1 && prevRV+1 != rv { - errCh <- fmt.Errorf("unexpected event received, prevRV=%d, rv=%d", prevRV, rv) + t.Errorf("unexpected event received, prevRV=%d, rv=%d", prevRV, rv) return } prevRV = rv @@ -981,11 +984,6 @@ func TestCacherDontMissEventsOnReinitialization(t *testing.T) { }() } wg.Wait() - close(errCh) - - for err := range errCh { - t.Error(err) - } } func TestCacherNoLeakWithMultipleWatchers(t *testing.T) {