Repository: kubernetes/kubernetes (language: go)

Issue:
[Flaking test] unit test TestStoreListResourceVersion
### Which jobs are flaking?

pull-kubernetes-unit

### Which tests are flaking?

TestStoreListResourceVersion in `k8s.io/apiserver/pkg/registry/generic: registry`

### Since when has it been flaking?

seems like July 7th 2024: 
![image](https://github.com/kubernetes/kubernetes/assets/10359181/c65e82e9-9983-47a8-9a06-3256258efa0e)


### Testgrid link

https://testgrid.k8s.io/presubmits-kubernetes-blocking#pull-kubernetes-unit

### Reason for failure (if possible)

Don't know, here is the log:
```
{Failed;  === RUN   TestStoreListResourceVersion
W0610 08:12:32.485776   69887 logging.go:59] [core] [Channel #205 SubChannel #208] grpc: addrConn.createTransport failed to connect to {Addr: "localhost:45055", ServerName: "localhost:45055", }. Err: connection error: desc = "transport: Error while dialing: dial tcp 127.0.0.1:45055: connect: connection refused"
    store_test.go:287: storage is (re)initializing
    store_test.go:296: expected waiting, but get <nil>
W0610 08:12:32.576001   69887 logging.go:59] [core] [Channel #325 SubChannel #327] grpc: addrConn.createTransport failed to connect to {Addr: "localhost:34737", ServerName: "localhost:34737", }. Err: connection error: desc = "transport: Error while dialing: dial tcp 127.0.0.1:34737: connect: connection refused"
--- FAIL: TestStoreListResourceVersion (0.33s)
;}
```

### Anything else we need to know?

_No response_

### Relevant SIG(s)

/sig


Discussion / hints:
This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the `triage/accepted` label and provide further guidance.

The `triage/accepted` label can be added by org members by writing `/triage accepted` in a comment.


<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
</details>
https://github.com/kubernetes/kubernetes/blob/d145bf09074fcd9b884daa582e0f19f60450a39d/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store_test.go#L255

/sig api-machinery

See also https://testgrid.k8s.io/sig-release-master-blocking#ci-kubernetes-unit

https://go.k8s.io/triage?pr=1&test=k8s.io%2Fapiserver%2Fpkg%2Fregistry%2Fgeneric
/sig etcd
/assign @serathius 
/cc @wenjiaswe 

Bisected this to https://github.com/kubernetes/kubernetes/commit/a8ef6e9f0104a44023162bb8229fb677ec80beb1
cc @wojtek-t 

Fix patch:
diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go
index a9230e1129e2c..efa67d0c19438 100644
--- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go
+++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go
@@ -1358,6 +1358,11 @@ func (c *Cacher) waitUntilWatchCacheFreshAndForceAllEvents(ctx context.Context,
 	return nil
 }
 
+// Wait blocks until the cacher is Ready or Stopped, it returns an error if Stopped.
+func (c *Cacher) Wait(ctx context.Context) error {
+	return c.ready.wait(ctx)
+}
+
 // errWatcher implements watch.Interface to return a single error
 type errWatcher struct {
 	result chan watch.Event


Test patch:
diff --git a/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store_test.go b/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store_test.go
index 25fe2700b76f1..7ef8544994a66 100644
--- a/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store_test.go
+++ b/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store_test.go
@@ -2446,6 +2446,14 @@ func newTestGenericStoreRegistry(t *testing.T, scheme *runtime.Scheme, hasCacheE
 		if err != nil {
 			t.Fatalf("Couldn't create cacher: %v", err)
 		}
+		if utilfeature.DefaultFeatureGate.Enabled(features.ResilientWatchCacheInitialization) {
+			// The tests assume that Get/GetList/Watch calls shouldn't fail.
+			// However, 429 error can now be returned if watchcache is under initialization.
+			// To avoid rewriting all tests, we wait for watchcache to initialize.
+			if err := cacher.Wait(context.Background()); err != nil {
+				t.Fatal(err)
+			}
+		}
 		d := destroyFunc
 		s = cacher
 		destroyFunc = func() {
diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go
index 5df73019b900e..ea690524068ba 100644
--- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go
+++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go
@@ -472,8 +472,8 @@ func testSetupWithEtcdServer(t *testing.T, opts ...setupOption) (context.Context
 	if utilfeature.DefaultFeatureGate.Enabled(features.ResilientWatchCacheInitialization) {
 		// The tests assume that Get/GetList/Watch calls shouldn't fail.
 		// However, 429 error can now be returned if watchcache is under initialization.
-		// To avoid rewriting all tests, we wait for watcache to initialize.
-		if err := cacher.ready.wait(ctx); err != nil {
+		// To avoid rewriting all tests, we wait for watchcache to initialize.
+		if err := cacher.Wait(ctx); err != nil {
 			t.Fatal(err)
 		}
 	}
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 0407fafbec8d3..16a56e38ea864 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
@@ -93,7 +93,7 @@ func newTestCacher(s storage.Interface) (*Cacher, storage.Versioner, error) {
 		// The tests assume that Get/GetList/Watch calls shouldn't fail.
 		// However, 429 error can now be returned if watchcache is under initialization.
 		// To avoid rewriting all tests, we wait for watcache to initialize.
-		if err := cacher.ready.wait(context.Background()); err != nil {
+		if err := cacher.Wait(context.Background()); err != nil {
 			return nil, storage.APIObjectVersioner{}, err
 		}
 	}