diff --git a/internal/worker/pool_test.go b/internal/worker/pool_test.go index 2dd70b7..1dde3af 100644 --- a/internal/worker/pool_test.go +++ b/internal/worker/pool_test.go @@ -5,22 +5,19 @@ import ( "sync" "sync/atomic" "testing" - "time" "github.com/WilliamLi0623/codex-issue-worker/internal/issue" ) -type blockingRunner struct { - active int32 - peak int32 - release <-chan struct{} - wg *sync.WaitGroup +type gatedRunner struct { + active int32 + peak int32 + started chan int + release chan struct{} + canceled chan<- struct{} } -func (r *blockingRunner) Run(ctx context.Context, i issue.Issue) Result { - if r.wg != nil { - defer r.wg.Done() - } +func (r *gatedRunner) Run(ctx context.Context, i issue.Issue) Result { n := atomic.AddInt32(&r.active, 1) for { p := atomic.LoadInt32(&r.peak) @@ -29,65 +26,173 @@ func (r *blockingRunner) Run(ctx context.Context, i issue.Issue) Result { } } defer atomic.AddInt32(&r.active, -1) + r.started <- i.Number select { case <-r.release: return Result{Issue: i.Number, Status: Succeeded} case <-ctx.Done(): + if r.canceled != nil { + r.canceled <- struct{}{} + } return Result{Issue: i.Number, Status: Failed, Err: ctx.Err()} } } -func TestPoolBoundsConcurrency(t *testing.T) { - release := make(chan struct{}) - var wg sync.WaitGroup - wg.Add(2) - r := &blockingRunner{release: release, wg: &wg} - p := NewPool(2, r) - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - for n := 1; n <= 2; n++ { - if !p.Submit(ctx, issue.Issue{Number: n}) { - t.Fatalf("issue %d rejected", n) +func TestPoolAcceptsConcurrentSubmissionsUpToLimit(t *testing.T) { + const limit = 4 + r := &gatedRunner{started: make(chan int, limit), release: make(chan struct{})} + p := NewPool(limit, r) + + accepted := make(chan bool, 100) + var submissions sync.WaitGroup + submissions.Add(cap(accepted)) + for n := 1; n <= cap(accepted); n++ { + go func(number int) { + defer submissions.Done() + accepted <- p.Submit(context.Background(), issue.Issue{Number: number}) + }(n) + } + submissions.Wait() + + count := 0 + for range cap(accepted) { + if <-accepted { + count++ } } - if p.Submit(ctx, issue.Issue{Number: 3}) { - t.Fatal("accepted work beyond the concurrency limit") + if count != limit { + t.Fatalf("accepted=%d want %d", count, limit) + } + for range limit { + <-r.started + } + if got := atomic.LoadInt32(&r.peak); got != limit { + t.Fatalf("peak=%d want %d", got, limit) + } + close(r.release) + p.Wait() +} + +func TestPoolSuppressesConcurrentDuplicateSubmissions(t *testing.T) { + const attempts = 100 + r := &gatedRunner{started: make(chan int, 1), release: make(chan struct{})} + p := NewPool(1, r) + accepted := make(chan bool, attempts) + var submissions sync.WaitGroup + submissions.Add(attempts) + for range attempts { + go func() { + defer submissions.Done() + accepted <- p.Submit(context.Background(), issue.Issue{Number: 1}) + }() } - if p.Submit(ctx, issue.Issue{Number: 1}) { - t.Fatal("accepted duplicate issue") + submissions.Wait() + + count := 0 + for range attempts { + if <-accepted { + count++ + } } - time.Sleep(50 * time.Millisecond) - if got := atomic.LoadInt32(&r.peak); got != 2 { - t.Fatalf("peak=%d want 2", got) + if count != 1 { + t.Fatalf("accepted duplicate submissions=%d want 1", count) } - close(release) - wg.Wait() + <-r.started + close(r.release) p.Wait() } func TestPoolCancellationReachesTasks(t *testing.T) { - release := make(chan struct{}) - var wg sync.WaitGroup - wg.Add(1) - r := &blockingRunner{release: release, wg: &wg} + canceled := make(chan struct{}, 1) + r := &gatedRunner{started: make(chan int, 1), release: make(chan struct{}), canceled: canceled} p := NewPool(1, r) ctx, cancel := context.WithCancel(context.Background()) - p.Submit(ctx, issue.Issue{Number: 1}) + if !p.Submit(ctx, issue.Issue{Number: 1}) { + t.Fatal("submission was rejected") + } + <-r.started cancel() - done := make(chan struct{}) - go func() { p.Wait(); close(done) }() + p.Wait() select { - case <-done: - case <-time.After(time.Second): - t.Fatal("pool did not stop after cancellation") + case <-canceled: + default: + t.Fatal("task did not observe cancellation") } } +func TestPoolReleasesSlotAfterFailedTask(t *testing.T) { + firstStarted := make(chan struct{}) + secondStarted := make(chan int, 1) + var calls atomic.Int32 + r := runnerFunc(func(_ context.Context, i issue.Issue) Result { + if calls.Add(1) == 1 { + close(firstStarted) + return Result{Issue: i.Number, Status: Failed} + } + secondStarted <- i.Number + return Result{Issue: i.Number, Status: Succeeded} + }) + p := NewPool(1, r) + if !p.Submit(context.Background(), issue.Issue{Number: 1}) { + t.Fatal("first submission was rejected") + } + <-firstStarted + p.Wait() + if !p.Submit(context.Background(), issue.Issue{Number: 2}) { + t.Fatal("slot was not released after failed task") + } + if got := <-secondStarted; got != 2 { + t.Fatalf("second task issue=%d want 2", got) + } + p.Wait() +} + +func TestPoolBoundsHighSubmissionPressure(t *testing.T) { + const ( + limit = 8 + submits = 1000 + ) + r := &gatedRunner{started: make(chan int, limit), release: make(chan struct{})} + p := NewPool(limit, r) + accepted := make(chan bool, submits) + var submissions sync.WaitGroup + submissions.Add(submits) + for n := 1; n <= submits; n++ { + go func(number int) { + defer submissions.Done() + accepted <- p.Submit(context.Background(), issue.Issue{Number: number}) + }(n) + } + submissions.Wait() + + count := 0 + for range submits { + if <-accepted { + count++ + } + } + if count != limit { + t.Fatalf("accepted=%d under pressure, want %d", count, limit) + } + for range limit { + <-r.started + } + if got := atomic.LoadInt32(&r.peak); got > limit { + t.Fatalf("peak=%d exceeds limit %d", got, limit) + } + close(r.release) + p.Wait() +} + func TestPoolRejectsInvalidLimit(t *testing.T) { defer func() { if recover() == nil { t.Fatal("expected invalid limit to panic") } }() - NewPool(0, &blockingRunner{}) + NewPool(0, runnerFunc(func(context.Context, issue.Issue) Result { return Result{} })) } + +type runnerFunc func(context.Context, issue.Issue) Result + +func (f runnerFunc) Run(ctx context.Context, i issue.Issue) Result { return f(ctx, i) }