// Unit tests for the §0 item 4 download-pool mechanics: the shared // bandwidth bucket (reservation math, unlimited passthrough) and the // fair-share feed pick (never-served first, least-recently-served // after). Pure logic — no DB. package services import ( "testing" "time" "github.com/jackc/pgx/v5/pgtype" "github.com/stretchr/testify/assert" ) func uuidOf(s string) pgtype.UUID { var u pgtype.UUID _ = u.Scan(s) return u } func TestBandwidthLimiter(t *testing.T) { t.Run("zero rate is unlimited", func(t *testing.T) { var l bandwidthLimiter l.setRate(0) assert.Zero(t, l.take(1<<20), "unlimited → no wait, ever") assert.Zero(t, l.take(1<<20)) }) t.Run("reservations share one bucket", func(t *testing.T) { var l bandwidthLimiter l.setRate(1_000_000) // 1 MB/s start := time.Now() w1 := l.take(1_000_000) // first caller: free flow w2 := l.take(1_000_000) // second caller: waits one full second w3 := l.take(1_000_000) // third caller: waits two seconds assert.Less(t, int64(w1), int64(50*time.Millisecond), "first reservation starts immediately") assert.GreaterOrEqual(t, int64(w2), int64(900*time.Millisecond), "second waits ~1s at 1MB/s") assert.GreaterOrEqual(t, int64(w3), int64(1900*time.Millisecond), "third waits ~2s — SHARED bucket, not per-worker") assert.Less(t, time.Since(start), 100*time.Millisecond, "take() only reserves — callers sleep outside the lock") }) t.Run("idle bucket recovers", func(t *testing.T) { var l bandwidthLimiter l.setRate(1_000_000) _ = l.take(1_000_000) l.next = time.Now().Add(-time.Second) // simulate a second of idle flow assert.Less(t, int64(l.take(500_000)), int64(50*time.Millisecond), "budget accrued while idle → next take flows immediately") }) } func TestPickFeedToServe(t *testing.T) { a, b, c := uuidOf("00000000-0000-0000-0000-00000000000a"), uuidOf("00000000-0000-0000-0000-00000000000b"), uuidOf("00000000-0000-0000-0000-00000000000c") all := map[pgtype.UUID][]pgtype.UUID{a: {}, b: {}, c: {}} t.Run("never-served beats served", func(t *testing.T) { served := map[string]time.Time{ a.String(): time.Now().Add(-time.Hour), b.String(): time.Now().Add(-time.Minute), } assert.Equal(t, c, pickFeedToServe(all, served), "c was never served → c wins even though a starved longest") }) t.Run("least-recently-served wins", func(t *testing.T) { served := map[string]time.Time{ a.String(): time.Now().Add(-time.Hour), // starved longest b.String(): time.Now().Add(-time.Minute), // served recently } two := map[pgtype.UUID][]pgtype.UUID{a: {}, b: {}} assert.Equal(t, a, pickFeedToServe(two, served)) }) t.Run("empty queue picks nothing", func(t *testing.T) { assert.False(t, pickFeedToServe(map[pgtype.UUID][]pgtype.UUID{}, nil).Valid) }) }