From b8d6fa39357192139cdc1970fe4f4d07951fc80f Mon Sep 17 00:00:00 2001 From: chrisliew Date: Mon, 16 Nov 2026 09:48:28 +0800 Subject: [PATCH] Use sync.WaitGroup.Go for goroutine fan-out Replace the wg.Add/go/defer wg.Done pattern with WaitGroup.Go (Go 1.25+) in the daemon accept loop and the two concurrency tests, and switch those tests to range-over-int loops. TestConcurrentClaimsOneGrant now records each response in its own slot and counts grants with gslice.CountBy, dropping the mutex and shared counter. No behaviour change. Co-Authored-By: Claude Opus 5.5 --- internal/client/client_test.go | 10 ++++------ internal/core/state_test.go | 21 ++++++++------------- internal/daemon/daemon.go | 6 +----- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/internal/client/client_test.go b/internal/client/client_test.go index 3b6c52b..b9cc4a3 100644 --- a/internal/client/client_test.go +++ b/internal/client/client_test.go @@ -345,17 +345,15 @@ func TestConcurrentStartOneDaemon(t *testing.T) { resps := make([]proto.Response, n) errs := make([]error, n) var wg sync.WaitGroup - for i := 0; i < n; i++ { - wg.Add(1) - go func() { - defer wg.Done() + for i := range n { + wg.Go(func() { resps[i], errs[i] = Do(proto.Request{Op: proto.OpNote}, o) - }() + }) } wg.Wait() t.Cleanup(func() { stopDaemons(t, home, resps[0].Context, resps[1].Context) }) - for i := 0; i < n; i++ { + for i := range n { if errs[i] != nil || !resps[i].OK { t.Fatalf("Do(note) call %d = %+v, %v, want OK and nil error", i, resps[i], errs[i]) } diff --git a/internal/core/state_test.go b/internal/core/state_test.go index d6c9218..31a2849 100644 --- a/internal/core/state_test.go +++ b/internal/core/state_test.go @@ -9,6 +9,8 @@ import ( "time" "unicode/utf8" + "github.com/bytedance/gg/gslice" + "github.com/ChristopherLiew/agent-quorum/internal/guard" "github.com/ChristopherLiew/agent-quorum/internal/proto" ) @@ -231,22 +233,15 @@ func TestPathsAreCleaned(t *testing.T) { func TestConcurrentClaimsOneGrant(t *testing.T) { env := newEnv() s := newState(env) + resps := make([]proto.Response, 100) var wg sync.WaitGroup - var mu sync.Mutex - grants := 0 - for i := 0; i < 100; i++ { - wg.Add(1) - go func(i int) { - defer wg.Done() - resp := s.Handle(rq(proto.OpClaim, fmt.Sprintf("s%d", i), 1000+i, "/repo/hot.go")) - if resp.OK && resp.Decision == proto.Grant { - mu.Lock() - grants++ - mu.Unlock() - } - }(i) + for i := range resps { + wg.Go(func() { + resps[i] = s.Handle(rq(proto.OpClaim, fmt.Sprintf("s%d", i), 1000+i, "/repo/hot.go")) + }) } wg.Wait() + grants := gslice.CountBy(resps, func(r proto.Response) bool { return r.OK && r.Decision == proto.Grant }) if grants != 1 { t.Fatalf("grants = %d, want 1", grants) } diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index a80a30a..07a16e0 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -155,11 +155,7 @@ func Serve(ctx context.Context, o Options) error { } continue } - wg.Add(1) - go func() { - defer wg.Done() - s.serveConn(conn) - }() + wg.Go(func() { s.serveConn(conn) }) } drained := make(chan struct{})