From 359aff0552ed3b60d544158e5edf33d28492f01a Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 14 Jan 2026 11:57:24 +0100 Subject: [PATCH] client-go testing: include event handler in List+Watch unit test The event handler must receive both object (the initial one from List, the newer one from Watch) and it must be considered synced. --- .../testing/internal/listandwatch_test.go | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/client-go/testing/internal/listandwatch_test.go b/staging/src/k8s.io/client-go/testing/internal/listandwatch_test.go index e1996490dfe..e7bb134d078 100644 --- a/staging/src/k8s.io/client-go/testing/internal/listandwatch_test.go +++ b/staging/src/k8s.io/client-go/testing/internal/listandwatch_test.go @@ -84,6 +84,17 @@ func testListAndWatch(t *testing.T) { }, client), &v1.ConfigMap{}, defaultEventHandlerResyncPeriod, nil) }) + var adds, updates, deletes int + handle, err := configMapInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: func(_ any) { adds++ }, + UpdateFunc: func(_, _ any) { updates++ }, + DeleteFunc: func(_ any) { deletes++ }, + }) + if err != nil { + t.Fatalf("Unexpected error adding event handler: %v", err) + } + defer configMapInformer.RemoveEventHandler(handle) + configMapStore := configMapInformer.GetStore() f.Start(stopCh) f.WaitForCacheSync(stopCh) @@ -100,7 +111,7 @@ func testListAndWatch(t *testing.T) { Namespace: "default", }, } - _, err := client.CoreV1().ConfigMaps(cm.Namespace).Create(ctx, cm, metav1.CreateOptions{}) + _, err = client.CoreV1().ConfigMaps(cm.Namespace).Create(ctx, cm, metav1.CreateOptions{}) if err != nil { t.Fatalf("Unexpected error creating ConfigMap: %v", err) } @@ -112,6 +123,13 @@ func testListAndWatch(t *testing.T) { objs = configMapStore.List() if len(objs) != 2 { - t.Fatalf("Unexpected item(s) in informer cache, want 2, got %d = %v", len(objs), objs) + t.Errorf("Unexpected item(s) in informer cache, want 2, got %d = %v", len(objs), objs) + } + + if !handle.HasSynced() { + t.Error("Expected event handler to have synced, it didn't") + } + if adds != 2 || updates != 0 || deletes != 0 { + t.Errorf("Expected two new objects, got adds/updates/deletes %d/%d/%d", adds, updates, deletes) } }