Files
Agent-Native/exec/limiter_test.go
T
weichenandCursor fedf671a48
ci / vet-and-test (push) Successful in 5s
ci / docker-checkout-hostgateway (push) Successful in 3s
Import the current agent-native module as a fresh history.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-29 12:29:04 +00:00

51 lines
962 B
Go

package exec_test
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"agent-native/exec"
)
type slowBackend struct {
running atomic.Int32
peak atomic.Int32
}
func (s *slowBackend) Name() string { return "slow" }
func (s *slowBackend) Execute(ctx context.Context, req exec.Request) (*exec.Result, error) {
cur := s.running.Add(1)
for {
p := s.peak.Load()
if cur <= p || s.peak.CompareAndSwap(p, cur) {
break
}
}
time.Sleep(40 * time.Millisecond)
s.running.Add(-1)
return &exec.Result{Success: true}, nil
}
func TestLimitingBackendCapsConcurrency(t *testing.T) {
inner := &slowBackend{}
limited := exec.WrapWithConcurrencyLimit(inner, 2)
var wg sync.WaitGroup
for i := 0; i < 6; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, _ = limited.Execute(context.Background(), exec.Request{})
}()
}
wg.Wait()
if peak := inner.peak.Load(); peak > 2 {
t.Fatalf("expected peak concurrency <= 2, got %d", peak)
}
}