Skip to content

Commit b17cc68

Browse files
authored
Merge pull request #1267 from dfajmon/data-race
fix data race on makeCapacity & fakeController
2 parents 656955b + 64f2912 commit b17cc68

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

pkg/capacity/capacity_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strconv"
2727
"strings"
2828
"sync"
29+
"sync/atomic"
2930
"testing"
3031
"time"
3132

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

13411343
func fakeController(ctx context.Context, client *fakeclientset.Clientset, owner *metav1.OwnerReference, storage CSICapacityClient, topologyInformer topology.Informer, immediateBinding bool) (*Controller, metrics.KubeRegistry) {
1342-
utilruntime.ReallyCrash = false // avoids os.Exit after "close of closed channel" in shared informer code
1343-
1344-
// We don't need resyncs, they just lead to confusing log output if they get triggered while already some
1345-
// new test is running.
13461344
resyncPeriod := time.Hour
13471345
informerFactory := informers.NewSharedInformerFactory(client, resyncPeriod)
13481346
scInformer := informerFactory.Storage().V1().StorageClasses()
@@ -1608,10 +1606,10 @@ func str2quantity(str string) *resource.Quantity {
16081606
return &quantity
16091607
}
16101608

1611-
var capacityCounter int
1609+
var capacityCounter atomic.Int32
16121610

16131611
func makeCapacity(in testCapacity) *storagev1.CSIStorageCapacity {
1614-
capacityCounter++
1612+
capacityCounter.Add(1)
16151613
var owners []metav1.OwnerReference
16161614
switch in.owner {
16171615
case nil:
@@ -1638,7 +1636,7 @@ func makeCapacity(in testCapacity) *storagev1.CSIStorageCapacity {
16381636
ObjectMeta: metav1.ObjectMeta{
16391637
UID: in.uid,
16401638
ResourceVersion: in.resourceVersion,
1641-
Name: fmt.Sprintf("csisc-%d", capacityCounter),
1639+
Name: fmt.Sprintf("csisc-%d", capacityCounter.Load()),
16421640
Namespace: ownerNamespace,
16431641
OwnerReferences: owners,
16441642
Labels: labels,

0 commit comments

Comments
 (0)