Skip to content

Commit f34c7eb

Browse files
committed
Use newer approach to use sync.WaitGroup which prevents leaking counters
1 parent e2ac853 commit f34c7eb

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

cmd/metal-api/internal/datastore/integer_integration_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,14 @@ func TestRethinkStore_AcquireUniqueIntegerPoolExhaustionIntegration(t *testing.T
9797
var wg sync.WaitGroup
9898

9999
for i := rs.VRFPoolRangeMin; i <= rs.VRFPoolRangeMax; i++ {
100-
wg.Add(1)
101-
go func() {
102-
defer wg.Done()
100+
wg.Go(func() {
103101
got, err := pool.AcquireRandomUniqueInteger()
104102
if err != nil {
105103
t.Fail()
106104
}
107105
assert.GreaterOrEqual(t, got, uint(rs.VRFPoolRangeMin))
108106
assert.LessOrEqual(t, got, uint(rs.VRFPoolRangeMax))
109-
}()
107+
})
110108
}
111109

112110
wg.Wait()

cmd/metal-api/internal/datastore/machine_integration_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,12 +1004,9 @@ func Test_FindWaitingMachine_NoConcurrentModificationErrors(t *testing.T) {
10041004
}
10051005

10061006
for i := range 100 {
1007-
wg.Add(1)
1008-
10091007
log := root.With("worker", i)
10101008

1011-
go func() {
1012-
defer wg.Done()
1009+
wg.Go(func() {
10131010

10141011
for {
10151012
machine, err := sharedDS.FindWaitingMachine(context.Background(), "project", "partition", size, nil, metal.RoleMachine)
@@ -1052,7 +1049,7 @@ func Test_FindWaitingMachine_NoConcurrentModificationErrors(t *testing.T) {
10521049

10531050
return
10541051
}
1055-
}()
1052+
})
10561053
}
10571054

10581055
wg.Wait()

cmd/metal-api/internal/datastore/shared_mutex_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func newLockOptAcquireTimeout(t time.Duration) *lockOptAcquireTimeout {
2222

2323
func Test_sharedMutex_reallyLocking(t *testing.T) {
2424
defer mutexCleanup(t)
25-
ctx := context.Background()
25+
ctx := t.Context()
2626
expiration := 10 * time.Second
2727

2828
err := sharedDS.sharedMutex.lock(ctx, "test", expiration, newLockOptAcquireTimeout(10*time.Millisecond))
@@ -51,20 +51,17 @@ func Test_sharedMutex_reallyLocking(t *testing.T) {
5151

5252
func Test_sharedMutex_acquireAfterRelease(t *testing.T) {
5353
defer mutexCleanup(t)
54-
ctx := context.Background()
54+
ctx := t.Context()
5555

5656
err := sharedDS.sharedMutex.lock(ctx, "test", 3*time.Second, newLockOptAcquireTimeout(10*time.Millisecond))
5757
require.NoError(t, err)
5858

5959
var wg sync.WaitGroup
60-
wg.Add(1)
61-
62-
go func() {
63-
defer wg.Done()
60+
wg.Go(func() {
6461

6562
err = sharedDS.sharedMutex.lock(ctx, "test", 1*time.Second, newLockOptAcquireTimeout(3*time.Second))
6663
assert.NoError(t, err)
67-
}()
64+
})
6865

6966
time.Sleep(1 * time.Second)
7067

@@ -75,7 +72,7 @@ func Test_sharedMutex_acquireAfterRelease(t *testing.T) {
7572

7673
func Test_sharedMutex_expires(t *testing.T) {
7774
defer mutexCleanup(t)
78-
ctx := context.Background()
75+
ctx := t.Context()
7976

8077
err := sharedDS.sharedMutex.lock(ctx, "test", 2*time.Second, newLockOptAcquireTimeout(10*time.Millisecond))
8178
require.NoError(t, err)
@@ -105,7 +102,7 @@ func Test_sharedMutex_expires(t *testing.T) {
105102

106103
func Test_sharedMutex_stop(t *testing.T) {
107104
defer mutexCleanup(t)
108-
ctx, cancel := context.WithCancel(context.Background())
105+
ctx, cancel := context.WithCancel(t.Context())
109106

110107
mutex, err := newSharedMutex(context.Background(), slog.Default(), sharedDS.dbsession)
111108
require.NoError(t, err)

0 commit comments

Comments
 (0)