84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
package extraction
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestMemoryStoreNeverExtractsProxyTwice(t *testing.T) {
|
|
now := time.Date(2026, 7, 28, 10, 0, 0, 0, time.UTC)
|
|
store := NewMemoryStore([]Candidate{
|
|
{ID: "p1", State: Available, ExpiresAt: now.Add(time.Minute), LastCheckedAt: now},
|
|
})
|
|
|
|
var wg sync.WaitGroup
|
|
results := make(chan string, 1000)
|
|
for range 1000 {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
result, err := store.Extract(context.Background(), Command{
|
|
Requested: 1,
|
|
Fulfillment: Partial,
|
|
Now: now,
|
|
MinRemainingTTL: 30 * time.Second,
|
|
MaxHealthCheckAge: 10 * time.Second,
|
|
})
|
|
if err != nil {
|
|
t.Errorf("Extract(): %v", err)
|
|
return
|
|
}
|
|
for _, item := range result.Items {
|
|
results <- item.ID
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
close(results)
|
|
|
|
count := 0
|
|
for id := range results {
|
|
if id != "p1" {
|
|
t.Fatalf("unexpected proxy %q", id)
|
|
}
|
|
count++
|
|
}
|
|
if count != 1 {
|
|
t.Fatalf("proxy extracted %d times, want exactly once", count)
|
|
}
|
|
}
|
|
|
|
func TestAllOrNothingDoesNotConsumePartialInventory(t *testing.T) {
|
|
now := time.Date(2026, 7, 28, 10, 0, 0, 0, time.UTC)
|
|
store := NewMemoryStore([]Candidate{
|
|
{ID: "p1", State: Available, ExpiresAt: now.Add(time.Minute), LastCheckedAt: now},
|
|
})
|
|
|
|
result, err := store.Extract(context.Background(), Command{
|
|
Requested: 2,
|
|
Fulfillment: AllOrNothing,
|
|
Now: now,
|
|
MinRemainingTTL: 30 * time.Second,
|
|
MaxHealthCheckAge: 10 * time.Second,
|
|
})
|
|
if err != ErrInsufficientProxies {
|
|
t.Fatalf("Extract() error = %v, want ErrInsufficientProxies", err)
|
|
}
|
|
if len(result.Items) != 0 {
|
|
t.Fatalf("Extract() returned %d items, want 0", len(result.Items))
|
|
}
|
|
|
|
partial, err := store.Extract(context.Background(), Command{
|
|
Requested: 1,
|
|
Fulfillment: Partial,
|
|
Now: now,
|
|
MinRemainingTTL: 30 * time.Second,
|
|
MaxHealthCheckAge: 10 * time.Second,
|
|
})
|
|
if err != nil || len(partial.Items) != 1 {
|
|
t.Fatalf("inventory was consumed by failed all-or-nothing: result=%+v err=%v", partial, err)
|
|
}
|
|
}
|