Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

refactor(docker): replace the custom queue with a bounded errgroup fan-out

Sergey Kozyrenko40eeea93c091
diff --git a/backend/pkg/docker/client.go b/backend/pkg/docker/client.go
index 58c1b9bf..3c1739c1 100644
--- a/backend/pkg/docker/client.go
+++ b/backend/pkg/docker/client.go
@@ -14,7 +14,6 @@ import (
"pentagi/pkg/config"
"pentagi/pkg/database"
- "pentagi/pkg/queue"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
@@ -26,6 +25,7 @@ import (
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/sirupsen/logrus"
+ "golang.org/x/sync/errgroup"
)
const WorkFolderPathInContainer = "/work"
@@ -42,17 +42,6 @@ const (
containerListWorkers = 20
)
-type containerPathStatRequest struct {
- name string
- path string
-}
-
-type containerPathStatResult struct {
- name string
- stat container.PathStat
- err error
-}
-
type dockerClient struct {
db database.Querier
logger *logrus.Logger
@@ -637,39 +626,48 @@ func (dc *dockerClient) ListContainerDir(
names = append(names, name)
}
- input := make(chan containerPathStatRequest, len(names))
- outputStats := make(chan containerPathStatResult)
- for _, name := range names {
- input <- containerPathStatRequest{
- name: name,
- path: path.Join(dirPath, name),
- }
+ stats, err := statContainerEntries(ctx, names, containerListWorkers, func(ctx context.Context, name string) (container.PathStat, error) {
+ return dc.ContainerStatPath(ctx, containerID, path.Join(dirPath, name))
+ })
+ if err != nil {
+ return nil, err
}
- close(input)
- statQueue := queue.NewQueue(input, outputStats, containerListWorkers, func(req containerPathStatRequest) (containerPathStatResult, error) {
- stat, err := dc.ContainerStatPath(ctx, containerID, req.path)
+ return stats, nil
+}
- return containerPathStatResult{
- name: req.name,
- stat: stat,
- err: err,
- }, nil
- })
- if err := statQueue.Start(); err != nil {
- return nil, fmt.Errorf("failed to start container stat queue: %w", err)
+// statContainerEntries stats every name concurrently, bounded to `workers`
+// in-flight calls, and returns the results in input order. It waits for all
+// stats before reporting, so the surfaced error is the lowest-index failure
+// deterministically — not whichever call happened to fail first.
+func statContainerEntries(
+ ctx context.Context,
+ names []string,
+ workers int,
+ statFn func(context.Context, string) (container.PathStat, error),
+) ([]container.PathStat, error) {
+ stats := make([]container.PathStat, len(names))
+ errs := make([]error, len(names))
+
+ var g errgroup.Group
+ g.SetLimit(workers)
+ for i, name := range names {
+ g.Go(func() error {
+ if stat, err := statFn(ctx, name); err != nil {
+ errs[i] = err
+ } else {
+ stats[i] = stat
+ }
+ return nil
+ })
}
+ _ = g.Wait()
- stats := make([]container.PathStat, 0, len(names))
- for range names {
- result := <-outputStats
- if result.err != nil {
- _ = statQueue.Stop()
- return nil, fmt.Errorf("failed to stat container entry '%s': %w", result.name, result.err)
+ for i, name := range names {
+ if errs[i] != nil {
+ return nil, fmt.Errorf("failed to stat container entry '%s': %w", name, errs[i])
}
- stats = append(stats, result.stat)
}
- _ = statQueue.Stop()
return stats, nil
}
diff --git a/backend/pkg/docker/client_statfanout_test.go b/backend/pkg/docker/client_statfanout_test.go
new file mode 100644
index 00000000..4976853f
--- /dev/null
+++ b/backend/pkg/docker/client_statfanout_test.go
@@ -0,0 +1,178 @@
+package docker
+
+import (
+ "context"
+ "fmt"
+ "math/rand"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/docker/docker/api/types/container"
+)
+
+func okStat(_ context.Context, name string) (container.PathStat, error) {
+ return container.PathStat{Name: name, Size: int64(len(name))}, nil
+}
+
+func TestStatContainerEntries_AllSucceedInInputOrder(t *testing.T) {
+ names := []string{"c", "a", "b", "z", "m"}
+ stats, err := statContainerEntries(context.Background(), names, 20, okStat)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if len(stats) != len(names) {
+ t.Fatalf("got %d stats, want %d", len(stats), len(names))
+ }
+ for i, name := range names {
+ if stats[i].Name != name {
+ t.Fatalf("result order not preserved at %d: got %q want %q", i, stats[i].Name, name)
+ }
+ }
+}
+
+func TestStatContainerEntries_Empty(t *testing.T) {
+ stats, err := statContainerEntries(context.Background(), nil, 20, okStat)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if len(stats) != 0 {
+ t.Fatalf("got %d stats, want 0", len(stats))
+ }
+}
+
+// With several failing entries, the surfaced error is deterministically the
+// lowest-index failure regardless of completion order, so it stays reproducible.
+func TestStatContainerEntries_LowestIndexErrorSurfaces(t *testing.T) {
+ names := []string{"e0", "e1", "e2", "e3", "e4"}
+ fail := map[string]bool{"e3": true, "e1": true} // lowest failing index is 1
+ // e3 fails fast, e1 fails slowly: completion order would surface e3, but
+ // the input-order rule must surface e1.
+ delay := map[string]time.Duration{"e1": 20 * time.Millisecond}
+ statFn := func(_ context.Context, name string) (container.PathStat, error) {
+ time.Sleep(delay[name])
+ if fail[name] {
+ return container.PathStat{}, fmt.Errorf("boom %s", name)
+ }
+ return container.PathStat{Name: name}, nil
+ }
+ stats, err := statContainerEntries(context.Background(), names, 20, statFn)
+ if err == nil {
+ t.Fatal("expected an error")
+ }
+ if stats != nil {
+ t.Fatalf("expected nil stats on error, got %v", stats)
+ }
+ if !strings.Contains(err.Error(), "entry 'e1'") {
+ t.Fatalf("want lowest-index entry 'e1' in error, got %q", err.Error())
+ }
+}
+
+// The pool never runs more than `workers` stat calls at once — the load-bearing
+// bound against the Docker daemon (which caps idle but not active connections).
+func TestStatContainerEntries_RespectsConcurrencyLimit(t *testing.T) {
+ const workers = 5
+ names := make([]string, 100)
+ for i := range names {
+ names[i] = fmt.Sprintf("e%d", i)
+ }
+
+ var cur, max atomic.Int64
+ statFn := func(_ context.Context, name string) (container.PathStat, error) {
+ c := cur.Add(1)
+ for {
+ m := max.Load()
+ if c <= m || max.CompareAndSwap(m, c) {
+ break
+ }
+ }
+ time.Sleep(2 * time.Millisecond)
+ cur.Add(-1)
+ return container.PathStat{Name: name}, nil
+ }
+
+ if _, err := statContainerEntries(context.Background(), names, workers, statFn); err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if got := max.Load(); got > workers {
+ t.Fatalf("concurrency exceeded limit: peak=%d, limit=%d", got, workers)
+ } else if got < 2 {
+ t.Fatalf("statFn never overlapped (peak=%d) — test is not exercising concurrency", got)
+ }
+}
+
+// The caller's context reaches each stat call, so a cancellation aborts them.
+func TestStatContainerEntries_ContextPropagates(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ names := []string{"a", "b", "c", "d", "e"}
+ var once atomic.Bool
+ statFn := func(ctx context.Context, name string) (container.PathStat, error) {
+ if once.CompareAndSwap(false, true) {
+ cancel()
+ }
+ select {
+ case <-ctx.Done():
+ return container.PathStat{}, ctx.Err()
+ case <-time.After(2 * time.Second):
+ return container.PathStat{Name: name}, nil
+ }
+ }
+ if _, err := statContainerEntries(ctx, names, 20, statFn); err == nil {
+ t.Fatal("expected an error once the context is cancelled")
+ }
+}
+
+// Property-based: over randomized shapes, success yields input-order stats and
+// any failure yields exactly the lowest-index entry's error. Fixed seed.
+func TestStatContainerEntries_PropertyFuzz(t *testing.T) {
+ rng := rand.New(rand.NewSource(2))
+ for iter := 0; iter < 300; iter++ {
+ n := rng.Intn(50)
+ names := make([]string, n)
+ for i := range names {
+ names[i] = fmt.Sprintf("it%d-e%d", iter, i)
+ }
+ fail := make(map[string]bool, n)
+ delay := make(map[string]time.Duration, n)
+ lowest := -1
+ for i, nm := range names {
+ if rng.Intn(3) == 0 {
+ fail[nm] = true
+ if lowest == -1 {
+ lowest = i
+ }
+ }
+ delay[nm] = time.Duration(rng.Intn(2)) * time.Millisecond
+ }
+ statFn := func(_ context.Context, name string) (container.PathStat, error) {
+ time.Sleep(delay[name])
+ if fail[name] {
+ return container.PathStat{}, fmt.Errorf("boom %s", name)
+ }
+ return container.PathStat{Name: name, Size: int64(len(name))}, nil
+ }
+
+ stats, err := statContainerEntries(context.Background(), names, 20, statFn)
+ if lowest >= 0 {
+ if err == nil {
+ t.Fatalf("iter %d: expected error for lowest-index failure %q", iter, names[lowest])
+ }
+ if want := fmt.Sprintf("entry '%s'", names[lowest]); !strings.Contains(err.Error(), want) {
+ t.Fatalf("iter %d: want %q in error, got %q", iter, want, err.Error())
+ }
+ if stats != nil {
+ t.Fatalf("iter %d: want nil stats on error", iter)
+ }
+ continue
+ }
+ if err != nil {
+ t.Fatalf("iter %d: unexpected error: %v", iter, err)
+ }
+ for i, name := range names {
+ if stats[i].Name != name {
+ t.Fatalf("iter %d: order not preserved at %d", iter, i)
+ }
+ }
+ }
+}
diff --git a/backend/pkg/queue/queue.go b/backend/pkg/queue/queue.go
deleted file mode 100644
index d22d29e4..00000000
--- a/backend/pkg/queue/queue.go
+++ /dev/null
@@ -1,239 +0,0 @@
-package queue
-
-import (
- "context"
- "errors"
- "reflect"
- "sync"
-
- "github.com/google/uuid"
- "github.com/sirupsen/logrus"
-)
-
-const defaultWorkersAmount = 10
-
-var (
- ErrAlreadyRunning = errors.New("already running")
- ErrAlreadyStopped = errors.New("already stopped")
-)
-
-type Queue interface {
- Instance() uuid.UUID
- Running() bool
- Start() error
- Stop() error
-}
-
-type message[I any] struct {
- value I
- doneCtx context.Context
- cancel context.CancelFunc
-}
-
-type queue[I any, O any] struct {
- mx *sync.Mutex
- wg *sync.WaitGroup
-
- // running is the liveness flag: true from Start() until the input closes OR
- // Stop() runs. stopCtx is the ONLY cancellation signal — workers and the
- // reader bail on it, and it is cancelled ONLY by Stop(). A normal input-close
- // flips running without touching stopCtx, so the queue still drains and
- // delivers every in-flight result rather than dropping them.
- running bool
- stopCtx context.Context
- stopCancel context.CancelFunc
-
- instance uuid.UUID
- workers int
- queue chan *message[I]
- input <-chan I
- output chan O
- process func(I) (O, error)
-}
-
-func NewQueue[I, O any](input <-chan I, output chan O, workers int, process func(I) (O, error)) Queue {
- mx, wg := &sync.Mutex{}, &sync.WaitGroup{}
- stopCtx, stopCancel := context.WithCancel(context.Background())
- stopCancel()
-
- if workers <= 0 {
- workers = defaultWorkersAmount
- }
-
- return &queue[I, O]{
- mx: mx,
- wg: wg,
-
- stopCtx: stopCtx,
- stopCancel: stopCancel,
-
- instance: uuid.New(),
- workers: workers,
- input: input,
- output: output,
- process: process,
- }
-}
-
-func (q *queue[I, O]) Instance() uuid.UUID {
- return q.instance
-}
-
-func (q *queue[I, O]) Running() bool {
- q.mx.Lock()
- defer q.mx.Unlock()
-
- return q.running
-}
-
-func (q *queue[I, O]) Start() error {
- q.mx.Lock()
- defer q.mx.Unlock()
-
- // stopCtx is live between Start() and Stop(); a normal input-close does NOT
- // cancel it. So a live stopCtx means a previous run is still winding down and
- // must be Stop()ped before restarting — this also prevents leaking the old
- // workers/stopCtx onto the shared wg.
- if q.stopCtx.Err() == nil {
- return ErrAlreadyRunning
- }
-
- q.running = true
- q.stopCtx, q.stopCancel = context.WithCancel(context.Background())
- q.queue = make(chan *message[I], q.workers*2)
-
- q.wg.Add(q.workers)
- for idx := 0; idx < q.workers; idx++ {
- go q.worker(idx)
- }
-
- // We make a buffered signal-only channel to raise possibility
- // q.reader() is running before Start() returns.
- ch := make(chan struct{}, 1)
- q.wg.Add(1)
- go func() {
- ch <- struct{}{}
- q.reader()
- }()
- <-ch
-
- return nil
-}
-
-func (q *queue[I, O]) Stop() error {
- q.mx.Lock()
-
- if q.stopCtx.Err() != nil {
- q.mx.Unlock()
- return ErrAlreadyStopped
- }
-
- q.stopCancel()
- q.running = false
- q.mx.Unlock()
- q.wg.Wait()
-
- return nil
-}
-
-func (q *queue[I, O]) inputType() string {
- return reflect.Zero(reflect.TypeOf(new(I)).Elem()).Type().String()
-}
-
-func (q *queue[I, O]) outputType() string {
- return reflect.Zero(reflect.TypeOf(new(O)).Elem()).Type().String()
-}
-
-func (q *queue[I, O]) worker(wid int) {
- defer q.wg.Done()
- logger := logrus.WithFields(logrus.Fields{
- "component": "queue_processor",
- "input_type": q.inputType(),
- "output_type": q.outputType(),
- "instance": q.instance,
- "worker": wid,
- })
- logger.Debug("worker started")
- defer logger.Debug("worker exited")
-
- for msg := range q.queue {
- if q.process == nil {
- logger.Error("no processing function provided")
- } else if result, err := q.process(msg.value); err != nil {
- logger.WithError(err).Error("failed to process message")
- } else {
- // On a hard stop, return WITHOUT msg.cancel() so later workers stay
- // blocked on their doneCtx and bail too — output ends at a contiguous
- // prefix, never with a gap.
- select {
- case <-msg.doneCtx.Done():
- case <-q.stopCtx.Done():
- return
- }
-
- select {
- case q.output <- result:
- case <-q.stopCtx.Done():
- return
- }
- }
-
- // close the context to mark this operation as complete
- msg.cancel()
- }
-}
-
-func (q *queue[I, O]) reader() {
- defer q.wg.Done()
- defer close(q.queue)
-
- logger := logrus.WithFields(logrus.Fields{
- "component": "queue_reader",
- "input_type": q.inputType(),
- "output_type": q.outputType(),
- "instance": q.instance,
- })
- logger.Debug("worker started")
- defer logger.Debug("worker exited")
-
- // create a root context as the initial doneCtx
- lastDoneCtx, cancel := context.WithCancel(context.Background())
- // cancel a root context because "previous" message was processed
- cancel()
-
- for {
- select {
- case <-q.stopCtx.Done():
- return
- case value, ok := <-q.input:
- // input closed: stop reading and let the workers drain q.queue. This is
- // NOT a hard stop, so stopCtx stays live and every queued result is
- // still delivered.
- if !ok {
- q.mx.Lock()
- q.running = false
- q.mx.Unlock()
- return
- }
-
- // create a new context for each message
- newCtx, cancel := context.WithCancel(context.Background())
-
- // Bail only on a hard Stop() so a stopped queue whose workers have
- // exited can't block the reader on a full q.queue.
- select {
- case q.queue <- &message[I]{
- value: value,
- doneCtx: lastDoneCtx,
- cancel: cancel,
- }:
- case <-q.stopCtx.Done():
- cancel()
- return
- }
-
- // update lastDoneCtx for next message
- lastDoneCtx = newCtx
- }
- }
-}
diff --git a/backend/pkg/queue/queue_scenarios_test.go b/backend/pkg/queue/queue_scenarios_test.go
deleted file mode 100644
index e2492385..00000000
--- a/backend/pkg/queue/queue_scenarios_test.go
+++ /dev/null
@@ -1,365 +0,0 @@
-package queue_test
-
-import (
- "errors"
- "fmt"
- "math/rand"
- "runtime"
- "testing"
- "time"
-
- "pentagi/pkg/queue"
-
- "github.com/google/uuid"
-)
-
-// Inputs are 0..n-1 and process returns the value unchanged, so a correct queue
-// delivers the contiguous, in-order prefix 0,1,2,... A dropped or out-of-order
-// result shows up immediately as a wrong value or a hang.
-func newIntQueue(input chan int, output chan int, workers int) queue.Queue {
- return queue.NewQueue(input, output, workers, func(i int) (int, error) { return i, nil })
-}
-
-// readPrefix reads want results, asserting each equals its index (contiguous
-// in-order prefix from 0). Fails the test on a wrong value or a timeout.
-func readPrefix(t *testing.T, output <-chan int, want int, timeout time.Duration) {
- t.Helper()
- done := make(chan error, 1)
- go func() {
- for i := 0; i < want; i++ {
- v, ok := <-output
- if !ok {
- done <- fmt.Errorf("output closed after %d/%d results", i, want)
- return
- }
- if v != i {
- done <- fmt.Errorf("gap/out-of-order at %d: got %d", i, v)
- return
- }
- }
- done <- nil
- }()
- select {
- case err := <-done:
- if err != nil {
- t.Fatal(err)
- }
- case <-time.After(timeout):
- t.Fatalf("hung waiting for %d results (in-flight result dropped?)", want)
- }
-}
-
-func mustStopWithin(t *testing.T, q queue.Queue, d time.Duration) {
- t.Helper()
- done := make(chan error, 1)
- go func() { done <- q.Stop() }()
- select {
- case err := <-done:
- if err != nil && !errors.Is(err, queue.ErrAlreadyStopped) {
- t.Fatalf("Stop returned %v", err)
- }
- case <-time.After(d):
- t.Fatal("Stop() hung")
- }
-}
-
-// The exact class of the shipped CON-1 regression: a normal input-close must
-// deliver EVERY queued result across the whole N range, including the buffer
-// boundary where the reader blocks on q.queue while workers drain.
-func TestQueue_DeliversAllAtBoundaries(t *testing.T) {
- const workers = 8
- for _, n := range []int{1, workers - 1, workers, workers * 2, workers*2 + 1, 500} {
- t.Run(fmt.Sprintf("n=%d", n), func(t *testing.T) {
- input := make(chan int, n)
- output := make(chan int)
- for i := 0; i < n; i++ {
- input <- i
- }
- close(input)
-
- q := newIntQueue(input, output, workers)
- if err := q.Start(); err != nil {
- t.Fatalf("start: %v", err)
- }
- readPrefix(t, output, n, 5*time.Second)
- mustStopWithin(t, q, 3*time.Second)
- })
- }
-}
-
-// Consumer abandons output after reading a prefix (ListContainerDir's error
-// path). What it read must be a contiguous in-order prefix, and Stop() must
-// return without hanging — for both small (reader reached input-close) and large
-// (reader blocked on q.queue) N.
-func TestQueue_ContiguousPrefixThenStopOnAbandon(t *testing.T) {
- const workers = 4 // buffer = 8
- cases := []struct{ n, read int }{
- {n: 4, read: 1}, // small: reader reaches input-close first
- {n: 4, read: 0}, // small: abandon immediately
- {n: 4, read: 4}, // small: read all
- {n: 200, read: 1}, // large: reader blocked on full q.queue
- {n: 200, read: 50},
- {n: 200, read: 200},
- }
- for _, c := range cases {
- t.Run(fmt.Sprintf("n=%d_read=%d", c.n, c.read), func(t *testing.T) {
- input := make(chan int, c.n)
- output := make(chan int)
- for i := 0; i < c.n; i++ {
- input <- i
- }
- close(input)
-
- q := newIntQueue(input, output, workers)
- if err := q.Start(); err != nil {
- t.Fatalf("start: %v", err)
- }
- readPrefix(t, output, c.read, 5*time.Second)
- mustStopWithin(t, q, 3*time.Second)
- })
- }
-}
-
-// Property-based: over many randomized shapes, the delivered results are always
-// a contiguous in-order prefix of the input and Stop() always returns. Fixed
-// seed keeps it deterministic.
-func TestQueue_PrefixInvariantFuzz(t *testing.T) {
- rng := rand.New(rand.NewSource(1))
- for iter := 0; iter < 300; iter++ {
- n := 1 + rng.Intn(200)
- workers := 1 + rng.Intn(20)
- read := rng.Intn(n + 1) // 0..n
- input := make(chan int, n)
- output := make(chan int)
- for i := 0; i < n; i++ {
- input <- i
- }
- close(input)
-
- q := newIntQueue(input, output, workers)
- if err := q.Start(); err != nil {
- t.Fatalf("iter %d start: %v", iter, err)
- }
- readPrefix(t, output, read, 5*time.Second)
- mustStopWithin(t, q, 3*time.Second)
- }
-}
-
-// After Stop() returns, no queue goroutines are left running.
-func TestQueue_NoGoroutineLeakOnAbandon(t *testing.T) {
- base := runtime.NumGoroutine()
- for i := 0; i < 20; i++ {
- input := make(chan int, 50)
- output := make(chan int)
- for j := 0; j < 50; j++ {
- input <- j
- }
- close(input)
- q := newIntQueue(input, output, 8)
- if err := q.Start(); err != nil {
- t.Fatalf("start: %v", err)
- }
- <-output // read one, abandon the rest
- mustStopWithin(t, q, 3*time.Second)
- }
- // allow scheduler to reap exited goroutines
- for i := 0; i < 100; i++ {
- if runtime.NumGoroutine() <= base+2 {
- return
- }
- time.Sleep(10 * time.Millisecond)
- }
- t.Fatalf("goroutine leak: baseline %d, now %d", base, runtime.NumGoroutine())
-}
-
-// Restarting without an intervening Stop() (a contract violation) must be
-// rejected, not silently corrupt the shared wg / stopCtx.
-func TestQueue_RestartWithoutStopRejected(t *testing.T) {
- input := make(chan int, 3)
- output := make(chan int)
- q := newIntQueue(input, output, 1)
- if err := q.Start(); err != nil {
- t.Fatalf("start1: %v", err)
- }
- for i := 0; i < 3; i++ {
- input <- i
- }
- close(input)
- <-output
- for q.Running() {
- time.Sleep(time.Millisecond) // reader processes input-close
- }
- if err := q.Start(); !errors.Is(err, queue.ErrAlreadyRunning) {
- t.Fatalf("restart without Stop: want ErrAlreadyRunning, got %v", err)
- }
- mustStopWithin(t, q, 3*time.Second)
-}
-
-func TestQueue_RestartAfterStopReuse(t *testing.T) {
- input := make(chan int)
- output := make(chan int)
- q := newIntQueue(input, output, 4)
- for cycle := 0; cycle < 3; cycle++ {
- if err := q.Start(); err != nil {
- t.Fatalf("cycle %d start: %v", cycle, err)
- }
- if !q.Running() {
- t.Fatalf("cycle %d: expected running", cycle)
- }
- if err := q.Stop(); err != nil {
- t.Fatalf("cycle %d stop: %v", cycle, err)
- }
- if q.Running() {
- t.Fatalf("cycle %d: expected stopped", cycle)
- }
- }
-}
-
-// A non-nil Go error from process() must not stall the ordering chain: the
-// worker logs it and moves on (this error path yields no output for that item,
-// which is why ListContainerDir carries its errors inside the result value).
-func TestQueue_ProcessGoErrorAdvancesChain(t *testing.T) {
- input := make(chan int, 3)
- output := make(chan string)
- for i := 0; i < 3; i++ {
- input <- i
- }
- close(input)
-
- q := queue.NewQueue(input, output, 2, func(i int) (string, error) {
- if i == 1 {
- return "", errors.New("boom")
- }
- return fmt.Sprintf("ok-%d", i), nil
- })
- if err := q.Start(); err != nil {
- t.Fatalf("start: %v", err)
- }
-
- got := make(chan []string, 1)
- go func() {
- var out []string
- for len(out) < 2 { // items 0 and 2 produce output; item 1 errors
- out = append(out, <-output)
- }
- got <- out
- }()
- select {
- case out := <-got:
- if len(out) != 2 {
- t.Fatalf("got %v", out)
- }
- case <-time.After(3 * time.Second):
- t.Fatal("hung: Go-error branch stalled the chain")
- }
- mustStopWithin(t, q, 3*time.Second)
-}
-
-func TestQueue_DoubleStop(t *testing.T) {
- input := make(chan int)
- output := make(chan int)
- q := newIntQueue(input, output, 2)
- if err := q.Start(); err != nil {
- t.Fatalf("start: %v", err)
- }
- if err := q.Stop(); err != nil {
- t.Fatalf("stop1: %v", err)
- }
- if err := q.Stop(); !errors.Is(err, queue.ErrAlreadyStopped) {
- t.Fatalf("stop2: want ErrAlreadyStopped, got %v", err)
- }
-}
-
-// Non-positive workers must fall back to the default; otherwise zero workers
-// leave nothing to drain q.queue and delivery hangs.
-func TestQueue_DefaultWorkersWhenNonPositive(t *testing.T) {
- for _, workers := range []int{0, -5} {
- t.Run(fmt.Sprintf("workers=%d", workers), func(t *testing.T) {
- const n = 20
- input := make(chan int, n)
- output := make(chan int)
- for i := 0; i < n; i++ {
- input <- i
- }
- close(input)
-
- q := newIntQueue(input, output, workers)
- if err := q.Start(); err != nil {
- t.Fatalf("start: %v", err)
- }
- readPrefix(t, output, n, 5*time.Second)
- mustStopWithin(t, q, 3*time.Second)
- })
- }
-}
-
-func TestQueue_InstanceStable(t *testing.T) {
- input := make(chan int)
- output := make(chan int)
- q := newIntQueue(input, output, 2)
- id := q.Instance()
- if id == uuid.Nil {
- t.Fatal("Instance() returned the nil UUID")
- }
- if q.Instance() != id {
- t.Fatal("Instance() changed between calls")
- }
- if newIntQueue(input, output, 2).Instance() == id {
- t.Fatal("two queues share an instance id")
- }
-}
-
-// A nil process function is a misconfiguration: the worker logs and drops each
-// item rather than delivering or panicking, and Stop() still returns cleanly.
-func TestQueue_NilProcessDropsWithoutHang(t *testing.T) {
- const n = 5
- input := make(chan int, n)
- output := make(chan int)
- for i := 0; i < n; i++ {
- input <- i
- }
- close(input)
-
- q := queue.NewQueue[int, int](input, output, 3, nil)
- if err := q.Start(); err != nil {
- t.Fatalf("start: %v", err)
- }
- select {
- case v := <-output:
- t.Fatalf("nil process delivered %d; want nothing", v)
- case <-time.After(500 * time.Millisecond):
- }
- mustStopWithin(t, q, 3*time.Second)
-}
-
-func TestQueue_RunningTransitions(t *testing.T) {
- input := make(chan int, 2)
- output := make(chan int)
- q := newIntQueue(input, output, 2)
- if q.Running() {
- t.Fatal("before Start: want not running")
- }
- if err := q.Start(); err != nil {
- t.Fatalf("start: %v", err)
- }
- if !q.Running() {
- t.Fatal("after Start: want running")
- }
- input <- 0
- input <- 1
- close(input)
- <-output
- <-output
- for i := 0; i < 200 && q.Running(); i++ {
- time.Sleep(time.Millisecond)
- }
- if q.Running() {
- t.Fatal("after input-close: want not running")
- }
- if err := q.Stop(); err != nil {
- t.Fatalf("stop: %v", err)
- }
- if q.Running() {
- t.Fatal("after Stop: want not running")
- }
-}
diff --git a/backend/pkg/queue/queue_test.go b/backend/pkg/queue/queue_test.go
deleted file mode 100644
index d654480d..00000000
--- a/backend/pkg/queue/queue_test.go
+++ /dev/null
@@ -1,331 +0,0 @@
-package queue_test
-
-import (
- "strconv"
- "testing"
- "time"
-
- "pentagi/pkg/queue"
-)
-
-func TestQueue_StartStop(t *testing.T) {
- input := make(chan int)
- output := make(chan string)
- workers := 4
-
- q := queue.NewQueue(input, output, workers, func(i int) (string, error) {
- return strconv.Itoa(i), nil
- })
-
- if running := q.Running(); running {
- t.Errorf("expected queue to be not running, but it is")
- }
-
- if err := q.Start(); err != nil {
- t.Errorf("failed to start queue: %v", err)
- }
-
- if running := q.Running(); !running {
- t.Errorf("expected queue to be running, but it is not")
- }
-
- if err := q.Stop(); err != nil {
- t.Errorf("failed to stop queue: %v", err)
- }
-
- if running := q.Running(); running {
- t.Errorf("expected queue to be not running, but it is")
- }
-}
-
-func TestQueue_CloseInputChannel(t *testing.T) {
- input := make(chan int)
- output := make(chan string)
- workers := 4
-
- q := queue.NewQueue(input, output, workers, func(i int) (string, error) {
- return strconv.Itoa(i), nil
- })
-
- if running := q.Running(); running {
- t.Errorf("expected queue to be not running, but it is")
- }
-
- if err := q.Start(); err != nil {
- t.Errorf("failed to start queue: %v", err)
- }
-
- if running := q.Running(); !running {
- t.Errorf("expected queue to be running, but it is not")
- }
-
- close(input)
- time.Sleep(100 * time.Millisecond)
-
- if running := q.Running(); running {
- t.Errorf("expected queue to be not running, but it is")
- }
-}
-
-func TestQueue_Process(t *testing.T) {
- input := make(chan int)
- output := make(chan string)
- workers := 4
-
- q := queue.NewQueue(input, output, workers, func(i int) (string, error) {
- return strconv.Itoa(i), nil
- })
-
- if err := q.Start(); err != nil {
- t.Errorf("failed to start queue: %v", err)
- }
-
- input <- 42
- result := <-output
-
- expected := "42"
- if result != expected {
- t.Errorf("unexpected result. expected: %s, got: %s", expected, result)
- }
-
- if err := q.Stop(); err != nil {
- t.Errorf("failed to stop queue: %v", err)
- }
-}
-
-func TestQueue_ProcessOrdering(t *testing.T) {
- input := make(chan int)
- output := make(chan int)
- workers := 4
-
- q := queue.NewQueue(input, output, workers, func(i int) (int, error) {
- return i + 1, nil
- })
-
- if err := q.Start(); err != nil {
- t.Errorf("failed to start queue: %v", err)
- }
-
- go func() {
- for i := 0; i < 100000; i++ {
- input <- i
- }
-
- if err := q.Stop(); err != nil {
- t.Errorf("failed to stop queue: %v", err)
- }
-
- close(input)
- close(output)
- }()
-
- var prev int
- for cur := range output {
- if cur != prev+1 {
- t.Errorf("unexpected result. expected: %d, got: %d", prev+1, cur)
- } else {
- prev = cur
- }
- }
-}
-
-// Stop() must return when the consumer stops reading output (e.g.
-// ListContainerDir bailing on a stat error), leaving workers blocked on the
-// send; otherwise wg.Wait() hangs.
-func TestQueue_StopDoesNotDeadlockWithUnreadOutput(t *testing.T) {
- input := make(chan int, 20)
- output := make(chan int)
- workers := 4
-
- q := queue.NewQueue(input, output, workers, func(i int) (int, error) {
- return i, nil
- })
- if err := q.Start(); err != nil {
- t.Fatalf("failed to start queue: %v", err)
- }
-
- for i := 0; i < 20; i++ {
- input <- i
- }
- close(input)
-
- <-output // read one, then abandon output with items still in flight
-
- done := make(chan error, 1)
- go func() { done <- q.Stop() }()
-
- select {
- case <-done:
- case <-time.After(3 * time.Second):
- t.Fatal("Stop() deadlocked with unread output")
- }
-
- if q.Running() {
- t.Error("expected queue to be stopped")
- }
-}
-
-// The ListContainerDir happy path: the consumer reads every result after input
-// closes. A normal input-close must not make workers drop in-flight results, or
-// the consumer waits forever for the last one.
-func TestQueue_DeliversEveryResultAfterInputClose(t *testing.T) {
- const n = 200
- input := make(chan int, n)
- output := make(chan int) // unbuffered, like ListContainerDir's outputStats
- for i := 0; i < n; i++ {
- input <- i
- }
- close(input)
-
- q := queue.NewQueue(input, output, 20, func(i int) (int, error) { return i, nil })
- if err := q.Start(); err != nil {
- t.Fatalf("failed to start queue: %v", err)
- }
-
- done := make(chan struct{})
- go func() {
- for i := 0; i < n; i++ {
- <-output
- }
- close(done)
- }()
-
- select {
- case <-done:
- case <-time.After(5 * time.Second):
- t.Fatal("consumer hung waiting for a result after input close")
- }
-
- _ = q.Stop()
-}
-
-// Small input closes before the consumer bails, so the reader reaches
-// input-close (which cancels ctx) first. Stop() must still hard-stop the
-// blocked workers instead of short-circuiting as "already stopped".
-func TestQueue_StopHardStopsAfterInputClose(t *testing.T) {
- input := make(chan int, 4)
- output := make(chan int)
- for i := 0; i < 4; i++ {
- input <- i
- }
- close(input)
-
- q := queue.NewQueue(input, output, 4, func(i int) (int, error) { return i, nil })
- if err := q.Start(); err != nil {
- t.Fatalf("failed to start queue: %v", err)
- }
-
- <-output // read one, abandon the rest
- for q.Running() {
- time.Sleep(time.Millisecond) // wait until the reader processes input-close
- }
-
- done := make(chan error, 1)
- go func() { done <- q.Stop() }()
- select {
- case err := <-done:
- if err != nil {
- t.Fatalf("Stop() short-circuited (%v) instead of hard-stopping blocked workers", err)
- }
- case <-time.After(3 * time.Second):
- t.Fatal("Stop() hung after input close")
- }
-}
-
-func BenchmarkQueue_DefaultWorkers(b *testing.B) {
- simpleBenchmark(b, 0)
-}
-
-func BenchmarkQueue_EightWorkers(b *testing.B) {
- simpleBenchmark(b, 8)
-}
-
-func BenchmarkQueue_FourWorkers(b *testing.B) {
- simpleBenchmark(b, 4)
-}
-
-func BenchmarkQueue_ThreeWorkers(b *testing.B) {
- simpleBenchmark(b, 3)
-}
-
-func BenchmarkQueue_TwoWorkers(b *testing.B) {
- simpleBenchmark(b, 2)
-}
-
-func BenchmarkQueue_OneWorker(b *testing.B) {
- simpleBenchmark(b, 1)
-}
-
-func BenchmarkQueue_OriginalSingleGoroutine(b *testing.B) {
- ch := make(chan struct{})
- input := make(chan int, 100)
- output := make(chan string, 100)
- process := func(i int) (string, error) {
- var res string
- for j := i; j < i+1000; j++ {
- res = strconv.Itoa(i)
- }
- return res, nil
- }
-
- go func() {
- ch <- struct{}{}
- for i := range input {
- res, _ := process(i)
- output <- res
- }
- close(output)
- }()
- <-ch
-
- b.ResetTimer()
-
- go func() {
- for i := 0; i < b.N; i++ {
- input <- i
- }
- close(input)
- }()
-
- for range output {
- }
-
- b.StopTimer()
-}
-
-func simpleBenchmark(b *testing.B, workers int) {
- input := make(chan int, 100)
- output := make(chan string, 100)
- process := func(i int) (string, error) {
- var res string
- for j := i; j < i+1000; j++ {
- res = strconv.Itoa(i)
- }
- return res, nil
- }
- q := queue.NewQueue(input, output, workers, process)
-
- if err := q.Start(); err != nil {
- b.Fatalf("failed to start queue: %v", err)
- }
-
- b.ResetTimer()
-
- go func() {
- for i := 0; i < b.N; i++ {
- input <- i
- }
-
- if err := q.Stop(); err != nil {
- b.Errorf("failed to stop queue: %v", err)
- }
-
- close(input)
- close(output)
- }()
-
- for range output {
- }
-
- b.StopTimer()
-}