use stopCh to avoid goroutine leak in tests

Kubernetes-commit: 132d477cb7aa323c0eae6dd9a09f9c93fb570b83
This commit is contained in:
Mskxn 2023-07-06 16:24:58 +08:00 committed by Kubernetes Publisher
parent 5d08b1abe9
commit 2595ae0416
2 changed files with 10 additions and 4 deletions

View File

@ -107,7 +107,11 @@ func TestAccessReviewCheckOnMissingNamespace(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected error initializing handler: %v", err) t.Errorf("unexpected error initializing handler: %v", err)
} }
informerFactory.Start(wait.NeverStop)
stopCh := make(chan struct{})
defer close(stopCh)
informerFactory.Start(stopCh)
err = handler.Admit(context.TODO(), admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{Group: "authorization.k8s.io", Version: "v1", Kind: "LocalSubjectAccesReview"}, namespace, "", schema.GroupVersionResource{Group: "authorization.k8s.io", Version: "v1", Resource: "localsubjectaccessreviews"}, "", admission.Create, &metav1.CreateOptions{}, false, nil), nil) err = handler.Admit(context.TODO(), admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{Group: "authorization.k8s.io", Version: "v1", Kind: "LocalSubjectAccesReview"}, namespace, "", schema.GroupVersionResource{Group: "authorization.k8s.io", Version: "v1", Resource: "localsubjectaccessreviews"}, "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
if err != nil { if err != nil {

View File

@ -115,15 +115,17 @@ func TestQueueWaitTimeLatencyTracker(t *testing.T) {
QueueSetFactory: fqs.NewQueueSetFactory(clk), QueueSetFactory: fqs.NewQueueSetFactory(clk),
}) })
informerFactory.Start(nil) stopCh := make(chan struct{})
defer close(stopCh)
status := informerFactory.WaitForCacheSync(nil) informerFactory.Start(stopCh)
status := informerFactory.WaitForCacheSync(stopCh)
if names := unsynced(status); len(names) > 0 { if names := unsynced(status); len(names) > 0 {
t.Fatalf("WaitForCacheSync did not successfully complete, resources=%#v", names) t.Fatalf("WaitForCacheSync did not successfully complete, resources=%#v", names)
} }
go func() { go func() {
controller.Run(nil) controller.Run(stopCh)
}() }()
// ensure that the controller has run its first loop. // ensure that the controller has run its first loop.