Merge pull request #119127 from Mskxn/fix_leak

use stopCh to avoid goroutine leak in tests
This commit is contained in:
Kubernetes Prow Robot 2023-07-06 14:39:25 -07:00 committed by GitHub
commit 4c7cda3e55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 12 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.

View File

@ -1656,8 +1656,11 @@ func Test_syncNode(t *testing.T) {
nodeStatusUpdateFrequency: 1 * time.Second, nodeStatusUpdateFrequency: 1 * time.Second,
} }
factory.Start(nil) stopCh := make(chan struct{})
factory.WaitForCacheSync(nil) defer close(stopCh)
factory.Start(stopCh)
factory.WaitForCacheSync(stopCh)
w := eventBroadcaster.StartLogging(klog.Infof) w := eventBroadcaster.StartLogging(klog.Infof)
defer w.Stop() defer w.Stop()
@ -1740,8 +1743,11 @@ func TestGCEConditionV2(t *testing.T) {
nodeStatusUpdateFrequency: 1 * time.Second, nodeStatusUpdateFrequency: 1 * time.Second,
} }
factory.Start(nil) stopCh := make(chan struct{})
factory.WaitForCacheSync(nil) defer close(stopCh)
factory.Start(stopCh)
factory.WaitForCacheSync(stopCh)
w := eventBroadcaster.StartLogging(klog.Infof) w := eventBroadcaster.StartLogging(klog.Infof)
defer w.Stop() defer w.Stop()
@ -1828,8 +1834,11 @@ func TestGCECondition(t *testing.T) {
nodeStatusUpdateFrequency: 1 * time.Second, nodeStatusUpdateFrequency: 1 * time.Second,
} }
factory.Start(nil) stopCh := make(chan struct{})
factory.WaitForCacheSync(nil) defer close(stopCh)
factory.Start(stopCh)
factory.WaitForCacheSync(stopCh)
w := eventBroadcaster.StartLogging(klog.Infof) w := eventBroadcaster.StartLogging(klog.Infof)
defer w.Stop() defer w.Stop()
@ -1934,10 +1943,13 @@ func Test_reconcileNodeLabels(t *testing.T) {
nodeInformer: factory.Core().V1().Nodes(), nodeInformer: factory.Core().V1().Nodes(),
} }
stopCh := make(chan struct{})
defer close(stopCh)
// activate node informer // activate node informer
factory.Core().V1().Nodes().Informer() factory.Core().V1().Nodes().Informer()
factory.Start(nil) factory.Start(stopCh)
factory.WaitForCacheSync(nil) factory.WaitForCacheSync(stopCh)
err := cnc.reconcileNodeLabels("node01") err := cnc.reconcileNodeLabels("node01")
if err != test.expectedErr { if err != test.expectedErr {