Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix data race on makeCapacity & fakeController #1267

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions pkg/capacity/capacity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -132,6 +133,7 @@ csistoragecapacities_obsolete %d
// TestCapacityController checks that the controller handles the initial state and
// several different changes at runtime correctly.
func TestCapacityController(t *testing.T) {
utilruntime.ReallyCrash = false // avoids os.Exit after "close of closed channel" in shared informer code
testcases := map[string]struct {
immediateBinding bool
owner *metav1.OwnerReference
Expand Down Expand Up @@ -1339,10 +1341,6 @@ func updateCSIStorageCapacityReactor() func(action ktesting.Action) (handled boo
}

func fakeController(ctx context.Context, client *fakeclientset.Clientset, owner *metav1.OwnerReference, storage CSICapacityClient, topologyInformer topology.Informer, immediateBinding bool) (*Controller, metrics.KubeRegistry) {
utilruntime.ReallyCrash = false // avoids os.Exit after "close of closed channel" in shared informer code

// We don't need resyncs, they just lead to confusing log output if they get triggered while already some
// new test is running.
resyncPeriod := time.Hour
informerFactory := informers.NewSharedInformerFactory(client, resyncPeriod)
scInformer := informerFactory.Storage().V1().StorageClasses()
Expand Down Expand Up @@ -1608,10 +1606,10 @@ func str2quantity(str string) *resource.Quantity {
return &quantity
}

var capacityCounter int
var capacityCounter atomic.Int32

func makeCapacity(in testCapacity) *storagev1.CSIStorageCapacity {
capacityCounter++
capacityCounter.Add(1)
var owners []metav1.OwnerReference
switch in.owner {
case nil:
Expand All @@ -1638,7 +1636,7 @@ func makeCapacity(in testCapacity) *storagev1.CSIStorageCapacity {
ObjectMeta: metav1.ObjectMeta{
UID: in.uid,
ResourceVersion: in.resourceVersion,
Name: fmt.Sprintf("csisc-%d", capacityCounter),
Name: fmt.Sprintf("csisc-%d", capacityCounter.Load()),
Namespace: ownerNamespace,
OwnerReferences: owners,
Labels: labels,
Expand Down