Skip to content

Commit d2ff8b5

Browse files
committed
feat: use Go 1.25 features in tests
- Replace manual WaitGroup.Add/Done with sync.WaitGroup.Go - Add testing/synctest for deterministic concurrent testing - Update TestLockerConcurrency to use wg.Go() - Update TestLockerLock to use wg.Go() - Add TestLockerConcurrencyWithSynctest using testing/synctest - Update TestCurrentRequestByDatacenterAccessors to use wg.Go() - Fix linting issues (use require.NoError instead of assert.NoError)
1 parent ad28acb commit d2ff8b5

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

internal/util/locker/locker_test.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
"context"
2121
"sync"
2222
"testing"
23+
"testing/synctest"
2324
"time"
2425

25-
"github.com/stretchr/testify/assert"
2626
"github.com/stretchr/testify/require"
2727
)
2828

@@ -64,11 +64,12 @@ func TestLockerLock(t *testing.T) {
6464

6565
require.EqualValues(t, 0, lwc.count())
6666

67+
var wg sync.WaitGroup
6768
chDone := make(chan struct{})
68-
go func(t *testing.T) {
69-
assert.NoError(t, l.Lock(context.Background(), "test"))
69+
wg.Go(func() {
70+
require.NoError(t, l.Lock(context.Background(), "test"))
7071
close(chDone)
71-
}(t)
72+
})
7273

7374
chWaiting := make(chan struct{})
7475
go func() {
@@ -119,13 +120,11 @@ func TestLockerConcurrency(t *testing.T) {
119120

120121
var wg sync.WaitGroup
121122
for range 10_000 {
122-
wg.Add(1)
123-
go func(t *testing.T) {
124-
assert.NoError(t, l.Lock(context.Background(), "test"))
123+
wg.Go(func() {
124+
require.NoError(t, l.Lock(context.Background(), "test"))
125125
// If there is a concurrency issue, it will very likely become visible here.
126126
l.Unlock("test")
127-
wg.Done()
128-
}(t)
127+
})
129128
}
130129

131130
withTimeout(t, wg.Wait)
@@ -160,3 +159,28 @@ func TestLockerContextDeadlineExceeded(t *testing.T) {
160159

161160
require.NotPanics(t, func() { l.Unlock("test") })
162161
}
162+
163+
// TestLockerConcurrencyWithSynctest uses testing/synctest for deterministic concurrent testing.
164+
func TestLockerConcurrencyWithSynctest(t *testing.T) {
165+
synctest.Test(t, func(t *testing.T) {
166+
l := New()
167+
168+
var wg sync.WaitGroup
169+
// Use a smaller number for synctest to keep test execution time reasonable
170+
for range 100 {
171+
wg.Go(func() {
172+
require.NoError(t, l.Lock(context.Background(), "test"))
173+
// If there is a concurrency issue, it will very likely become visible here.
174+
l.Unlock("test")
175+
})
176+
}
177+
178+
// Wait for all goroutines to complete
179+
wg.Wait()
180+
// Ensure all goroutines in the bubble are durably blocked or finished
181+
synctest.Wait()
182+
183+
// Since everything has unlocked the map should be empty.
184+
require.Empty(t, l.locks)
185+
})
186+
}

scope/cluster_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,8 @@ func TestCurrentRequestByDatacenterAccessors(t *testing.T) {
365365
// If there is a concurrency issue, it will very likely become visible here.
366366
var wg sync.WaitGroup
367367
for i := range 10_000 {
368-
wg.Add(1)
369-
go func(t *testing.T, id string) {
370-
defer wg.Done()
371-
368+
id := strconv.Itoa(i)
369+
wg.Go(func() {
372370
req, exists := cluster.GetCurrentRequestByDatacenter(id)
373371
assert.False(t, exists)
374372
assert.Zero(t, req)
@@ -386,7 +384,7 @@ func TestCurrentRequestByDatacenterAccessors(t *testing.T) {
386384
req, exists = cluster.GetCurrentRequestByDatacenter(id)
387385
assert.False(t, exists)
388386
assert.Zero(t, req)
389-
}(t, strconv.Itoa(i))
387+
})
390388
}
391389

392390
wg.Wait()

0 commit comments

Comments
 (0)