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:
@@ -474,12 +474,10 @@ func TestCircuitBreaker_HalfOpenMaxRequests(t *testing.T) {
|
||||
responses := make(chan int, totalRequests)
|
||||
|
||||
for i := 0; i < totalRequests; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
wg.Go(func() {
|
||||
status := doRequest()
|
||||
responses <- status
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
@@ -619,11 +619,9 @@ func TestKeyConcurrent(t *testing.T) {
|
||||
const numGoroutines = 10
|
||||
var wg sync.WaitGroup
|
||||
for r := range numGoroutines {
|
||||
wg.Add(1)
|
||||
go func(r int) {
|
||||
defer wg.Done()
|
||||
wg.Go(func() {
|
||||
tc.run(r, key)
|
||||
}(r)
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user