mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
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:
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user