proxy-pool/internal/domain/upstream/pool_test.go

52 lines
1.6 KiB
Go

package upstream
import (
"testing"
"time"
proxyDomain "github.com/proxy-pool/proxy-pool/internal/domain/proxy"
)
func TestInventoryAvailableSlotsUsesOnlyAllocatableCapacity(t *testing.T) {
now := time.Date(2026, 7, 28, 12, 0, 0, 0, time.UTC)
inventory := Inventory{Proxies: []ProxyCapacity{
{State: proxyDomain.StateAvailable, ExpiresAt: now.Add(time.Minute), Max: 10, Active: 3, Reserved: 2},
{State: proxyDomain.StateAvailable, ExpiresAt: now.Add(5 * time.Second), Max: 10},
{State: proxyDomain.StateChecking, ExpiresAt: now.Add(time.Minute), Max: 10},
{State: proxyDomain.StateAvailable, ExpiresAt: now.Add(time.Minute), Max: 2, Active: 3},
}}
if got := inventory.AvailableSlots(now, 10*time.Second); got != 5 {
t.Fatalf("AvailableSlots() = %d, want 5", got)
}
}
func TestInventoryFetchAllowanceSeparatesPoolAndCumulativeLimits(t *testing.T) {
inventory := Inventory{
Proxies: []ProxyCapacity{
{State: proxyDomain.StateFetched},
{State: proxyDomain.StateChecking},
{State: proxyDomain.StateAvailable},
{State: proxyDomain.StateSuspect},
{State: proxyDomain.StateDraining},
{State: proxyDomain.StateExtracted},
},
PendingExpected: 2,
FetchedTotal: 98,
MaxSize: 10,
MaxTotal: 100,
}
if got := inventory.ManagedCount(); got != 7 {
t.Fatalf("ManagedCount() = %d, want 7", got)
}
if got := inventory.FetchAllowance(10); got != 2 {
t.Fatalf("FetchAllowance() = %d, want 2 from cumulative quota", got)
}
inventory.MaxTotal = 0
if got := inventory.FetchAllowance(10); got != 3 {
t.Fatalf("FetchAllowance() with unlimited cumulative quota = %d, want 3 from pool size", got)
}
}