build: modernize with "go fix -waitgroupgo": use WaitGroup.Go

This commit is contained in:
Nick Craig-Wood
2026-08-21 12:23:31 +01:00
parent 2d1a3386a8
commit a017a54bef
2 changed files with 14 additions and 28 deletions
+12 -24
View File
@@ -288,22 +288,18 @@ func TestOutputHandlerConcurrency(t *testing.T) {
// Goroutines calling Handle (text format) // Goroutines calling Handle (text format)
for range goroutines { for range goroutines {
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
for range iterations { for range iterations {
r := slog.NewRecord(t0, slog.LevelInfo, "concurrent text", 0) r := slog.NewRecord(t0, slog.LevelInfo, "concurrent text", 0)
r.AddAttrs(slog.String("object", "obj")) r.AddAttrs(slog.String("object", "obj"))
_ = h.Handle(ctx, r) _ = h.Handle(ctx, r)
} }
}() })
} }
// Goroutines calling setFormat (switching between text and JSON) // Goroutines calling setFormat (switching between text and JSON)
for range 2 { for range 2 {
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
for j := range iterations { for j := range iterations {
if j%2 == 0 { if j%2 == 0 {
h.setFormat(logFormatDate | logFormatTime) h.setFormat(logFormatDate | logFormatTime)
@@ -311,23 +307,19 @@ func TestOutputHandlerConcurrency(t *testing.T) {
h.setFormat(logFormatJSON) h.setFormat(logFormatJSON)
} }
} }
}() })
} }
// Goroutines calling setFormatFlags / clearFormatFlags // Goroutines calling setFormatFlags / clearFormatFlags
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
for range iterations { for range iterations {
h.setFormatFlags(logFormatPid | logFormatMicroseconds) h.setFormatFlags(logFormatPid | logFormatMicroseconds)
h.clearFormatFlags(logFormatPid | logFormatMicroseconds) h.clearFormatFlags(logFormatPid | logFormatMicroseconds)
} }
}() })
// Goroutines calling SetLevel // Goroutines calling SetLevel
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
for j := range iterations { for j := range iterations {
if j%2 == 0 { if j%2 == 0 {
h.SetLevel(slog.LevelDebug) h.SetLevel(slog.LevelDebug)
@@ -335,28 +327,24 @@ func TestOutputHandlerConcurrency(t *testing.T) {
h.SetLevel(slog.LevelInfo) h.SetLevel(slog.LevelInfo)
} }
} }
}() })
// Goroutines calling SetOutput / ResetOutput // Goroutines calling SetOutput / ResetOutput
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
noop := func(_ slog.Level, _ string) {} noop := func(_ slog.Level, _ string) {}
for range iterations { for range iterations {
h.SetOutput(noop) h.SetOutput(noop)
h.ResetOutput() h.ResetOutput()
} }
}() })
// Goroutines calling WithAttrs / WithGroup (reads format) // Goroutines calling WithAttrs / WithGroup (reads format)
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
for range iterations { for range iterations {
_ = h.WithAttrs(nil) _ = h.WithAttrs(nil)
_ = h.WithGroup("g") _ = h.WithGroup("g")
} }
}() })
// Use a channel with a timeout to detect deadlocks // Use a channel with a timeout to detect deadlocks
done := make(chan struct{}) done := make(chan struct{})
+2 -4
View File
@@ -58,16 +58,14 @@ func TestAuxConcurrent(t *testing.T) {
wg sync.WaitGroup wg sync.WaitGroup
) )
for i := range owners { for i := range owners {
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
owner := &i owner := &i
for j := range iterations { for j := range iterations {
value := fmt.Sprintf("%d-%d", i, j) value := fmt.Sprintf("%d-%d", i, j)
a.SetAux(owner, value) a.SetAux(owner, value)
assert.Equal(t, value, a.Aux(owner)) assert.Equal(t, value, a.Aux(owner))
} }
}() })
} }
wg.Wait() wg.Wait()
} }