refactor: use sync.WaitGroup.Go in tests (#26671)

Migrate `wg.Add(1); go func() { defer wg.Done(); ... }()` to
`wg.Go(func() { ... })` in tests.

Where the prior pattern passed the loop variable explicitly via a
closure parameter (`go func(id int) { ... }(i)`), drop the parameter and
reference the loop variable directly. Per-iteration loop variables since
Go 1.22 make this safe.
This commit is contained in:
Zach
2026-06-25 15:41:09 -06:00
committed by GitHub
parent b95f2531b5
commit 953091c7bc
24 changed files with 100 additions and 180 deletions
+6 -12
View File
@@ -124,15 +124,13 @@ func TestTracker_Track_Concurrent(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
workspaceID := uuid.New()
ownerID := uuid.New()
for j := 0; j < requestsPerGoroutine; j++ {
tracker.Track(workspaceID, ownerID, 1, 1)
}
}()
})
}
wg.Wait()
@@ -507,22 +505,18 @@ func TestTracker_ConcurrentFlushAndTrack(t *testing.T) {
var wg sync.WaitGroup
// Goroutine 1: Continuously track.
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
for i := 0; i < numOperations; i++ {
tracker.Track(uuid.New(), uuid.New(), 1, 1)
}
}()
})
// Goroutine 2: Continuously flush.
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
for i := 0; i < numOperations; i++ {
_ = tracker.FlushToDB(ctx, db, replicaID)
}
}()
})
wg.Wait()
+2 -4
View File
@@ -286,9 +286,7 @@ func TestConcurrencyLimit(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < maxConcurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"/", nil)
if err != nil {
results <- result{err: err}
@@ -301,7 +299,7 @@ func TestConcurrencyLimit(t *testing.T) {
}
defer resp.Body.Close()
results <- result{statusCode: resp.StatusCode}
}()
})
}
// Wait for all requests to enter the handler with a timeout.
+4 -8
View File
@@ -445,11 +445,9 @@ func TestSMTP(t *testing.T) {
// Start mock SMTP server in the background.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
assert.NoError(t, srv.Serve(listen))
}()
})
// Wait for the server to become pingable.
require.Eventually(t, func() bool {
@@ -590,11 +588,9 @@ func TestSMTPEnvelopeAndHeaders(t *testing.T) {
handler := dispatch.NewSMTPHandler(cfg, logger.Named("smtp"))
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
assert.NoError(t, srv.Serve(listen))
}()
})
require.Eventually(t, func() bool {
cl, err := smtptest.PingClient(listen, false, false)
+2 -4
View File
@@ -1541,11 +1541,9 @@ func TestNotificationTemplates_Golden(t *testing.T) {
// Start mock SMTP server in the background.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
assert.NoError(t, srv.Serve(listen))
}()
})
// Wait for the server to become pingable.
require.Eventually(t, func() bool {
+12 -21
View File
@@ -421,13 +421,10 @@ func TestOAuth2ConcurrentSecurityOperations(t *testing.T) {
// Launch concurrent attempts to access the client configuration
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
wg.Go(func() {
_, err := client.GetOAuth2ClientConfiguration(ctx, regResp.ClientID, regResp.RegistrationAccessToken)
errors[index] = err
}(i)
errors[i] = err
})
}
wg.Wait()
@@ -448,23 +445,20 @@ func TestOAuth2ConcurrentSecurityOperations(t *testing.T) {
// Launch concurrent attempts with invalid tokens
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
_, err := client.GetOAuth2ClientConfiguration(ctx, regResp.ClientID, fmt.Sprintf("invalid-token-%d", index))
wg.Go(func() {
_, err := client.GetOAuth2ClientConfiguration(ctx, regResp.ClientID, fmt.Sprintf("invalid-token-%d", i))
if err == nil {
t.Errorf("Expected error for goroutine %d", index)
t.Errorf("Expected error for goroutine %d", i)
return
}
var httpErr *codersdk.Error
if !errors.As(err, &httpErr) {
t.Errorf("Expected codersdk.Error for goroutine %d", index)
t.Errorf("Expected codersdk.Error for goroutine %d", i)
return
}
statusCodes[index] = httpErr.StatusCode()
}(i)
statusCodes[i] = httpErr.StatusCode()
})
}
wg.Wait()
@@ -494,13 +488,10 @@ func TestOAuth2ConcurrentSecurityOperations(t *testing.T) {
// Launch concurrent deletion attempts
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
wg.Go(func() {
err := client.DeleteOAuth2ClientConfiguration(ctx, deleteRegResp.ClientID, deleteRegResp.RegistrationAccessToken)
deleteResults[index] = err
}(i)
deleteResults[i] = err
})
}
wg.Wait()
+6 -12
View File
@@ -87,11 +87,9 @@ func TestTracker(t *testing.T) {
var wg sync.WaitGroup
count = 0
for i := 0; i < len(ids); i++ {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
tickCh <- now
}()
})
wut.Add(ids[i])
}
@@ -173,18 +171,14 @@ func TestTracker_MultipleInstances(t *testing.T) {
nowB := now.Add(2 * time.Minute)
var wg sync.WaitGroup
var flushedA, flushedB int
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
wuTickA <- nowA
flushedA = <-wuFlushA
}()
wg.Add(1)
go func() {
defer wg.Done()
})
wg.Go(func() {
wuTickB <- nowB
flushedB = <-wuFlushB
}()
})
wg.Wait()
// We expect 5 flushed IDs each
+2 -4
View File
@@ -416,12 +416,10 @@ func TestConfigCache_Singleflight(t *testing.T) {
var wg sync.WaitGroup
start := make(chan struct{})
for i := 0; i < callers; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
wg.Go(func() {
<-start
results[i], errs[i] = cache.EnabledProviders(ctx)
}(i)
})
}
close(start)
+2 -4
View File
@@ -139,11 +139,9 @@ func TestPubsub(t *testing.T) {
var first, second error
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
first = ps.Close()
}()
})
wg.Wait()
second = ps.Close()
assert.NoError(t, first)