Skip to content

Commit

Permalink
fix data race on makeCapacity & fakeController
Browse files Browse the repository at this point in the history
  • Loading branch information
dfajmon committed Aug 27, 2024
1 parent 66b9373 commit 14e834b
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions pkg/capacity/capacity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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 @@ -1338,9 +1339,8 @@ 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

func fakeController(ctx context.Context, client *fakeclientset.Clientset, owner *metav1.OwnerReference, storage CSICapacityClient, topologyInformer topology.Informer, immediateBinding bool) (*Controller, metrics.KubeRegistry) {
// 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
Expand Down Expand Up @@ -1608,10 +1608,28 @@ func str2quantity(str string) *resource.Quantity {
return &quantity
}

var capacityCounter int
type capacityCount struct {
sync.RWMutex
capacityCounter int
}

func (c *capacityCount) increment() {
c.Lock()
defer c.Unlock()

c.capacityCounter++
}

func (c *capacityCount) getCapacityCounter() int {
c.RLock()
defer c.RUnlock()
return c.capacityCounter
}

var capCounterVar = &capacityCount{capacityCounter: 0}

func makeCapacity(in testCapacity) *storagev1.CSIStorageCapacity {
capacityCounter++
capCounterVar.increment()
var owners []metav1.OwnerReference
switch in.owner {
case nil:
Expand All @@ -1638,7 +1656,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", capCounterVar.getCapacityCounter()),
Namespace: ownerNamespace,
OwnerReferences: owners,
Labels: labels,
Expand Down

0 comments on commit 14e834b

Please sign in to comment.