Skip to content

Commit

Permalink
Merge pull request #5804 from XiShanYongYe-Chang/cancel-concurrent-test
Browse files Browse the repository at this point in the history
[UT] cancel concurrent test for SetupWithManager
  • Loading branch information
karmada-bot authored Nov 11, 2024
2 parents ed3f48c + 41ba3c7 commit 90fe21e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 49 deletions.
63 changes: 20 additions & 43 deletions pkg/controllers/namespace/namespace_sync_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package namespace
import (
"context"
"regexp"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -349,10 +348,9 @@ func TestController_buildWorks(t *testing.T) {

func TestController_SetupWithManager(t *testing.T) {
tests := []struct {
name string
setupScheme func(*runtime.Scheme)
concurrentRuns int
expectError bool
name string
setupScheme func(*runtime.Scheme)
expectError bool
}{
{
name: "Successful setup",
Expand All @@ -362,25 +360,12 @@ func TestController_SetupWithManager(t *testing.T) {
_ = workv1alpha1.Install(scheme)
_ = policyv1alpha1.Install(scheme)
},
concurrentRuns: 1,
expectError: false,
expectError: false,
},
{
name: "Concurrent setup",
setupScheme: func(scheme *runtime.Scheme) {
_ = corev1.AddToScheme(scheme)
_ = clusterv1alpha1.Install(scheme)
_ = workv1alpha1.Install(scheme)
_ = policyv1alpha1.Install(scheme)
},
concurrentRuns: 10,
expectError: false,
},
{
name: "Setup with error",
setupScheme: func(_ *runtime.Scheme) {}, // Intentionally empty to trigger error
concurrentRuns: 1,
expectError: true,
name: "Setup with error",
setupScheme: func(_ *runtime.Scheme) {}, // Intentionally empty to trigger error
expectError: true,
},
}

Expand All @@ -395,28 +380,20 @@ func TestController_SetupWithManager(t *testing.T) {
})
assert.NoError(t, err)

var wg sync.WaitGroup
for i := 0; i < tt.concurrentRuns; i++ {
wg.Add(1)
go func() {
defer wg.Done()
c := &Controller{
Client: mgr.GetClient(),
EventRecorder: mgr.GetEventRecorderFor("test-controller"),
OverrideManager: overridemanager.New(mgr.GetClient(), mgr.GetEventRecorderFor("test-controller")),
}
err := c.SetupWithManager(mgr)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, c.Client, "Controller's Client should not be nil")
assert.NotNil(t, c.EventRecorder, "Controller's EventRecorder should not be nil")
assert.NotNil(t, c.OverrideManager, "Controller's OverrideManager should not be nil")
}
}()
c := &Controller{
Client: mgr.GetClient(),
EventRecorder: mgr.GetEventRecorderFor("test-controller"),
OverrideManager: overridemanager.New(mgr.GetClient(), mgr.GetEventRecorderFor("test-controller")),
}
err = c.SetupWithManager(mgr)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, c.Client, "Controller's Client should not be nil")
assert.NotNil(t, c.EventRecorder, "Controller's EventRecorder should not be nil")
assert.NotNil(t, c.OverrideManager, "Controller's OverrideManager should not be nil")
}
wg.Wait()
})
}
}
6 changes: 0 additions & 6 deletions pkg/controllers/remediation/remedy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,6 @@ func TestSetupWithManager(t *testing.T) {
return &RemedyController{Client: createFakeClient(scheme)}
},
},
{
name: "setup with nil client",
controllerSetup: func() *RemedyController {
return &RemedyController{Client: nil}
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 90fe21e

Please sign in to comment.