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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user