//go:build !short

package connector_throttle

import (
	"context"
	"sync"
	"testing"
)

// TestThrottle_ConcurrentCallers verifies the real bucket-state Throttler is
// safe under concurrent use (enforced by `go test -race`). The bucket cache
// uses a sync.Mutex for all mutations; this test ensures N goroutines can
// call Throttle simultaneously without a data race.
//
// With a real token-bucket each caller may receive a non-zero wait; the only
// invariant tested here is absence of errors and data races.
func TestThrottle_ConcurrentCallers(t *testing.T) {
	const goroutines = 50

	svc := NewService(Config{Credentials: activeResolver("race-cred-id", "test-pack")})
	ctx := context.Background()

	var wg sync.WaitGroup
	start := make(chan struct{})
	for i := 0; i < goroutines; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			<-start // synchronized start
			_, release, err := svc.Throttle(ctx, "noop-credential", "probe")
			if err != nil {
				t.Errorf("concurrent: unexpected error: %v", err)
			}
			release()
		}()
	}
	close(start)
	wg.Wait()
}
