Skip to content

Commit

Permalink
Merge pull request #5733 from anujagrawal699/addedTests-pkg/util/roun…
Browse files Browse the repository at this point in the history
…d_trippers.go-context.go-policy-go

Added tests for utility functions in the pkg/util
  • Loading branch information
karmada-bot authored Oct 24, 2024
2 parents e204459 + d35b21e commit c6c20a9
Show file tree
Hide file tree
Showing 3 changed files with 438 additions and 0 deletions.
140 changes: 140 additions & 0 deletions pkg/util/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copyright 2024 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestContextForChannel(t *testing.T) {
tests := []struct {
name string
setup func() (chan struct{}, func(context.Context, context.CancelFunc))
expectedDone bool
timeout time.Duration
}{
{
name: "context is cancelled when cancel function is called",
setup: func() (chan struct{}, func(context.Context, context.CancelFunc)) {
ch := make(chan struct{})
return ch, func(_ context.Context, cancel context.CancelFunc) {
cancel()
}
},
expectedDone: true,
timeout: time.Second,
},
{
name: "context is cancelled when parent channel is closed",
setup: func() (chan struct{}, func(context.Context, context.CancelFunc)) {
ch := make(chan struct{})
return ch, func(_ context.Context, _ context.CancelFunc) {
close(ch)
}
},
expectedDone: true,
timeout: time.Second,
},
{
name: "context remains open when neither cancelled nor parent channel closed",
setup: func() (chan struct{}, func(context.Context, context.CancelFunc)) {
ch := make(chan struct{})
return ch, func(_ context.Context, _ context.CancelFunc) {
// Do nothing - context should remain open
}
},
expectedDone: false,
timeout: 100 * time.Millisecond,
},
{
name: "concurrent operations - cancel first, then close parent",
setup: func() (chan struct{}, func(context.Context, context.CancelFunc)) {
ch := make(chan struct{})
return ch, func(_ context.Context, cancel context.CancelFunc) {
go cancel()
go func() {
time.Sleep(50 * time.Millisecond)
close(ch)
}()
}
},
expectedDone: true,
timeout: time.Second,
},
{
name: "concurrent operations - close parent first, then cancel",
setup: func() (chan struct{}, func(context.Context, context.CancelFunc)) {
ch := make(chan struct{})
return ch, func(_ context.Context, cancel context.CancelFunc) {
go close(ch)
go func() {
time.Sleep(50 * time.Millisecond)
cancel()
}()
}
},
expectedDone: true,
timeout: time.Second,
},
{
name: "multiple cancel calls should not panic",
setup: func() (chan struct{}, func(context.Context, context.CancelFunc)) {
ch := make(chan struct{})
return ch, func(_ context.Context, cancel context.CancelFunc) {
cancel()
cancel() // Second call should not panic
cancel() // Third call should not panic
}
},
expectedDone: true,
timeout: time.Second,
},
{
name: "parent channel already closed",
setup: func() (chan struct{}, func(context.Context, context.CancelFunc)) {
ch := make(chan struct{})
close(ch)
return ch, func(_ context.Context, _ context.CancelFunc) {}
},
expectedDone: true,
timeout: time.Second,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parentCh, operation := tt.setup()
ctx, cancel := ContextForChannel(parentCh)
defer cancel() // Always clean up

// Run the test operation
operation(ctx, cancel)

// Check if context is done within timeout
select {
case <-ctx.Done():
assert.True(t, tt.expectedDone, "context was cancelled but expected to remain open")
case <-time.After(tt.timeout):
assert.False(t, tt.expectedDone, "context remained open but expected to be cancelled")
}
})
}
}
56 changes: 56 additions & 0 deletions pkg/util/policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2024 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"testing"

"github.com/stretchr/testify/assert"

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
)

func TestIsLazyActivationEnabled(t *testing.T) {
tests := []struct {
name string
activationPreference policyv1alpha1.ActivationPreference
expected bool
}{
{
name: "empty activation preference",
activationPreference: "",
expected: false,
},
{
name: "lazy activation enabled",
activationPreference: policyv1alpha1.LazyActivation,
expected: true,
},
{
name: "different activation preference",
activationPreference: "SomeOtherPreference",
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsLazyActivationEnabled(tt.activationPreference)
assert.Equal(t, tt.expected, result, "unexpected result for activation preference: %s", tt.activationPreference)
})
}
}
Loading

0 comments on commit c6c20a9

Please sign in to comment.